feature/issue-132-map-prime
- Adds Decode.map'
- Adds various tests
This is looking good.
Would you also like to do the Encode side of this PR?
This is looking good.
Would you also like to do the
Encodeside of this PR?
I'm away for a week or so but happy to tackle it when I get back.
How should it look?
m
|> Map.map (fun _ v -> valueEncoder v)
|> Encode.map' keyEncoder
m |> Map.map (fun _ v -> valueEncoder v) |> Encode.map' keyEncoder
Hum, yes we will need to map the value and the key using their respective encoders.
However, here you are using the decoder you are trying to define Encode.map' so it will not work.
According to my test something like that should do the job:
let map'
(keyEncoder : Encoder<'Key>)
(valueEncoder : Encoder<'Value>)
(value : Map<'Key, 'Value>) =
value
|> Map.toArray
|> Array.map (Encode.tuple2 keyEncoder valueEncoder)
|> Encode.array
let x =
Map.ofList [
1, "maxime"
2, "njlr"
]
let json =
Encode.toString
4
(map' Encode.int Encode.string x)
JS.console.log(json)
// [
// [
// 1,
// "maxime"
// ],
// [
// 2,
// "njlr"
// ]
// ]
I have added Encode.map as you described above.
I'm worried the design is inconsistent though... Encode.list takes a JsonValue list and Encode.dict takes a Map<string, Value>. However, Encode.map takes two encoders and a map before encoding.
To mirror Encode.list and Encode.dict, perhaps Encode.map should take the key encoder and a Map<'key, JsonValue>?
Good point indeed.
My only problem is that because JsonValue is an alias for obj the compiler is really really tolerant about what we pass in the functions. I god bitten a few time because of that.
That's probably why I wrote this version of the encoder.
The situation should be improved in future version of Thoth.Json if I change it to type JsonValue = interface end or something like. So int or string would stop being valid by default.
Good point indeed.
My only problem is that because
JsonValueis an alias forobjthe compiler is really really tolerant about what we pass in the functions. I god bitten a few time because of that.That's probably why I wrote this version of the encoder.
The situation should be improved in future version of Thoth.Json if I change it to
type JsonValue = interface endor something like. Sointorstringwould stop being valid by default.
Makes sense!
Let me know what is required to move this one forward.
Let me know what is required to move this one forward.
Nothing more is needed from your side, I just need to review your PR and test it.