snowhite v0.2.2 Snowhite.Helpers.Decoder View Source
Link to this section Summary
Functions
Makes a structure decodable easily. This comes in handy to decode json structure from apis.
Link to this section Functions
Specs
Makes a structure decodable easily. This comes in handy to decode json structure from apis.
It makes any string-keyed map decodable as a struct. If you want to transform a value before adding it to the map, just override decode_field/2
function and pattern match on the key (first parameter).
Examples
In the examples below, you'll notice that the
defmodule User do
@keys :name, :email, :posts
defstruct @keys
use Snowhite.Helpers.Decoder, @keys
def decode_field(:posts, posts), do: Enum.map(posts, &Post.decode/1)
def decode_field(:email, email), do: String.downcase(email)
def decode_field(_, value), do: value # required as we are overriding the function
end
defmodule Post do
@keys :title, :body
defstruct @keys
end
iex> User.decode(%{"name" => "Bobby Hill", "email" => "BobbyHill@kingofthehill.com", "posts" => [%{"title" => "Being bobby", "body" => "..."}]})
%User{
name: "Bobby Hill",
email: "bobbyhill@kingofthehill.com",
posts: [
%Post{title: "Being Bobby", body: "..."}
]
}