Skip to content

Mason uses superpowers to coerce maps into structs. This is helpful e.g. when you interface a REST API and want to create a struct from the response.

License

Notifications You must be signed in to change notification settings

spacepilots/mason

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mason Build Status

Mason uses superpowers to coerce maps into structs. This is helpful e.g. when you interface a REST API and want to create a struct from the response.

defmodule User do
  defstruct [ :user_id, :created_at ]

  def masonstruct do
    %{
      createdAt: &({ :created_at, elem(DateTime.from_unix(&1), 1) }),
      userId: &({ :user_id, &1 })
    }
  end
end

response = %{ "userId" => 123, createdAt: 1456749030 }
Mason.struct User, response # %User{created_at: #DateTime<2016-02-29 12:30:30Z>, user_id: 123}

Features

  • coerce to Elixir's built-in types from strings
  • dynamically coerce values (e.g. timestamps to DateTimes)
  • map keys (e.g. createdAt to created_at)
  • convert string keys to atoms (e.g. ..., "age" => 29, ... to ..., age: 29, ...)
  • convert lists

Installation

The package can be installed by adding mason to your list of dependencies in mix.exs:

def deps do
  [
    {:mason, "~> 0.1.0"}
  ]
end

The docs can be found at https://hexdocs.pm/mason.

Contributing

About

Mason uses superpowers to coerce maps into structs. This is helpful e.g. when you interface a REST API and want to create a struct from the response.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages