你的问题的快速解决方法是替换
D.succeed ProviderWithVal
D.map ProviderWithVal (D.field "value" Decode.string)
但我会创建一个帮助器来匹配目标字符串,然后按以下方式使用它:
decoder =
Decode.oneOf [ decodeWithVal, decodeP2, decodeP3 ]
decodeWithVal =
exactMatch (Decode.field "name" Decode.string)
"providerWithVal"
(Decode.map ProviderWithVal <| Decode.field "value" Decode.string)
decodeP2 =
exactMatch (Decode.field "name" Decode.string) "provider2" (Decode.succeed Provider2)
decodeP3 =
exactMatch (Decode.field "name" Decode.string) "provider3" (Decode.succeed Provider3)
exactMatch : Decoder String -> String -> Decoder a -> Decoder a
exactMatch matchDecoder match dec =
matchDecoder
|> Decode.andThen
(\str ->
if str == match then
dec
else
Decode.fail <| "[exactMatch] tgt: " ++ match ++ " /= " ++ str
)