Cartograf
Struct-to-Struct Mapping Utility for Elixir
This Project is still under development, and may not be safe for production.
Documentation can be found at https://hexdocs.pm/cartograf.
Building Mapping Functions
defmodule A, do: defstruct [:a, :b, :cc, :d]
defmodule B, do: defstruct [:a, :b, :c, :d, :e]
defmodule YourModule do
use Cartograf
map A, B, :a_to_b do
[
# No need to directly map identical keys, i.e.
# let(:a, :a)
let(:cc, :c)
]
end
# auto mapping can be turned off
map A, B, :a_to_b, auto: false do
...
end
map B, A, :b_to_a do
[
# Keys that shouldn't be mapped can be dropped
drop(:e)
]
end
endInvoking Mapping Functions
iex> YourModule.a_to_b(%A{a: 1, b: 2, c: 3, d: 4})
%B{a: 1, b: 2, c: 3, d: 4, e: nil}List of Forms
For more information, please look at the docs and tests.
map(from_module, to_module, name_of_fn, opts) do ...
Setup a new mapping functionlet(from_symbol, to_symbol)
Set field to field mappingdrop(from_symbol)
Prevent a source field from being mappedconst(to_symbol, value)
Provide a constant value for a destination fieldnest(nested_module, dest_key) do ...
Specify a nested struct type, and a destination field for it.
Installation
The package can be installed
by adding cartograf to your list of dependencies in mix.exs:
def deps do
[
{:cartograf, "~> 0.1.0"}
# or
{:cartograf, git: "https://github.com/Herlitzd/cartograf.git"}
]
end