Last active
December 11, 2016 21:41
-
-
Save michie1/2736af940a87641d9184554c69aaa305 to your computer and use it in GitHub Desktop.
Decode Maybe type
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
module Foo exposing (..) | |
import Json.Decode exposing (..) | |
import Json.Decode.Pipeline exposing (..) | |
type alias Boat = | |
{ color : Maybe Color | |
} | |
type Color | |
= Red | |
| Blue | |
colorDecoder : String -> Decoder Color | |
colorDecoder str = | |
case str of | |
"red" -> | |
succeed Red | |
"blue" -> | |
succeed Blue | |
_ -> | |
fail (str ++ " color does not exists.") | |
boatDecoder : Decoder Boat | |
boatDecoder = | |
decode Boat | |
|> required "color" | |
(nullable | |
(string |> andThen colorDecoder) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment