StructConstructor allows you to declare structs using Ecto.Schema and generate constructor functions that will take care of coercion and handling various input formats (maps, keyword lists with string or atom keys).
Documentation can be found at https://hexdocs.pm/struct_constructor.
Add struct_constructor to your list of dependencies in mix.exs:
def deps do
[
{:struct_constructor, "~> 0.2.0"}
]
endDefine a typed structure:
defmodule User do
use StructConstructor do
field :name, :string
field :age, :integer
end
endInitialize your structure with external input:
User.new(%{"name" => "Alex", "age" => "27"})
# => %User{age: 27, name: "Alex"}Note, that :age attribute was automatically converted to integer.
MIT License, Copyright (c) 2017-2018 SaleMove