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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions servant-elm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ test-suite servant-elm-test
hs-source-dirs: test
main-is: GenerateSpec.hs
other-modules: Common
, PolymorphicData
build-depends:
Diff
, HUnit
Expand Down
6 changes: 4 additions & 2 deletions src/Servant/Elm/Internal/Generate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ mkLetParams opts request =
, "Just"
]
)

where
elmName = elmQueryArg qarg
name = qarg ^. F.queryArgName . F.argName . to (stext . F.unPathSegment)
Expand Down Expand Up @@ -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).

ETyCon (ETCon "Int") -> "Json.Decode.int"
ETyCon (ETCon "String") -> "Json.Decode.string"
_ -> ("jsonDec" <> stext (T.pack (renderElm elmTypeExpr)))
Expand Down Expand Up @@ -549,7 +551,7 @@ mkUrl opts segments =
dquotes (stext (F.unPathSegment path))
F.Cap arg ->
let
toStringSrc =
toStringSrc =
toString opts (maybeOf (arg ^. F.argType))
in
pipeRight [elmCaptureArg s, toStringSrc]
Expand Down
29 changes: 27 additions & 2 deletions test/GenerateSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import Test.Hspec (Spec, describe, hspec, it)
import Test.HUnit (Assertion, assertEqual)

import Common (testApi)
import PolymorphicData (SomeRecord(..), PolymorphicData(..))


main :: IO ()
main = hspec spec

spec :: Test.Hspec.Spec
spec = do
spec =
describe "encoding a simple api" $
do it "does it" $
do expected <-
Expand Down Expand Up @@ -122,6 +123,30 @@ spec = do
})
(Proxy :: Proxy ("one" :> Get '[JSON] Int)))
generated `itemsShouldBe` expected
it "works with polymorphic data" $
do expected <-
mapM
(\(fpath, header) -> do
source <- T.readFile fpath
return (fpath, header, source))
[ ( "test/elm-sources/getPolymorphicData.elm"
, "module GetPolymorphicData exposing (..)\n\n" <>
"import Http\n" <>
"import Json.Decode exposing (..)\n" <>
"import Url.Builder\n\n" <>
"type PolymorphicData a b = PolymorphicData a b\n" <>
"type SomeRecord = SomeRecord { recordId : Int, recordname : String }\n\n" <>
"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.

let generated =
map
(<> "\n")
(generateElmForAPIWith
defElmOptions
(Proxy :: Proxy ( "polymorphicData" :> Get '[JSON] (PolymorphicData [String] SomeRecord))))
generated `itemsShouldBe` expected

itemsShouldBe :: [Text] -> [(String, Text, Text)] -> IO ()
itemsShouldBe actual expected =
Expand All @@ -138,7 +163,7 @@ shouldBeDiff a (fpath,header,b) =
(Diff.getGroupedDiff
(lines (T.unpack actual))
(lines (T.unpack expected))))
actual expected
expected actual
where
actual = T.strip $ header <> a
expected = T.strip b
15 changes: 15 additions & 0 deletions test/PolymorphicData.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{-# LANGUAGE TemplateHaskell #-}

module PolymorphicData where

import Servant.Elm


data PolymorphicData a b = PolymorphicData a b deriving (Show, Eq)
data SomeRecord = SomeRecord
{ recordId :: Int
, recordName :: String
} deriving (Show, Eq)

deriveBoth defaultOptions ''PolymorphicData
deriveBoth defaultOptions ''SomeRecord
43 changes: 43 additions & 0 deletions test/elm-sources/getPolymorphicData.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module GetPolymorphicData exposing (..)

import Http
import Json.Decode exposing (..)
import Url.Builder

type PolymorphicData a b = PolymorphicData a b
type SomeRecord = SomeRecord { recordId : Int, recordname : String }

jsonDecPolymorphicData : Json.Decode.Decoder a -> Json.Decode.Decoder b -> Json.Decode.Decoder (PolymorphicData a b)
jsonDecPolymorphicData _ _ = Debug.todo "finish"

jsonDecSomeRecord : Json.Decode.Decoder SomeRecord
jsonDecSomeRecord = Debug.todo "finish"


getPolymorphicData : (Result Http.Error ((PolymorphicData (List String) SomeRecord)) -> msg) -> Cmd msg
getPolymorphicData toMsg =
let
params =
List.filterMap identity
(List.concat
[])
in
Http.request
{ method =
"GET"
, headers =
[]
, url =
Url.Builder.crossOrigin ""
[ "polymorphicData"
]
params
, 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

, timeout =
Nothing
, tracker =
Nothing
}