MapTransform (map_transform v0.1.1)
This is a simple library that can transform one map into another through mapping rules.
Link to this section Summary
Functions
Transform one map into another.
Link to this section Types
Link to this type
mapping()
Specs
Link to this type
path()
Specs
path() :: [term(), ...]
Link to this section Functions
Link to this function
transform(source, mapping)
Specs
Transform one map into another.
Format for mappings
For the paths is the standard that get_in/2
and put_in/2
use.
For example to get data from c
in %{a: %{b: %{c: 1}}}
you would provide
[:a, :b, :c]
as the path.
Then we'll use these paths in the mapping in a list of tuples where:
{from_path, to_path}
{from_path, to_path, &transform_function/1}
Example
Basic
iex> mapping = [
...> {[:a, :b, :c], [:abc]}
...> ]
...> source = %{a: %{b: %{c: 1}}}
...> transform(source, mapping)
%{abc: 1}
String keys and using a transform function
iex> mapping = [
...> {["a", "b", "c"], [:abc], &String.to_integer/1}
...> ]
...> source = %{"a" => %{"b" => %{"c" => "1"}}}
...> transform(source, mapping)
%{abc: 1}
Any nesting
iex> mapping = [
...> {[:a, :b, :c], [:foo, :bar]}
...> ]
...> source = %{a: %{b: %{c: 1}}}
...> transform(source, mapping)
%{foo: %{bar: 1}}