Skip to content
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

Improving support for polymorphic data types #61

Merged
merged 2 commits into from
Oct 31, 2019

Conversation

MikePors
Copy link

Hi!

First off: I'm a huge fan of this library.

In a recent project I encountered a problem where the generated API code for an endpoint which returned an (Either [String] Identifier) was being compiled to:

, expect = jsonDec(Either (List String) Identifier)
which fails to compile. This pull request changes the output in this case to:

, expect = Http.expectJson toMsg ((jsonDecEither (Json.Decode.list (Json.Decode.string))) jsonDecIdentifier)

which matches with the generated decoder.

I built the repo referenced in #58 both as-is and using the changes from this repo and the compilation issue there seems to have gone away.

@@ -522,6 +522,8 @@ renderDecoderName elmTypeExpr =
parens ("Json.Decode.list " <> parens (renderDecoderName t))
ETyApp (ETyCon (ETCon "Maybe")) t ->
parens ("Json.Decode.maybe " <> parens (renderDecoderName t))
ETyApp x y ->
parens (renderDecoderName x <+> renderDecoderName y)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously my use case would have been handled by the default case below (which renders spaces and parentheses). This change will render the decoder for x and will then render the decoder for y as an input to the decoder for x.

Example: If x ~ "Either" and y ~ "List String" then x -> jsonDecEither and y -> (Json.Decode.list Json.string) so ETyApp x y -> (jsonDecEither (Json.Decode.list Json.Decode.string).

"jsonDecPolymorphicData : Json.Decode.Decoder a -> Json.Decode.Decoder b -> Json.Decode.Decoder (PolymorphicData a b)\n"<>
"jsonDecPolymorphicData _ _ = Debug.todo \"finish\"\n\n" <>
"jsonDecSomeRecord : Json.Decode.Decoder SomeRecord\n"<>
"jsonDecSomeRecord = Debug.todo \"finish\"\n\n\n")]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spoofing the parameterized decoders which would be generated by elm-bridge.

, body =
Http.emptyBody
, expect =
Http.expectJson toMsg ((jsonDecPolymorphicData (Json.Decode.list (Json.Decode.string))) jsonDecSomeRecord)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

red -> green test

@k-bx
Copy link
Collaborator

k-bx commented Oct 31, 2019

This is fantastic! Thank you for the well-made PR.

@k-bx k-bx merged commit 3faa94c into haskell-servant:master Oct 31, 2019
@k-bx
Copy link
Collaborator

k-bx commented Oct 31, 2019

Released as 0.7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants