mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-10 10:02:38 +03:00
Check in the fully inlined version of record decoder
This commit is contained in:
parent
c34427bbf9
commit
fd52075a6f
@ -3,34 +3,43 @@ app "helloWorld"
|
||||
imports [Decode, Decode.{Decoder, Decoding, DecoderFormatting}, Json]
|
||||
provides [main] to pf
|
||||
|
||||
theDecoder : Decoder {first: a, second: b} fmt | a has Decoding, b has Decoding, fmt has DecoderFormatting
|
||||
theDecoder =
|
||||
initialState : {f0: Result a [NoField], f1: Result b [NoField]}
|
||||
initialState = {f0: Err NoField, f1: Err NoField}
|
||||
|
||||
stepField = \state, field ->
|
||||
when field is
|
||||
"first" ->
|
||||
Keep (Decode.custom \bytes, fmt ->
|
||||
when Decode.decodeWith bytes Decode.decoder fmt is
|
||||
{result, rest} ->
|
||||
{result: Result.map result \val -> {state & f0: Ok val}, rest})
|
||||
"second" ->
|
||||
Keep (Decode.custom \bytes, fmt ->
|
||||
when Decode.decodeWith bytes Decode.decoder fmt is
|
||||
{result, rest} ->
|
||||
{result: Result.map result \val -> {state & f1: Ok val}, rest})
|
||||
_ -> Skip
|
||||
|
||||
finalizer = \{f0, f1} ->
|
||||
when f0 is
|
||||
Ok first ->
|
||||
when f1 is
|
||||
Ok second -> Ok {first, second}
|
||||
Err NoField -> Err TooShort
|
||||
Err NoField -> Err TooShort
|
||||
|
||||
Decode.custom \bytes, fmt -> Decode.decodeWith bytes (Decode.record initialState stepField finalizer) fmt
|
||||
Decode.custom \bytes, fmt ->
|
||||
Decode.decodeWith
|
||||
bytes
|
||||
(Decode.record
|
||||
{f0: Err NoField, f1: Err NoField}
|
||||
(\state, field ->
|
||||
when field is
|
||||
"first" ->
|
||||
Keep (Decode.custom \bytes1, fmt1 ->
|
||||
when Decode.decodeWith bytes1 Decode.decoder fmt1 is
|
||||
rec ->
|
||||
{
|
||||
result: (when rec.result is
|
||||
Ok val -> Ok {state & f0: Ok val}
|
||||
Err e -> Err e),
|
||||
rest: rec.rest
|
||||
})
|
||||
"second" ->
|
||||
Keep (Decode.custom \bytes1, fmt1 ->
|
||||
when Decode.decodeWith bytes1 Decode.decoder fmt1 is
|
||||
rec ->
|
||||
{
|
||||
result: (when rec.result is
|
||||
Ok val -> Ok {state & f1: Ok val}
|
||||
Err e -> Err e),
|
||||
rest: rec.rest
|
||||
})
|
||||
_ -> Skip)
|
||||
(\stateRecord ->
|
||||
when stateRecord.f0 is
|
||||
Ok first ->
|
||||
when stateRecord.f1 is
|
||||
Ok second -> Ok {first, second}
|
||||
Err NoField -> Err TooShort
|
||||
Err NoField -> Err TooShort))
|
||||
fmt
|
||||
|
||||
main =
|
||||
when Str.toUtf8 "{\"first\":\"ab\",\"second\":[\"cd\",\"ef\"]}" |> Decode.decodeWith theDecoder Json.fromUtf8 is
|
||||
|
Loading…
Reference in New Issue
Block a user