Mapáil Build StatusHex VersionHex docs

Convert maps with string keys to an elixir struct.

Features

Example

defmodule User do
  defstruct [:first_name, :username, :password]
end

As seen below the map does not exactly match the struct keys due to CamelCasing in this instance. Mapáil will attempt to match with the struct keys by converting the unmatched keys to snake_case.

user = %{
        "FirstName" => "John",
        "Username" => "john",
        "password" => "pass",
        "age": 30
        }

Mapail.map_to_struct(user, MapailTest.User)

{:ok, %User{
           first_name: "John",
           username: "john",
           password: "pass"
           }
}

If the same conversion is attempted with transformations turned off and rest turned on, the keys would not match and the leftover map can optionally be returned separately.

Mapail.map_to_struct(user, MapailTest.User, transformations: [], rest: :true)

{:ok, %User{
           first_name: :nil,
           username: :nil,
           password: "pass"
           },
      %{
        "FirstName" => "John",
        "Username" => "john",
        "age" => 30
        }
}

Documentation

Installation

The package can be installed as follows:

  1. Add mapail to your list of dependencies in mix.exs:
def deps do
  [{:mapail, "~> 0.2"}]
end
  1. Ensure mapail is started before your application:
def application do
  [applications: [:mapail]]
end

The package can be found in hex.

Credit

This library has a dependency on the following libraries:

Licence

MIT Licence