Created
March 6, 2019 08:02
-
-
Save jinjor/0b7498cc0fc4b03c952682dfb617ff45 to your computer and use it in GitHub Desktop.
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
{-| | |
import Json.Decode exposing (..) | |
import Json.Decode.Pipeline exposing (..) | |
type FooOrBar | |
= Foo String | |
| Bar Int | |
oneOf | |
[ succeed Foo | |
|> keyword "type" "foo" | |
|> required "foo_value" string | |
, succeed Bar | |
|> keyword "type" "bar" | |
|> required "bar_value" int | |
] | |
-} | |
keyword : String -> String -> Decoder a -> Decoder a | |
keyword key value decoder = | |
field key string | |
|> andThen | |
(\v -> | |
if v == value then | |
decoder | |
else | |
fail | |
("expected " | |
++ key | |
++ " to be " | |
++ value | |
++ " but got " | |
++ v | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment