mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 20:28:02 +00:00
Add the tuple
ability member to EncoderFormatting
This commit is contained in:
parent
8a7d9f8f23
commit
a361c4cfc1
3 changed files with 28 additions and 5 deletions
|
@ -22,6 +22,7 @@ interface Encode
|
|||
list,
|
||||
record,
|
||||
tag,
|
||||
tuple,
|
||||
custom,
|
||||
appendWith,
|
||||
append,
|
||||
|
@ -69,6 +70,7 @@ EncoderFormatting has
|
|||
string : Str -> Encoder fmt | fmt has EncoderFormatting
|
||||
list : List elem, (elem -> Encoder fmt) -> Encoder fmt | fmt has EncoderFormatting
|
||||
record : List { key : Str, value : Encoder fmt } -> Encoder fmt | fmt has EncoderFormatting
|
||||
tuple : List (Encoder fmt) -> Encoder fmt | fmt has EncoderFormatting
|
||||
tag : Str, List (Encoder fmt) -> Encoder fmt | fmt has EncoderFormatting
|
||||
|
||||
custom : (List U8, fmt -> List U8) -> Encoder fmt | fmt has EncoderFormatting
|
||||
|
|
|
@ -96,6 +96,7 @@ Json := {} has [
|
|||
string: encodeString,
|
||||
list: encodeList,
|
||||
record: encodeRecord,
|
||||
tuple: encodeTuple,
|
||||
tag: encodeTag,
|
||||
},
|
||||
DecoderFormatting {
|
||||
|
@ -207,6 +208,25 @@ encodeRecord = \fields ->
|
|||
|
||||
List.append bytesWithRecord (Num.toU8 '}')
|
||||
|
||||
encodeTuple = \elems ->
|
||||
Encode.custom \bytes, @Json {} ->
|
||||
writeTuple = \{ buffer, elemsLeft }, elemEncoder ->
|
||||
bufferWithElem =
|
||||
appendWith buffer elemEncoder (@Json {})
|
||||
|
||||
bufferWithSuffix =
|
||||
if elemsLeft > 1 then
|
||||
List.append bufferWithElem (Num.toU8 ',')
|
||||
else
|
||||
bufferWithElem
|
||||
|
||||
{ buffer: bufferWithSuffix, elemsLeft: elemsLeft - 1 }
|
||||
|
||||
bytesHead = List.append bytes (Num.toU8 '[')
|
||||
{ buffer: bytesWithRecord } = List.walk elems { buffer: bytesHead, elemsLeft: List.len elems } writeTuple
|
||||
|
||||
List.append bytesWithRecord (Num.toU8 ']')
|
||||
|
||||
encodeTag = \name, payload ->
|
||||
Encode.custom \bytes, @Json {} ->
|
||||
# Idea: encode `A v1 v2` as `{"A": [v1, v2]}`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue