Я понимаю
Я не верю, что первое актуально для elm 0.19. Мне не удалось найти функцию decode
в NoRedInk /elm-json-decode-pipeline, и я не верю, что инфиксный оператор :=
все еще действителен.
Второй касается немного другого вопроса условного декодирования на основе значения поля JSON.
Если у меня есть данные, поступающие через порт, и следующие типы:
импортировать Json.Decode, раскрывающий (декодер, карта, oneOf, строка, успех и затем, карта2, карта4)
import Json.Decode exposing (Decoder, andThen, map, map2, map4, oneOf, string, succeed, field)
port loginResponse : (Value -> msg) -> Sub msg
type Status
= Authenticated Data
| Unauthenticated (Maybe Error)
type alias Data =
{ accessToken : String
, email : String
, name : String
, nickname : String
}
dataDecoder : Decoder Data
dataDecoder =
map4 Data
(field "accessToken" string)
(field "email" string)
(field "name" string)
(field "nickname" string)
type alias Error =
{ error : String
, errorDescription : String
}
errorDecoder : Decoder Error
errorDecoder =
map2 Error
(field "error" string)
(field "errorDescription" string)
Как я могу написать декодер для помеченного типа объединения Status
для декодирования данных, возвращающихся из порта?
Лучшее, что я получил до сих пор, это что-то вроде
statusDecoder : Decoder Status
statusDecoder =
oneOf
[ dataDecoder andThen (\data -> succeed (Authenticated data)
, errorDecoder andThen (\error -> succeed (Unauthenticated (Just error)))
]
что недействительно или
getStatus : Json.Decode.Value -> Status
getStatus value =
let
decodedData =
decodeValue dataDecoder value
in
case decodedData of
Ok data ->
Authenticated data
Err _ ->
let
decodedError =
decodeValue errorDecoder value
in
case decodedError of
Ok error ->
Unauthenticated (Just error)
Err err ->
Unauthenticated (Just { error = "There was a problem decoding the JSON", errorDescription = "" })
что действительно уродливо и не кажется правильным.