rescript-labs/decco

Support Js.Dict.t

ryb73 opened this issue · 3 comments

ryb73 commented
Support Js.Dict.t

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?