A simple Id type for your types that have ids.
The only way to make an Id
Id.fromString "vq93rUv0A4"Encode an Id
Encode.encode 0 (Id.encode id)
-- ""hDFL0Cs2EqWJ4jc3kMtOrKdEUTWh"" : String
[ ("id", Id.encode id) ]
|> Encode.object
|> Encode.encode 0
-- {\"id\":\"hDFL0Cs2EqWJ4jc3kMtOrKdEUTWh\"} : StringDecode an Id
Decode.decodeString (Decode.field "id" Id.decoder) "{\"id\":\"19\"}"
-- Ok (Id 19) : Result String IdA way to generate random Ids
import Id exposing (Id)
import Random.Pcg as Random exposing (Seed)
user : Seed -> ( User, Seed )
user seed =
let
( id, nextSeed ) =
Random.step Id.generator seed
in
( { id = id, email = "[email protected]" }
, nextSeed
)Data that comes from a remote source usually has an id, while data that is made locally doesnt. If thats your situation, using this Origin type might be a better way of representing your datas id.
type alias Car =
{ origin : Origin
, make : String
}
saveCar : Car -> Http.Request (Result Err ())
saveCar car =
case car.origin of
Id id ->
Car.update id car
New ->
Car.create carA dictionary that uses Id as keys
Insert a new value into an Id.Dict
Make a special Dict that uses Id as keys
type alias User =
{ id : Id
, name : String
}
Id.toDict .id users : Dict UserGet just the values of a Id.Dict