Revert "Show that putting the decoder in a different module works"

This reverts commit 7f1a850bef12aa9f55dbbed34030c8a07f82d2a4.
This commit is contained in:
Ayaz Hafiz 2022-08-12 15:55:59 -07:00
parent 8b80f10e56
commit 6a0abdd32d
No known key found for this signature in database
GPG Key ID: 0E2A37416A25EF58
2 changed files with 40 additions and 47 deletions

View File

@ -1,45 +0,0 @@
interface Inter
exposes [
theDecoder
]
imports [
Decode
]
theDecoder =
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

View File

@ -1,10 +1,48 @@
app "helloWorld"
packages { pf: "platform/main.roc" }
imports [Inter, Decode, Json]
imports [Decode, Decode.{Decoder, Decoding, DecoderFormatting}, Json]
provides [main] to pf
theDecoder =
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 Inter.theDecoder Json.fromUtf8 is
when Str.toUtf8 "{\"first\":\"ab\",\"second\":[\"cd\",\"ef\"]}" |> Decode.decodeWith theDecoder Json.fromUtf8 is
{result, rest: _} ->
when result is
Ok { first, second } -> Str.concat first (Str.joinWith second ",")