Skip to content

Commit

Permalink
Merge pull request #16 from WhileTruu/fix-nested-partial-record-extra…
Browse files Browse the repository at this point in the history
…-field-last

fix decoding nested partial record when extra field is last
  • Loading branch information
lukewilliamboswell authored Feb 18, 2024
2 parents d99270f + a2b2c6d commit 2410aaa
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion package/Core.roc
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ decodeRecord = \initialState, stepField, finalizer -> Decode.custom \bytes, @Jso
skipFieldHelp : SkipValueState, U8 -> [Break SkipValueState, Continue SkipValueState]
skipFieldHelp = \state, byte ->
when (state, byte) is
(FieldValue n, b) if b == '}' -> Continue (FieldValueEnd n)
(FieldValue n, b) if b == '}' -> Break (FieldValueEnd n)
(FieldValue n, b) if b == '[' -> Continue (InsideAnArray { index: (n + 1), nesting: 0 })
(FieldValue n, b) if b == '{' -> Continue (InsideAnObject { index: (n + 1), nesting: 0 })
(FieldValue n, b) if b == '"' -> Continue (InsideAString (n + 1))
Expand Down Expand Up @@ -1510,6 +1510,39 @@ expect
result = actual.result
result == expected

# Test decode of partial record in list additional field last
expect
input = Str.toUtf8 "[{\"ownerName\": \"Farmer Joe\", \"extraField\":2}]"
actual : DecodeResult (List { ownerName : Str })
actual = Decode.fromBytesPartial input json

expected = Ok [{ ownerName: "Farmer Joe" }]

result = actual.result
result == expected

# Test decode of partial record in record partial field last
expect
input = Str.toUtf8 "{\"value\": {\"ownerName\": \"Farmer Joe\",\"extraField\":2}}"
actual : DecodeResult { value : { ownerName : Str } }
actual = Decode.fromBytesPartial input json

expected = Ok { value: { ownerName: "Farmer Joe" } }

result = actual.result
result == expected

# Test decode of partial record in partial record additional fields last
expect
input = Str.toUtf8 "{\"value\": {\"ownerName\": \"Farmer Joe\", \"extraField\":2}, \"extraField\":2}"
actual : DecodeResult { value : { ownerName : Str } }
actual = Decode.fromBytesPartial input json

expected = Ok { value: { ownerName: "Farmer Joe" } }

result = actual.result
result == expected

# Test decode of partial record with multiple additional fields
expect
input = Str.toUtf8 "{\"extraField\":2, \"ownerName\": \"Farmer Joe\", \"extraField2\":2 }"
Expand Down

0 comments on commit 2410aaa

Please sign in to comment.