mlms13 / bs-decode

Type-safe JSON decoding for ReasonML and OCaml
https://mlms13.github.io/bs-decode/docs/what-and-why
MIT License
103 stars 18 forks source link

Add Map decoder #50

Closed mlms13 closed 5 years ago

mlms13 commented 5 years ago

For simple Belt.Map types with string keys, we can reuse the dict decoder and convert pretty easily, but it would also be nice to take a compare function and a key decoder and return a Map with whatever key type you want (e.g. a Map where the keys are dates).

mlms13 commented 5 years ago

Naming crisis averted: we can call the decode function stringMap to avoid the collision with the map: (a -> b) -> f a -> f b function. Seems like we should be able to implement this using something like:

let stringMap = decodeInner =>
  dict(decodeInner)
  |> map(Js.Dict.entries)
  |> map(Array.foldLeft(
    (acc, (key, val)) =>
      Map.String.set(key, val, acc),
      Map.String.empty
    );