Mapáil 


Convert maps with string keys to an elixir struct.
Features
String keyed maps: Convert maps with string keys to a corresponding struct.
Transformations: Optionally, string manipulations can be applied to the string of the map so as to attempt to force the key to match the key of the struct. Currently, the only transformation option is conversion to snake_case.
Residual maps: Optionally, the part of the map leftover after the struct has been built can be retrieved or merged back into the returned struct.
Examples
Example (1)
-
Converting a map using the default settings which include coercions of keys
into the
snake_caseformat and discarding any unmatching key-value pairs.
user = %{
"FirstName" => "John",
"Username" => "john",
"password" => "pass",
"age" => 30
}
defmodule User, do: defstruct [:first_name, :username, :password]
Mapail.map_to_struct(user, User)
{:ok, %User{
first_name: "John",
username: "john",
password: "pass"
}
}Example (2)
- Converting a map without applying any transformations so that only exact matches on key-value pairs will be used and unmatching key-value pairs will be discarded.
user = %{
"FirstName" => "John",
"Username" => "john",
"password" => "pass",
"age" => 30
}
defmodule User, do: defstruct [:first_name, :username, :password]
Mapail.map_to_struct(user, User, transformations: [])
{:ok, %User{
first_name: nil,
password: "pass",
username: nil
}
}Example (3)
- Converting a map without applying any transformations so that only exact matches on key-value pairs will be used and unmatching key-value pairs will be returned as a separate map.
user = %{
"FirstName" => "John",
"Username" => "john",
"password" => "pass",
"age" => 30
}
defmodule User, do: defstruct [:first_name, :username, :password]
Mapail.map_to_struct(user, User, transformations: [], rest: :true)
{:ok, %User{
first_name: nil,
password: "pass",
username: nil
},
%{
"FirstName" => "John",
"Username" => "john",
"age" => 30
}
}Example (4)
-
Converting a map using the default settings which include coercions of keys
into the
snake_caseformat and unmatching key-value pairs will be merged back into the original map under the:mapailkey.
user = %{
"FirstName" => "John",
"Username" => "john",
"password" => "pass",
"age" => 30
}
defmodule User, do: defstruct [:first_name, :username, :password]
Mapail.map_to_struct(user, User, rest: :merge)
{:ok, %{
__struct__: User,
first_name: "John",
password: "pass",
username: "john",
mapail: %{
"FirstName" => "John",
"Username" => "john",
"age" => 30
}
}
}
Example (5)
-
Converting a map without applying any transformations so that only exact matches
on key-value pairs will be used and unmatching key-value pairs will be merged back into
the original map under the
:mapailkey.
user = %{
"FirstName" => "John",
"Username" => "john",
"password" => "pass",
"age" => 30
}
defmodule User, do: defstruct [:first_name, :username, :password]
Mapail.map_to_struct(user, User, transformations: [], rest: :merge)
{:ok, %{
__struct__: User,
first_name: nil,
password: "pass",
username: nil,
mapail: %{
"FirstName" => "John",
"Username" => "john",
"age" => 30
},
}
}Documentation
- Documentation can be found in the hex package manager.
Installation
The package can be installed as follows:
-
Add mapail to your list of dependencies in
mix.exs:
def deps do
[{:mapail, "~> 0.2"}]
end- Ensure mapail is started before your application:
def application do
[applications: [:mapail]]
endThe package can be found in hex.
Credit
This library has a dependency on the following libraries:
- Maptu v1.0.0 library. For converting a matching map to a struct. MIT © 2016 Andrea Leopardi, Aleksei Magusev. Licence