garyb / purescript-codec-argonaut

Bi-directional JSON codecs for argonaut
MIT License
39 stars 16 forks source link

Codec for homogeneous record #64

Open wclr opened 2 weeks ago

wclr commented 2 weeks ago

This question is not directly related to this library, but PS row-type "magic" capabilities.

Is it possible to code a codec maker for homogeneous record smth like below?

homoRecCodec :: ∀ @r a. Homogeneous r a => JsonCodec a -> JsonCodec (Record r)

Considering all the records' fields use the same codec, so there would be no need to code this record of codecs manually.

garyb commented 2 weeks ago

That definitely seems like it should be possible, yeah. I was going to suggest looking at Foreign.Object.fromHomogeneous, but that's implemented as unsafeCoerce 🤣. The code for the Homogeneous constraint it uses might be a useful starting place though: https://github.com/purescript/purescript-typelevel-prelude/blob/v7.0.0/src/Type/Row/Homogeneous.purs.