Skip to content

Instantly share code, notes, and snippets.

@ggb
Created March 29, 2017 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggb/222385d8a2b734616a7cfaaa83b2f580 to your computer and use it in GitHub Desktop.
Save ggb/222385d8a2b734616a7cfaaa83b2f580 to your computer and use it in GitHub Desktop.
How to decode union types in Elm
import Html exposing (text)
import Json.Decode exposing (..)
type Status
= Done
| Open
| Failed
type alias Stuff =
{ f: Status }
statusDecoder =
let
statusHelper s =
case s of
"Done" -> Done
"Open" -> Open
_ -> Failed
in
map statusHelper string
stuffDecoder =
map Stuff
(field "f" statusDecoder)
main =
"{\"f\": \"Done\"}"
|> decodeString stuffDecoder
|> toString
|> text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment