榆树冒号等于运算符
Elm Colon Equals Operator
在尝试解码更大的 json 值时,我已将 运行 放入 Json-Decode-Extra
库中的以下代码中。 (位于here)
import Date (Date)
type alias User =
{ id : Int
, createdAt : Date
, updatedAt : Date
, deletedAt : Maybe Date
, username : Maybe String
, email : Maybe String
, fullname : Maybe String
, avatar : Maybe String
, isModerator : Bool
, isOrganization : Bool
, isAdmin : Bool
}
metaDecoder : (Int -> Date -> Date -> Maybe Date -> b) -> Decoder b
metaDecoder f = f
`map` ("id" := int)
`apply` ("createdAt" := date)
`apply` ("updatedAt" := date)
`apply` ("deletedAt" := maybe date)
userDecoder : Decoder User
userDecoder = metaDecoder User
`apply` ("username" := maybe string)
`apply` ("email" := maybe string)
`apply` ("fullname" := maybe string)
`apply` ("avatar" := maybe string)
`apply` ("isModerator" := bool)
`apply` ("isOrganization" := bool)
`apply` ("isAdmin" := bool)
但是,我经常 运行 遇到 :=
运算符的编译器错误。这是在哪里定义的? JSON 解码教程不会在任何地方显式导入此运算符。
在 Elm 0.18 中,:=
运算符被 Json.Decode.field
取代,并且删除了对中缀运算符使用反引号。
您正在使用尚未更新到 Elm 0.18 的软件包 (circuithub/elm-json-extra)。
考虑改用 Elm 社区维护的包:elm-community/json-extra
。您可以使用 andMap
而不是 apply
。这是升级到新库和 Elm 0.18 的示例代码:
import Date exposing (Date)
import Json.Decode exposing (..)
import Json.Decode.Extra exposing (andMap, date)
metaDecoder : (Int -> Date -> Date -> Maybe Date -> b) -> Decoder b
metaDecoder f =
succeed f
|> andMap (field "id" int)
|> andMap (field "createdAt" date)
|> andMap (field "updatedAt" date)
|> andMap (field "deletedAt" (maybe date))
userDecoder : Decoder User
userDecoder =
metaDecoder User
|> andMap (field "username" (maybe string))
|> andMap (field "email" (maybe string))
|> andMap (field "fullname" (maybe string))
|> andMap (field "avatar" (maybe string))
|> andMap (field "isModerator" bool)
|> andMap (field "isOrganization" bool)
|> andMap (field "isAdmin" bool)
请注意,elm-community/json-extra
包还导出一个中缀运算符 |:
,它是 andMap
的中缀版本。您可以使用它来使您的代码更简洁。例如:
metaDecoder : (Int -> Date -> Date -> Maybe Date -> b) -> Decoder b
metaDecoder f =
succeed f
|: field "id" int
|: field "createdAt" date
|: field "updatedAt" date
|: field "deletedAt" (maybe date)
在尝试解码更大的 json 值时,我已将 运行 放入 Json-Decode-Extra
库中的以下代码中。 (位于here)
import Date (Date)
type alias User =
{ id : Int
, createdAt : Date
, updatedAt : Date
, deletedAt : Maybe Date
, username : Maybe String
, email : Maybe String
, fullname : Maybe String
, avatar : Maybe String
, isModerator : Bool
, isOrganization : Bool
, isAdmin : Bool
}
metaDecoder : (Int -> Date -> Date -> Maybe Date -> b) -> Decoder b
metaDecoder f = f
`map` ("id" := int)
`apply` ("createdAt" := date)
`apply` ("updatedAt" := date)
`apply` ("deletedAt" := maybe date)
userDecoder : Decoder User
userDecoder = metaDecoder User
`apply` ("username" := maybe string)
`apply` ("email" := maybe string)
`apply` ("fullname" := maybe string)
`apply` ("avatar" := maybe string)
`apply` ("isModerator" := bool)
`apply` ("isOrganization" := bool)
`apply` ("isAdmin" := bool)
但是,我经常 运行 遇到 :=
运算符的编译器错误。这是在哪里定义的? JSON 解码教程不会在任何地方显式导入此运算符。
在 Elm 0.18 中,:=
运算符被 Json.Decode.field
取代,并且删除了对中缀运算符使用反引号。
您正在使用尚未更新到 Elm 0.18 的软件包 (circuithub/elm-json-extra)。
考虑改用 Elm 社区维护的包:elm-community/json-extra
。您可以使用 andMap
而不是 apply
。这是升级到新库和 Elm 0.18 的示例代码:
import Date exposing (Date)
import Json.Decode exposing (..)
import Json.Decode.Extra exposing (andMap, date)
metaDecoder : (Int -> Date -> Date -> Maybe Date -> b) -> Decoder b
metaDecoder f =
succeed f
|> andMap (field "id" int)
|> andMap (field "createdAt" date)
|> andMap (field "updatedAt" date)
|> andMap (field "deletedAt" (maybe date))
userDecoder : Decoder User
userDecoder =
metaDecoder User
|> andMap (field "username" (maybe string))
|> andMap (field "email" (maybe string))
|> andMap (field "fullname" (maybe string))
|> andMap (field "avatar" (maybe string))
|> andMap (field "isModerator" bool)
|> andMap (field "isOrganization" bool)
|> andMap (field "isAdmin" bool)
请注意,elm-community/json-extra
包还导出一个中缀运算符 |:
,它是 andMap
的中缀版本。您可以使用它来使您的代码更简洁。例如:
metaDecoder : (Int -> Date -> Date -> Maybe Date -> b) -> Decoder b
metaDecoder f =
succeed f
|: field "id" int
|: field "createdAt" date
|: field "updatedAt" date
|: field "deletedAt" (maybe date)