Mapail v0.2.1 Maptu.Extension
Custom functions absent from Maptu
and superfluous to Maptu requirements but required for Mapail to function properly.
This module builds on top of maptu.ex
and contains extracts from maptu.ex
,
thereby, extending the available functionality. Where possible, the original Maptu
functions are called.
Acknowldegements:
Credit to the Maptu Creators:
- [Andrea Leopardi](https://github.com/whatyouhide)
- [Aleksei Magusev](https://github.com/lexmag)
Maptu License:
- MIT
- https://github.com/lexhide/maptu/blob/master/LICENSE.txt
Summary
Functions
Behaves like Maptu.Extension.struct_rest/1
but returns the residual rest
map rather
than the struct
and raises in case of error
Behaves like Maptu.Extension.struct_rest/2
but returns the residual rest
map rather than the struct
and raises in case of error
Converts a map to a struct, silently capturing residual key => value
pairs into a map with keys in the String.t
format
Builds the mod
struct with the given fields
, silently capturing residual key => value
pairs into a map with keys in the String.t
format
Types
non_strict_error_reason :: :missing_struct_key | {:bad_module_name, binary} | {:non_existing_module, binary} | {:non_struct, module}
strict_error_reason :: non_strict_error_reason | {:non_existing_atom, binary} | {:non_existing_module, binary} | {:unknown_struct_field, module, atom}
Functions
Behaves like Maptu.Extension.struct_rest/1
but returns the residual rest
map rather
than the struct
and raises in case of error.
This function behaves like Maptu.Extension.struct_rest/1
, but it returns the rest
map (instead
of {:ok, struct, rest}
) if the conversion is valid, and raises an ArgumentError
exception if it’s not valid.
Examples
iex> Maptu.Extension.rest!(%{"__struct__" => "Elixir.URI", "port" => 8080})
%{}
iex> Maptu.Extension.rest!(%{"__struct__" => "Elixir.URI", "port" => 8080, "foo" => 1})
%{"foo" => 1}
iex> Maptu.Extension.rest!(%{"__struct__" => "Elixir.GenServer"})
** (ArgumentError) module is not a struct: GenServer
Behaves like Maptu.Extension.struct_rest/2
but returns the residual rest
map rather than the struct
and raises in case of error.
This function behaves like Maptu.Extension.struct_rest/2
, but it returns the rest
map (instead
of {:ok, struct, rest}
) if the conversion is valid, and raises an ArgumentError
exception if it’s not valid.
Examples
iex> Maptu.Extension.rest!(URI, %{"port" => 8080, "nonexisting_field" => 1})
%{"nonexisting_field" => 1}
iex> Maptu.Extension.rest!(GenServer, %{})
** (ArgumentError) module is not a struct: GenServer
Converts a map to a struct, silently capturing residual key => value
pairs into a map with keys in the String.t
format.
map
is a map with binary keys that represents a “dumped” struct; it must
contain a "__struct__"
key with a binary value that can be converted to a
valid module name. If the value of "__struct__"
is not a module name or it’s
a module that isn’t a struct, then an error is returned.
Keys in map
that are not fields of the resulting struct are are collected along with their
respective values into a separate map denoted by rest
.
This function returns {:ok, struct, rest}
if the conversion is successful,
{:error, reason}
otherwise.
Examples
iex> Maptu.Extension.struct_rest(%{"__struct__" => "Elixir.URI", "port" => 8080, "foo" => 1})
{:ok, %URI{port: 8080}, %{"foo" => 1}}
iex> Maptu.Extension.struct_rest(%{"__struct__" => "Elixir.GenServer"})
{:error, {:non_struct, GenServer}}
struct_rest(module, %{}) :: {:ok, %{}, %{}} | {:error, non_strict_error_reason}
Builds the mod
struct with the given fields
, silently capturing residual key => value
pairs into a map with keys in the String.t
format.
This function takes a struct mod
(mod
should be a module that defines a
struct) and a map of fields with binary keys. It builds the mod
struct by
safely parsing the fields in fields
.
If a key in fields
doesn’t map to a field in the resulting struct, the key and it’s
respective value are collected into a separate map denoted by rest
.
This function returns {:ok, struct, rest}
if the building is successful,
{:error, reason}
otherwise.
Examples
iex> Maptu.Extension.struct_rest(URI, %{"port" => 8080, "nonexisting_field" => 1})
{:ok, %URI{port: 8080}, %{"nonexisting_field" => 1}}
iex> Maptu.Extension.struct_rest(GenServer, %{})
{:error, {:non_struct, GenServer}}