MessageStore.MapExtra (message_store v3.1.0) View Source

An extra functions for Map data type.

Link to this section Summary

Functions

Fetches the value for a specific path in the given map.

Fetches the value for a specific path in the given map, erroring out if map doesn't contain path.

Link to this section Functions

Specs

fetch_in(map(), [Map.key()]) :: {:ok, Map.value()} | :error

Fetches the value for a specific path in the given map.

If map contains the given path then its value is returned in the shape of {:ok, value}. If map doesn't contain path, :error is returned.

Examples

iex> a = %{a: 1, b: %{c: 2}}
iex> MapExtra.fetch_in(a, [:a])
{:ok, 1}

iex> a = %{a: 1, b: %{c: 2}}
iex> MapExtra.fetch_in(a, [:b, :c])
{:ok, 2}

iex> a = %{a: 1, b: %{c: 2}}
iex> MapExtra.fetch_in(a, [:b, :d])
:error

Specs

fetch_in!(map(), [Map.key()]) :: Map.value()

Fetches the value for a specific path in the given map, erroring out if map doesn't contain path.

If map contains path, the corresponding value is returned. If map doesn't contain path, a PathError exception is raised.

Examples

iex> a = %{a: 1, b: %{c: 2}}
iex> MapExtra.fetch_in!(a, [:a])
1