Created
March 29, 2017 12:04
-
-
Save ggb/222385d8a2b734616a7cfaaa83b2f580 to your computer and use it in GitHub Desktop.
How to decode union types in Elm
This file contains hidden or 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 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