Skip to content

Commit

Permalink
Merge pull request #61 from MikePors/master
Browse files Browse the repository at this point in the history
Improving support for polymorphic data types
  • Loading branch information
k-bx authored Oct 31, 2019
2 parents 11acdd4 + 0365af3 commit 3faa94c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
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)
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")]
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)
, timeout =
Nothing
, tracker =
Nothing
}

0 comments on commit 3faa94c

Please sign in to comment.