Skip to content

Instantly share code, notes, and snippets.

@MarkNijhof
Forked from michie1/Foo.elm
Created December 11, 2016 21:41
Show Gist options
  • Save MarkNijhof/e00c9d772999477162a7cfc7349ba8ed to your computer and use it in GitHub Desktop.
Save MarkNijhof/e00c9d772999477162a7cfc7349ba8ed to your computer and use it in GitHub Desktop.
Decode Maybe type
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