Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding of records with missing fields. #6587

Merged
merged 23 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/basic_cli_build_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ env:
# use .tar.gz for quick testing
ARCHIVE_FORMAT: .tar.br
# Make a new basic-cli git tag and set it here before starting this workflow
RELEASE_TAG: 0.7.1
RELEASE_TAG: 0.9.0

jobs:
prepare:
Expand All @@ -38,6 +38,10 @@ jobs:
- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_arm64-latest.tar.gz
- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-macos_x86_64-latest.tar.gz
- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-macos_apple_silicon-latest.tar.gz
#- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_x86_64-TESTING.tar.gz
#- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_arm64-TESTING.tar.gz
#- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-macos_x86_64-TESTING.tar.gz
#- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-macos_apple_silicon-TESTING.tar.gz

- name: Save roc_nightly archives
uses: actions/upload-artifact@v4
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/basic_webserver_build_release.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
on:
# pull_request:
#pull_request:
workflow_dispatch:

# this cancels workflows currently in progress if you start a new one
Expand All @@ -18,10 +18,16 @@ jobs:
steps:
- uses: actions/checkout@v4

# get latest nightly releases
- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_x86_64-latest.tar.gz
- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_arm64-latest.tar.gz
- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-macos_x86_64-latest.tar.gz
- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-macos_apple_silicon-latest.tar.gz
#- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_x86_64-TESTING.tar.gz
#- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_arm64-TESTING.tar.gz
#- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-macos_x86_64-TESTING.tar.gz
#- run: curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-macos_apple_silicon-TESTING.tar.gz


- name: Save roc_nightly archives
uses: actions/upload-artifact@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly_linux_arm64.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
on:
# pull_request:
#pull_request:
workflow_dispatch:
schedule:
- cron: '0 9 * * *'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly_linux_x86_64.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
on:
# pull_request:
#pull_request:
workflow_dispatch:
schedule:
- cron: '0 9 * * *'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly_macos_apple_silicon.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
on:
# pull_request:
#pull_request:
workflow_dispatch:
schedule:
- cron: '0 9 * * *'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly_macos_x86_64.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
on:
# pull_request:
#pull_request:
workflow_dispatch:
schedule:
- cron: "0 9 * * *" # 9=9am utc+0
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly_old_linux_arm64.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
on:
# pull_request:
#pull_request:
workflow_dispatch:
schedule:
- cron: '0 9 * * *'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly_old_linux_x86_64.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
on:
# pull_request:
#pull_request:
workflow_dispatch:
schedule:
- cron: '0 9 * * *'
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/cli_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ mod cli_run {
This roc file can print its own source code. The source is:

app "ingested-file"
packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.8.1/x8URkvfyi9I0QhmVG98roKBUs_AZRkLFwFJVJ3942YA.tar.br" }
packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.9.0/oKWkaruh2zXxin_xfsYsCJobH1tO8_JvNkFzDwwzNUQ.tar.br" }
imports [
pf.Stdout,
"ingested-file.roc" as ownCode : Str,
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/builtins/roc/Decode.roc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ DecoderFormatting implements
## `Skip` if the field is not a part of the decoded record.
##
## `finalizer` should produce the record value from the decoded `state`.
record : state, (state, Str -> [Keep (Decoder state fmt), Skip]), (state -> Result val DecodeError) -> Decoder val fmt where fmt implements DecoderFormatting
record : state, (state, Str -> [Keep (Decoder state fmt), Skip]), (state, fmt -> Result val DecodeError) -> Decoder val fmt where fmt implements DecoderFormatting

## `tuple state stepElem finalizer` decodes a tuple element-by-element.
##
Expand Down
75 changes: 46 additions & 29 deletions crates/compiler/builtins/roc/TotallyNotJson.roc
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,20 @@ escapedByteToJson = \b ->
encodeList = \lst, encodeElem ->
Encode.custom \bytes, @Json {} ->
writeList = \{ buffer, elemsLeft }, elem ->
beforeBufferLen = buffer |> List.len

bufferWithElem = appendWith buffer (encodeElem elem) (@Json {})
bufferWithSuffix =
if elemsLeft > 1 then
List.append bufferWithElem (Num.toU8 ',')
else
bufferWithElem
# If our encoder returned [] we just skip the elem
if bufferWithElem |> List.len == beforeBufferLen then
{ buffer: bufferWithElem, elemsLeft: elemsLeft - 1 }
else
bufferWithSuffix =
if elemsLeft > 1 then
List.append bufferWithElem (Num.toU8 ',')
else
bufferWithElem

{ buffer: bufferWithSuffix, elemsLeft: elemsLeft - 1 }
{ buffer: bufferWithSuffix, elemsLeft: elemsLeft - 1 }

head = List.append bytes (Num.toU8 '[')
{ buffer: withList } = List.walk lst { buffer: head, elemsLeft: List.len lst } writeList
Expand All @@ -249,21 +255,27 @@ encodeList = \lst, encodeElem ->
encodeRecord = \fields ->
Encode.custom \bytes, @Json {} ->
writeRecord = \{ buffer, fieldsLeft }, { key, value } ->
fieldName = key
bufferWithKeyValue =
List.append buffer (Num.toU8 '"')
|> List.concat (Str.toUtf8 fieldName)
|> List.append (Num.toU8 '"')
|> List.append (Num.toU8 ':') # Note we need to encode using the json config here
|> appendWith value (@Json {})

bufferWithSuffix =
if fieldsLeft > 1 then
List.append bufferWithKeyValue (Num.toU8 ',')
else
bufferWithKeyValue

{ buffer: bufferWithSuffix, fieldsLeft: fieldsLeft - 1 }
fieldValue = [] |> appendWith value (json)
# If our encoder returned [] we just skip the field
if fieldValue == [] then
{ buffer, fieldsLeft: fieldsLeft - 1 }
else
fieldName = key
bufferWithKeyValue =
List.append buffer (Num.toU8 '"')
|> List.concat (Str.toUtf8 fieldName)
|> List.append (Num.toU8 '"')
|> List.append (Num.toU8 ':') # Note we need to encode using the json config here
|> List.concat fieldValue

bufferWithSuffix =
if fieldsLeft > 1 then
List.append bufferWithKeyValue (Num.toU8 ',')
else
bufferWithKeyValue

{ buffer: bufferWithSuffix, fieldsLeft: fieldsLeft - 1 }

bytesHead = List.append bytes (Num.toU8 '{')
{ buffer: bytesWithRecord } = List.walk fields { buffer: bytesHead, fieldsLeft: List.len fields } writeRecord
Expand All @@ -273,16 +285,21 @@ encodeRecord = \fields ->
encodeTuple = \elems ->
Encode.custom \bytes, @Json {} ->
writeTuple = \{ buffer, elemsLeft }, elemEncoder ->
bufferWithElem =
appendWith buffer elemEncoder (@Json {})
beforeBufferLen = buffer |> List.len

bufferWithSuffix =
if elemsLeft > 1 then
List.append bufferWithElem (Num.toU8 ',')
else
bufferWithElem
bufferWithElem = appendWith buffer (elemEncoder) (@Json {})

# If our encoder returned [] we just skip the elem
if bufferWithElem |> List.len == beforeBufferLen then
{ buffer: bufferWithElem, elemsLeft: elemsLeft - 1 }
else
bufferWithSuffix =
if elemsLeft > 1 then
List.append bufferWithElem (Num.toU8 ',')
else
bufferWithElem

{ buffer: bufferWithSuffix, elemsLeft: elemsLeft - 1 }
{ buffer: bufferWithSuffix, elemsLeft: elemsLeft - 1 }

bytesHead = List.append bytes (Num.toU8 '[')
{ buffer: bytesWithRecord } = List.walk elems { buffer: bytesHead, elemsLeft: List.len elems } writeTuple
Expand Down Expand Up @@ -1273,7 +1290,7 @@ decodeRecord = \initialState, stepField, finalizer -> Decode.custom \bytes, @Jso
rest = List.dropFirst bytesAfterValue n

# Build final record from decoded fields and values
when finalizer updatedRecord is
when finalizer updatedRecord json is
Ok val -> { result: Ok val, rest }
Err e -> { result: Err e, rest }

Expand Down
Loading