-
Notifications
You must be signed in to change notification settings - Fork 48
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <- | ||
|
@@ -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")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. red -> green test |
||
, timeout = | ||
Nothing | ||
, tracker = | ||
Nothing | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).