swiss v2.6.0 Swiss.Map View Source

A few extra functions to deal with Maps.

Link to this section Summary

Functions

Wrapper around Map.from_struct/1 that tolerates nil.

Fetches a value from a map with indifferent access, i.e. given an atom, returns the value that is keyed by that atom, or by its string equivalent.

Converts an atom-keyed map into a string-keyed map.

Link to this section Functions

Link to this function

from_struct(struct)

View Source
from_struct(struct() | nil) :: Map.t() | nil

Wrapper around Map.from_struct/1 that tolerates nil.

Examples

iex> Swiss.Map.from_struct(nil)
nil

iex> Swiss.Map.from_struct(%{__struct__: SomeStruct, life: 42})
%{life: 42}
Link to this function

indif_fetch!(map, key)

View Source
indif_fetch!(Map.t(), atom()) :: any()

Fetches a value from a map with indifferent access, i.e. given an atom, returns the value that is keyed by that atom, or by its string equivalent.

If both atom and String keys exist in the map, the atom's value is returned.

Examples

iex> Swiss.Map.indif_fetch!(%{life: 42}, :life)
42

iex> Swiss.Map.indif_fetch!(%{"life" => 42}, :life)
42

iex> Swiss.Map.indif_fetch!(%{:life => 42, "life" => 64}, :life)
42

iex> Swiss.Map.indif_fetch!(%{}, :life)
** (KeyError) key :life not found in: %{}
Link to this function

to_string_keys(map)

View Source
to_string_keys(Map.t()) :: Map.t()

Converts an atom-keyed map into a string-keyed map.

Examples

iex> Swiss.Map.to_string_keys(%{life: 42})
%{"life" => 42}

iex> Swiss.Map.to_string_keys(%{"life" => 42, death: 27})
%{"life" => 42, "death" => 27}