An Id
type that leverages the type system (thanks to phantom types)
to ensure that it refers to the right resource.
For example, if your application handles users with integer-based IDs, and articles with string-based IDs, you could define them as such:
type User
= User
{ id : UserID
, name : String
}
type Article
= Article
{ id : ArticleID
, title : String
}
type alias UserID =
Id String User
type alias ArticleID =
Id Int Article
The Id a resource
type handles IDs whose representation are of type a
that refers to resources of type resource
.
from : a -> Id a resource
Make an Id
from its representation.
userID : Id String User
userID =
Id.from "e4edf8a"
articleID : Id Int Article
articleID =
Id.from 5
to : Id a resource -> a
Extract the raw representation from an Id
.
Id.to userID == "e4edf8a"
Id.to articleID == 5
encode : (a -> Json.Encode.Value) -> Id a resource -> Json.Encode.Value
Encode an Id
.
encodeUserID : Id String User -> Value
encodeUserID =
Id.encode Json.Encode.string
encodeArticleID : Id Int Article -> Value
encodeArticleID =
Id.encode Json.Encode.int
decoder : Json.Decode.Decoder a -> Json.Decode.Decoder (Id a resource)
Decode an Id
.
userIDDecoder : Decoder (Id String User)
userIDDecoder =
Id.decoder Json.Decode.string
articleIDDecoder : Decoder (Id Int Article)
articleIDDecoder =
Id.decoder Json.Decode.int