Thoth.Json icon indicating copy to clipboard operation
Thoth.Json copied to clipboard

feature/issue-132-map-prime

Open njlr opened this issue 3 years ago • 7 comments

  • Adds Decode.map'
  • Adds various tests

njlr avatar Apr 06 '22 22:04 njlr

This is looking good.

Would you also like to do the Encode side of this PR?

MangelMaxime avatar Apr 08 '22 07:04 MangelMaxime

This is looking good.

Would you also like to do the Encode side 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 

njlr avatar Apr 08 '22 07:04 njlr

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"
//     ]
// ]

MangelMaxime avatar Apr 08 '22 15:04 MangelMaxime

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>?

njlr avatar Apr 16 '22 20:04 njlr

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.

MangelMaxime avatar Apr 25 '22 12:04 MangelMaxime

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.

Makes sense!

Let me know what is required to move this one forward.

njlr avatar May 10 '22 16:05 njlr

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.

MangelMaxime avatar May 17 '22 07:05 MangelMaxime