git.iko.soy/haskell-servant/servant-elm
on master help sign in
Adds support for properly building decoders for polymorphic data types
commita98cff5a0bdda73aa7b2cdbc77e942ad59069e35 author= <=> dateWed 30 Oct 2019, 15:58 (7 years ago) parent11acdd4
4 files changed+82−5
src/Servant/Elm/Internal/Generate.hs+4−2 test/Common.hs+8 test/GenerateSpec.hs+27−3 test/elm-sources/getPolymorphicData.elm+43
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/Common.hs+8
@@ -15,7 +15,15 @@ data Book = Book
1515 { title :: String
1616 }
1717
18+data PolymorphicData a b = PolymorphicData a b deriving (Show, Eq)
19+data SomeRecord = SomeRecord
20+ { recordId :: Int
21+ , recordName :: String
22+ } deriving (Show, Eq)
23+
1824deriveBoth defaultOptions ''Book
25+deriveBoth defaultOptions ''PolymorphicData
26+deriveBoth defaultOptions ''SomeRecord
1927
2028type TestApi =
2129 "one"
test/GenerateSpec.hs+27−3
@@ -17,14 +17,14 @@ import Servant.Elm
1717import Test.Hspec (Spec, describe, hspec, it)
1818import Test.HUnit (Assertion, assertEqual)
1919
20−import Common (testApi)
20+import Common (testApi, SomeRecord(..), PolymorphicData(..))
2121
2222
2323main :: IO ()
2424main = hspec spec
2525
2626spec :: Test.Hspec.Spec
27−spec = do
27+spec =
2828 describe "encoding a simple api" $
2929 do it "does it" $
3030 do expected <-
@@ -122,6 +122,30 @@ spec = do
122122 })
123123 (Proxy :: Proxy ("one" :> Get '[JSON] Int)))
124124 generated `itemsShouldBe` expected
125+ it "works with polymorphic data" $
126+ do expected <-
127+ mapM
128+ (\(fpath, header) -> do
129+ source <- T.readFile fpath
130+ return (fpath, header, source))
131+ [ ( "test/elm-sources/getPolymorphicData.elm"
132+ , "module GetPolymorphicData exposing (..)\n\n" <>
133+ "import Http\n" <>
134+ "import Json.Decode exposing (..)\n" <>
135+ "import Url.Builder\n\n" <>
136+ "type PolymorphicData a b = PolymorphicData a b\n" <>
137+ "type SomeRecord = SomeRecord { recordId : Int, recordname : String }\n\n" <>
138+ "jsonDecPolymorphicData : Json.Decode.Decoder a -> Json.Decode.Decoder b -> Json.Decode.Decoder (PolymorphicData a b)\n"<>
139+ "jsonDecPolymorphicData _ _ = Debug.todo \"finish\"\n\n" <>
140+ "jsonDecSomeRecord : Json.Decode.Decoder SomeRecord\n"<>
141+ "jsonDecSomeRecord = Debug.todo \"finish\"\n\n\n")]
142+ let generated =
143+ map
144+ (<> "\n")
145+ (generateElmForAPIWith
146+ defElmOptions
147+ (Proxy :: Proxy ( "polymorphicData" :> Get '[JSON] (PolymorphicData [String] SomeRecord))))
148+ generated `itemsShouldBe` expected
125149
126150itemsShouldBe :: [Text] -> [(String, Text, Text)] -> IO ()
127151itemsShouldBe actual expected =
@@ -138,7 +162,7 @@ shouldBeDiff a (fpath,header,b) =
138162 (Diff.getGroupedDiff
139163 (lines (T.unpack actual))
140164 (lines (T.unpack expected))))
141− actual expected
165+ expected actual
142166 where
143167 actual = T.strip $ header <> a
144168 expected = T.strip b
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+ }