Skip to content

Instantly share code, notes, and snippets.

@jinjor
Created March 6, 2019 08:02
Show Gist options
  • Save jinjor/0b7498cc0fc4b03c952682dfb617ff45 to your computer and use it in GitHub Desktop.
Save jinjor/0b7498cc0fc4b03c952682dfb617ff45 to your computer and use it in GitHub Desktop.
{-|
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