XmlBuilder.Access v0.1.0 XmlBuilder.Access View Source

Provides a function-based Access implementation. This allows to get an access to deeply nested elemetns via Kernel.get_in/2, Kernel.pop_in/2, Kernel.put_in/3, Kernel.update_in/3, and Kernel.get_and_update_in/3.

Example:

iex> get_in({:person, %{id: 1}, [{:data, %{}, [{:name, %{}, "John"}]}]},
...>   [XmlBuilder.Access.key(:data), XmlBuilder.Access.key(:name), XmlBuilder.Access.key()])
"John"

iex> update_in({:persons, %{}, [{:name, %{}, "John"}, {:name, %{}, "Jane"}]},
...>   [XmlBuilder.Access.key({:name, -1}), XmlBuilder.Access.key()], fn _ -> "Mary" end)
{:persons, %{}, [{:name, %{}, "John"}, {:name, %{}, "Mary"}]}

Negative indices are supported, -1 for the last element, -2 for next to the last etc.

Link to this section Summary

Types

Nested elements of any node are in general accessible by {name, index} tuple. When a single name atom passed as an argument, the implementation assumes index zero.

Functions

Default Access function implementation accepting default values.

Link to this section Types

Link to this type

maybe_ordered_key() View Source
maybe_ordered_key() :: nil | atom() | {atom(), integer()}

Nested elements of any node are in general accessible by {name, index} tuple. When a single name atom passed as an argument, the implementation assumes index zero.

Link to this section Functions

Link to this function

key(key \\ nil) View Source
key(key :: maybe_ordered_key()) ::
  Access.access_fun(
    data :: {atom(), map(), list() | any()},
    get_value :: term()
  )

Default Access function implementation accepting default values.

Examples:

iex> # simple value
iex> get_in({:person, %{id: 1}, 42}, [XmlBuilder.Access.key()])
42
iex> put_in({:person, %{id: 1}, nil}, [XmlBuilder.Access.key()], 42)
{:person, %{id: 1}, 42}
iex> update_in({:person, %{id: 1}, nil},
...>   [XmlBuilder.Access.key()], fn _ -> 42 end)
{:person, %{id: 1}, 42}
iex> get_and_update_in({:person, %{id: 1}, nil},
...>   [XmlBuilder.Access.key()], fn old -> {old, 42} end)
{nil, {:person, %{id: 1}, 42}}

iex> # nested element
iex> get_in({:person, %{id: 1}, [{:name, %{}, "John"}]},
...>   [XmlBuilder.Access.key(:name)])
{:name, %{}, "John"}
iex> put_in({:person, %{id: 1}, [{:name, %{}, "John"}]},
...>   [XmlBuilder.Access.key(:name)], "Mary")
{:person, %{id: 1}, [{:name, %{}, "Mary"}]}
iex> update_in({:person, %{id: 1}, [{:name, %{}, "John"}]},
...>   [XmlBuilder.Access.key(:name)], fn _ -> "Mary" end)
{:person, %{id: 1}, [{:name, %{}, "Mary"}]}
iex> get_and_update_in({:person, %{id: 1}, [{:name, %{}, "John"}]},
...>   [XmlBuilder.Access.key(:name)], fn {_, _, old} -> {old, "Mary"} end)
{"John", {:person, %{id: 1}, [{:name, %{}, "Mary"}]}}