qcam / maptu

Tiny library to convert from "encoded" maps to Elixir structs.

Home Page:http://hexdocs.pm/maptu

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maptu

Build Status Hex.pm

Maptu is a small Elixir library that provides functions to convert from "encoded" maps to Elixir structs.

"Encoded" maps are maps/structs that have been encoded through some protocol (like MessagePack or JSON) decoded back from that protocol. In the case of structs, the information about the struct is lost, usually like this:

%URI{port: 8080} |> encode() |> decode()
#=> %{"__struct__" => "Elixir.URI", "port" => 8080}

Maptu's job is to get that map with string keys back to an Elixir struct in a safe way (to avoid memory leaks coming from mindlessly converting string keys to atoms):

%URI{port: 8080} |> encode() |> decode() |> Maptu.struct!()
#=> %URI{port: 8080}

Credit

Most of the design and implementation ideas in this library come from the awesome @lexmag ❤️

Installation

Add :maptu to your list of dependencies in mix.exs:

def deps do
  [{:maptu, "~> 1.0"}]
end

and be sure to add :maptu to your list of started applications:

def application do
  [applications: [:maptu]]
end

Documentation is available on Hex.

License

MIT © 2016 Andrea Leopardi, Aleksei Magusev (license file)

About

Tiny library to convert from "encoded" maps to Elixir structs.

http://hexdocs.pm/maptu

License:MIT License


Languages

Language:Elixir 100.0%