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

时间:2017-10-01 17:56:46

标签: elm

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

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

错误:

Decode.map (List JsonLink)
     

无法找到变量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

1 个答案:

答案 0 :(得分:3)

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

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

另请注意,Cannot find variable List错误是因为List是一种类型,而不是构造函数/函数。