Starchoice (starchoice v0.1.1) View Source
Starchoice takes his name from the satellite tv company (now called Shaw Direct) because they are selling TV decoders. Since this lib is used to declare map decoders, I thought it felt appropriate to be named that way. Maybe not. Anyway.
The goal of the library is to provide a streamline process for convertir String keyed maps to well defined structures. It is highly inspired by Elm's JSON decoders where you create different JSON decoders for the same data type.
For more information about creating decoder, visit the Starchoice.Decoder
module documentation.
Link to this section Summary
Functions
Decodes a map into a {:ok, _} | {:error | _}
tuple format. It accepts the same options and parameters as decode!/3
Decodes a map into according to the given decoder. A decoder can either be a %Starchoice.Decoder{}
, a one or two arity function that returns a decoder or a module implementing Starchoice.Decoder
.
Link to this section Types
Link to this section Functions
Specs
decode(any(), decoder(), [decode_option()]) :: {:ok, any()} | {:error, any()}
Decodes a map into a {:ok, _} | {:error | _}
tuple format. It accepts the same options and parameters as decode!/3
Specs
decode!(any(), decoder(), [decode_option()]) :: any()
Decodes a map into according to the given decoder. A decoder can either be a %Starchoice.Decoder{}
, a one or two arity function that returns a decoder or a module implementing Starchoice.Decoder
.
See module Starchoice.Decoder
for more information about decoders.
Examples
iex> decoder = %Decoder{struct: User, fields: [first_name: [], age: [with: &String.to_integer/1]]}
iex> Decoder.decode!(%{"first_name" => "Bobby Hill", "age" => "13"}, decoder)
%User{first_name: "Bobby Hill", age: 13}
If the module User
implements Starchoice.Decoder
, it can be used directly
iex> Decoder.decode!(%{"first_name" => "Bobby Hill", "age" => "13"}, User)
iex> Decoder.decode!(%{"first_name" => "Bobby Hill", "age" => "13"}, {User, :default}) # same as above