git.iko.soy/haskell-servant/servant-elm
on master help sign in
Merge pull request #61 from MikePors/master

Improving support for polymorphic data types

commit3faa94cfa7fdd9da93afc1cdab4895b1f782d573 authorKostiantyn Rybnikov <k-bx@k-bx.com> dateThu 31 Oct 2019, 10:46 (7 years ago) parent11acdd40365af3
5 files changed+90−4
servant-elm.cabal+1 src/Servant/Elm/Internal/Generate.hs+4−2 test/GenerateSpec.hs+27−2 test/PolymorphicData.hs+15 test/elm-sources/getPolymorphicData.elm+43
servant-elm.cabal+1
@@ -48,6 +48,7 @@ test-suite servant-elm-test
4848 hs-source-dirs: test
4949 main-is: GenerateSpec.hs
5050 other-modules: Common
51+ , PolymorphicData
5152 build-depends:
5253 Diff
5354 , HUnit
src/Servant/Elm/Internal/Generate.hs+4−2
@@ -420,7 +420,7 @@ mkLetParams opts request =
420420 , "Just"
421421 ]
422422 )
423−
423+
424424 where
425425 elmName = elmQueryArg qarg
426426 name = qarg ^. F.queryArgName . F.argName . to (stext . F.unPathSegment)
@@ -522,6 +522,8 @@ renderDecoderName elmTypeExpr =
522522 parens ("Json.Decode.list " <> parens (renderDecoderName t))
523523 ETyApp (ETyCon (ETCon "Maybe")) t ->
524524 parens ("Json.Decode.maybe " <> parens (renderDecoderName t))
525+ ETyApp x y ->
526+ parens (renderDecoderName x <+> renderDecoderName y)
525527 ETyCon (ETCon "Int") -> "Json.Decode.int"
526528 ETyCon (ETCon "String") -> "Json.Decode.string"
527529 _ -> ("jsonDec" <> stext (T.pack (renderElm elmTypeExpr)))
@@ -549,7 +551,7 @@ mkUrl opts segments =
549551 dquotes (stext (F.unPathSegment path))
550552 F.Cap arg ->
551553 let
552− toStringSrc =
554+ toStringSrc =
553555 toString opts (maybeOf (arg ^. F.argType))
554556 in
555557 pipeRight [elmCaptureArg s, toStringSrc]
test/GenerateSpec.hs+27−2
@@ -18,13 +18,14 @@ import Test.Hspec (Spec, describe, hspec, it)
1818import Test.HUnit (Assertion, assertEqual)
1919
2020import Common (testApi)
21+import PolymorphicData (SomeRecord(..), PolymorphicData(..))
2122
2223
2324main :: IO ()
2425main = hspec spec
2526
2627spec :: Test.Hspec.Spec
27−spec = do
28+spec =
2829 describe "encoding a simple api" $
2930 do it "does it" $
3031 do expected <-
@@ -122,6 +123,30 @@ spec = do
122123 })
123124 (Proxy :: Proxy ("one" :> Get '[JSON] Int)))
124125 generated `itemsShouldBe` expected
126+ it "works with polymorphic data" $
127+ do expected <-
128+ mapM
129+ (\(fpath, header) -> do
130+ source <- T.readFile fpath
131+ return (fpath, header, source))
132+ [ ( "test/elm-sources/getPolymorphicData.elm"
133+ , "module GetPolymorphicData exposing (..)\n\n" <>
134+ "import Http\n" <>
135+ "import Json.Decode exposing (..)\n" <>
136+ "import Url.Builder\n\n" <>
137+ "type PolymorphicData a b = PolymorphicData a b\n" <>
138+ "type SomeRecord = SomeRecord { recordId : Int, recordname : String }\n\n" <>
139+ "jsonDecPolymorphicData : Json.Decode.Decoder a -> Json.Decode.Decoder b -> Json.Decode.Decoder (PolymorphicData a b)\n"<>
140+ "jsonDecPolymorphicData _ _ = Debug.todo \"finish\"\n\n" <>
141+ "jsonDecSomeRecord : Json.Decode.Decoder SomeRecord\n"<>
142+ "jsonDecSomeRecord = Debug.todo \"finish\"\n\n\n")]
143+ let generated =
144+ map
145+ (<> "\n")
146+ (generateElmForAPIWith
147+ defElmOptions
148+ (Proxy :: Proxy ( "polymorphicData" :> Get '[JSON] (PolymorphicData [String] SomeRecord))))
149+ generated `itemsShouldBe` expected
125150
126151itemsShouldBe :: [Text] -> [(String, Text, Text)] -> IO ()
127152itemsShouldBe actual expected =
@@ -138,7 +163,7 @@ shouldBeDiff a (fpath,header,b) =
138163 (Diff.getGroupedDiff
139164 (lines (T.unpack actual))
140165 (lines (T.unpack expected))))
141− actual expected
166+ expected actual
142167 where
143168 actual = T.strip $ header <> a
144169 expected = T.strip b
test/PolymorphicData.hsnew file+15
@@ -0,0 +1,15 @@
1+{-# LANGUAGE TemplateHaskell #-}
2+
3+module PolymorphicData where
4+
5+import Servant.Elm
6+
7+
8+data PolymorphicData a b = PolymorphicData a b deriving (Show, Eq)
9+data SomeRecord = SomeRecord
10+ { recordId :: Int
11+ , recordName :: String
12+ } deriving (Show, Eq)
13+
14+deriveBoth defaultOptions ''PolymorphicData
15+deriveBoth defaultOptions ''SomeRecord
test/elm-sources/getPolymorphicData.elmnew file+43
@@ -0,0 +1,43 @@
1+module GetPolymorphicData exposing (..)
2+
3+import Http
4+import Json.Decode exposing (..)
5+import Url.Builder
6+
7+type PolymorphicData a b = PolymorphicData a b
8+type SomeRecord = SomeRecord { recordId : Int, recordname : String }
9+
10+jsonDecPolymorphicData : Json.Decode.Decoder a -> Json.Decode.Decoder b -> Json.Decode.Decoder (PolymorphicData a b)
11+jsonDecPolymorphicData _ _ = Debug.todo "finish"
12+
13+jsonDecSomeRecord : Json.Decode.Decoder SomeRecord
14+jsonDecSomeRecord = Debug.todo "finish"
15+
16+
17+getPolymorphicData : (Result Http.Error ((PolymorphicData (List String) SomeRecord)) -> msg) -> Cmd msg
18+getPolymorphicData toMsg =
19+ let
20+ params =
21+ List.filterMap identity
22+ (List.concat
23+ [])
24+ in
25+ Http.request
26+ { method =
27+ "GET"
28+ , headers =
29+ []
30+ , url =
31+ Url.Builder.crossOrigin ""
32+ [ "polymorphicData"
33+ ]
34+ params
35+ , body =
36+ Http.emptyBody
37+ , expect =
38+ Http.expectJson toMsg ((jsonDecPolymorphicData (Json.Decode.list (Json.Decode.string))) jsonDecSomeRecord)
39+ , timeout =
40+ Nothing
41+ , tracker =
42+ Nothing
43+ }