Thanks for your fast reply !
I have some issues with Js.Dict.t
and the uncurried mode. I’m trying to upgrade the code based on this coupled with @spice
.
For reference, here is the implementation :
module MakeString = () => {
module Id: {
@spice
type t
module Dict: {
type key = t
@spice
type t<'a>
let get: (t<'a>, key) => option<'a>
@set_index
external set: (t<'a>, key, 'a) => unit = ""
@val
external keys: t<'a> => array<string> = "Object.keys"
@obj /** Returns an empty dictionary. */
external empty: unit => t<'a> = ""
let unsafeDeleteKey: (t<string>, string) => unit
let entries: t<'a> => array<(key, 'a)>
let values: t<'a> => array<'a>
let fromList: list<(key, 'a)> => t<'a>
let fromArray: array<(key, 'a)> => t<'a>
let map: ('a => 'b, t<'a>) => t<'b>
let deleteKey: (t<'a>, key) => t<'a>
}
} = {
@spice
type t = string
module Dict = {
include Js.Dict
let deleteKey: (t<'a>, string) => t<'a> = %raw("function (dict, key) {
const newDict = Object.assign({},dict);
delete newDict[key];
return newDict;
}")
let t_encode = (encoder, dict): Js.Json.t => Js.Json.Object(
Js.Dict.map(a => encoder(a), dict),
)
let t_decode = (decoder, json) => {
open Spice
switch (json: Js.Json.t) {
| Js.Json.Object(dict) =>
dict
->Js.Dict.entries
->Belt.Array.reduce(Ok(Js.Dict.empty()), (acc, (key, value)) =>
switch (acc, decoder(value)) {
| (Error(_), _) => acc
| (_, Error({path} as error)) => Error({...error, path: "." ++ (key ++ path)})
| (Ok(prev), Ok(newVal)) =>
let () = prev->Js.Dict.set(key, newVal)
Ok(prev)
}
)
| _ => Error({path: "", message: "Not a dict", value: json})
}
}
}
}
@spice
type t = Id.t
module Dict = Id.Dict
external make: string => t = "%identity"
external toString: t => string = "%identity"
}
The compiler complains about signature missmatch for the t_encode
function
Signature mismatch:
...
In module Dict:
Values do not match:
let t_encode: ('a => Js.Json.t, Js.Dict.t<'a>) => Js.Json.t
is not included in
let t_encode: ('a => Js.Json.t) => t<'a> => Js.Json.t
How could I handle this case ? I tried the dot notation but without success
Thanks again !