elm-bridge icon indicating copy to clipboard operation
elm-bridge copied to clipboard

Apply the `constructorTagModifier` to the JSON but not the type def?

Open jhickner opened this issue 9 years ago • 3 comments

I have an existing type like this in my Haskell app:

data FooType
  = FooTypeEnum { fooTypeValues :: [Text] }
  ...

With JSON serialization defined which outputs:

{"enum": { "values": ["value1", "value2", ...]}}

Which is achieved by using the fieldLabelModifier and constructorTagModifier options for Aeson.

I'm using the analogous option values in elm-bridge to get the json encoding/decoding to match up, but the issue is that constructorTagModifier applies to both the type definition AND the json encoders/decoders, where I want it to apply to only the latter.

So I end up with the correct json encoding/decoding, but on the Elm side the type definition has the same modifications applied:

type FooType =
  Enum { values: (List String) }
  ...

Which causes issues, especially with constructors like FooTypeInt, which ends up being defined as just Int.

Is there a way to apply constructorTagModifer only to the json encoding/decoding?

jhickner avatar Apr 11 '16 23:04 jhickner

Right, this is currently not possible due to the fact that deriveElmDef generates Elm code that includes the JSON parser/serializer. It sounds like a good change though, would you like to give this a shot with a PR?

There's another bug: What should we do when elm-bridge generates an Elm type or record label that is reserved (like type or Int)? I've created a separate issue: https://github.com/agrafix/elm-bridge/issues/8

agrafix avatar Apr 12 '16 08:04 agrafix

@agrafix What's your preferred approach - add more fields to the existing options object, or make separate options objects for the type defs and the JSON parser/serializers?

jhickner avatar Apr 12 '16 18:04 jhickner

We are currently piggybacking on the aeson Options object, so I think we must wrap it to achieve this level of configuration.

agrafix avatar Apr 13 '16 15:04 agrafix