elm - 如何编写解码器来映射自定义数据类型的值列表?

标签 elm

我正在努力为链接列表编写解码器:

listOfLinksDecoder : Decoder (List JsonLink)
listOfLinksDecoder =
    Decode.map (List JsonLink)
        (field "Links" <| Decode.list linkDecoder)

Error:

Decode.map (List JsonLink)

Cannot find variable List

请注意,我已经成功地为单个链接编写了解码器:

linkDecoder : Decoder JsonLink
linkDecoder =
    Decode.map6 JsonLink
        (field "Profile" profileDecoder)
        (field "Title" Decode.string)
        (field "Url" Decode.string)
        (field "ContentType" Decode.string)
        (field "Topics" <| Decode.list topicDecoder)
        (field "IsFeatured" Decode.bool)

请注意,我尝试搜索 this documentation 。但是,我仍然无法找到适合我的案例的示例。

附录:

topicLinks : Id -> Topic -> ContentType -> (Result Http.Error (List JsonLink) -> msg) -> Cmd msg
topicLinks providerId topic contentType msg =
    let
        url =
            baseUrl ++ (getId providerId) ++ "/" ++ "topiclinks"

        body =
            encodeId providerId |> Http.jsonBody

        request =
            Http.post url body linksDecoder
    in
        Http.send msg request

最佳答案

您不需要映射,您可以这样做:

listOfLinksDecoder : Decoder (List JsonLink)
listOfLinksDecoder =
    field "Links" <| Decode.list linkDecoder

另请注意,无法找到变量列表错误是因为List是一种类型,而不是构造函数/函数。

关于elm - 如何编写解码器来映射自定义数据类型的值列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46515227/

相关文章:

decode - 在 Elm 中解码可扩展记录

date - 该模块导入错误

elm - 强制重绘 img 元素

elm - 为什么我不能过滤订阅?

elm - 如何退出榆树REPL?

Elm 模态对话框

f# - 使用 Fable-Elmish 上传文件

onclick - 如何在 Elm 0.19 中监听全局鼠标事件?

html - Elm -- 将文本文件解析为 Html

elm - Elm 的余数运算符是什么