-
Notifications
You must be signed in to change notification settings - Fork 17
Add JPropCodec variants of sum codecs
#81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
Nice idea, i see the purpose! Just one thing to consider: Doesn't this imply that |
|
Or maybe one day there's an encoding that encodes constructors without args just as strings. |
|
Yeah, those are good points. Maybe there's something that could be done for this case that could be constrained to If I can't come up with anything for that I might just merge this as it is for now, as I definitely have this use case (and have also had it in the past) and haven't personally encountered cases for those other encodings yet. 😉 |
go for it :) |
|
@garyb Any news on this topic? I think after some time I am getting more and more convinced that it may be better to stay with the initial I see that the Think about encodings that need to be optimized for size. Maybe you want to encode as Strings or Ints to save much space as possible. data Foo = Bar String | Baz Int So If you have an (Array Foo) it makes a difference on large data: (of course that's not possible with what we have today, but maybe in the future...) So I think to settle on Records is a bit arbitrary. Maybe there is a way to handle your case with a wrapper using some unsafe internal implementation? |
|
I thought about something like this. I had case where I need JSON object codec for type JsonObjectCodec a = Codec' (Either JsonDecodeError) (Object Json) a
To get this I have to go This approach also allows to safely implement typed tags mapping as a separate module/function, something like this: mapTags ∷
∀ r' rep a. GTagsMap r' rep ⇒ Generic a rep ⇒ Record r' → JPropCodec a -> JPropCodec a
mapTags tagsMap codec =
Codec.codec
(\obj → (Codec.decode codec =<< foldTags obj))
(mapKeys <<< Codec.encode codec)
where
tagsObj = gTagsMap (Proxy @rep) tagsMap
fromTagsMap s = fromMaybe s (FO.lookup s tagsObj)
mapKeys = map (lmap fromTagsMap)
foldTags obj = FO.fromFoldable <$> for (FO.toUnfoldable tagsObj :: List _)
\(Tuple tag mappedTag) ->
maybe (Left MissingValue) (Right <<< Tuple tag) (FO.lookup mappedTag obj)
So I'm not sure if such restriction is a bad thing. Anyway, you can transform JPropCodec to anything you like. |
@m-bock Right before I made the release as per #80 I ran into a case where I found myself wanting a
JPropCodecfor a sum type so I could combine it with another codec for a surrounding type:This gives us
Tuple "foo" (Str "BAR")⇔{ "name": "foo", "type": "str", "value": "BAR" }. The motivating case was a bit more involved, so this looks like a lot of work for something of questionable necessity, but hopefully it illustrates the idea still! Basically, without this, we can't deal with complex sums and would have to match our PS representation to the flat structure of the JSON object (aside from introducing further types and doing a conversion step or something like that).(Also, isn't it kinda cool the way
codecVarworked out? 😄)