-
-
Save alicebob/84083c48310cdb984fbe493d50aac905 to your computer and use it in GitHub Desktop.
elm decoder error
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
import Html exposing (..) | |
import Json.Decode exposing (Decoder, list, string) | |
import Json.Decode | |
type Elem | |
= Para (List Elem) | |
| Chunk String | |
elemListDecoder : Decoder (List Elem) | |
elemListDecoder = | |
Json.Decode.list elemDecoder | |
elemDecoder : Decoder Elem | |
elemDecoder = | |
Json.Decode.oneOf [ elemDecoderChunk, elemDecoderType ] | |
elemDecoderChunk : Decoder Elem | |
elemDecoderChunk = | |
Json.Decode.map Chunk string | |
elemDecoderType : Decoder Elem | |
elemDecoderType = | |
Json.Decode.field "type" string | |
|> Json.Decode.andThen elemDecoderTypeHelp | |
elemDecoderTypeHelp : String -> Decoder Elem | |
elemDecoderTypeHelp t = | |
case t of | |
"p" -> | |
-- meta: using elemDecoder directly doesn't work somehow | |
Json.Decode.map Para (Json.Decode.field "content" (Json.Decode.list elemDecoder)) | |
-- Json.Decode.map Para (Json.Decode.field "content" (Json.Decode.list (Json.Decode.oneOf [ elemDecoderChunk, elemDecoderType]))) | |
_ -> Json.Decode.fail <| "Can't do a " ++ t ++ " elem" | |
--- skeleton from here on | |
main = | |
Html.beginnerProgram { model = model, view = view, update = update } | |
model : Model | |
model = | |
let js = "[\"foo\", {\"type\": \"p\", \"content\": []}]" | |
in | |
case Json.Decode.decodeString elemListDecoder js of | |
Ok d -> { doc = d } | |
Err err -> Debug.crash err | |
-- MODEL | |
type alias Model = { doc : List Elem } | |
-- UPDATE | |
type Msg = Reset | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
Reset -> model | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
Html.text "all fine" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment