Make Sum type encoder more flexible
- Dominant language
- Haskell
- Stars
- 1.3k
- Forks
- 336
- Avg merge
- 3d 7h
- Merged PRs (30d)
- 4
Description
Hi,
I just noticed a mismatch in enconding conventions between Argounaut (PureScript) and Aeson.
Argounaut always wraps content into sub object with "content" key, when sum type is serialized, mean while Aeson does that only for constructors without named slots! So I have to drop names in Haskell source code and define lenses manually!
There are some tickets about Sum type encoder and I think this one won't be the last.
Sum encoder should be more configurable.
I agree PureScript encoding might be less compact, but PureScript community is smaller.
I would add following data constructor to `SumEncoding`:
```
| SnowFlakeTag (Text -> Value -> Value)
```
Then original encoding logic for `TaggedObject` might be expressed like that:
```
originalLogic :: Text -> Value -> Value
originalLogic dcName (Object encodedContent) =
Object $ insertWith keepOldAsItWouldBeOverwritten "tag" (String dcName) encodedContent
where
keepOldAsItWouldBeOverwritten _new old = old
```
and Argonaut case:
```
argonautLogic :: Text -> Value -> Value
argonautLogic dcName encodedContent =
Object $ fromList [ ("tag", String dcName), ("content", encodedContent) ]
```
Contributor guide
Research direction
Start by tracing SumEncoding and the existing TaggedObject encoding logic in aeson. Compare that behavior with the proposed SnowFlakeTag and Argonaut functions; done means custom sum encoding can support both layouts while preserving the existing TaggedObject behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- haskell
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100