Support Js.Dict.t
ryb73 opened this issue · 3 comments
ryb73 commented
Support Js.Dict.t
mrmurphy commented
In the meantime users can add their own Dict support by doing something like this:
In a file like MyCodecs.re
:
module Dict = {
type _t('a) = Js.Dict.t('a);
let encoder = (innerEnc: Decco.encoder('a), dct) => {
let jsonDict = dct->Js.Dict.map((. a) => innerEnc(a), _);
Json.Encode.dict(jsonDict);
};
let decoder = (innerDec, json) => {
let unsafeInner = unsafe(innerDec);
let dictDecoder = Json.Decode.dict(unsafeInner);
fromLegacy(dictDecoder, json);
};
let codec = (encoder, decoder);
[@decco]
type t('a) = [@decco.codec codec] _t('a);
};
And then in a record type, refer to that the instead of Dict:
type blah = {
yar: MyCodecs.Dict.t(string)
}
Note that I was lazy in this snippet and used Json.Decode
inside from @glennsl/bs-json
. But it can be written without that.
ryb73 commented
Nice. I'm unfamiliar with unsafe
and fromLegacy
?
mrmurphy commented
Oh whoops! I should have read that better before posting it. Those are functions from within my own codebase.
…On Dec 5, 2019, 9:48 PM -0700, Ryan ***@***.***>, wrote:
Nice. I'm unfamiliar with unsafe and fromLegacy?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.