Unpack v0.1.5 Unpack View Source

Unpack lets you safely “unpack” any values from a nested map, struct or database object.

Link to this section Summary

Functions

Traverses nested data map or struct in order of keys list to return a value. Returns nil for missing keys, unloaded associations or empty maps

Traverses nested data map or struct in order of keys list to return a value. Returns given default parameter for missing keys, unloaded associations or empty maps

Link to this section Functions

Link to this function get(data, list) View Source
get(map(), [any()]) :: any() | nil

Traverses nested data map or struct in order of keys list to return a value. Returns nil for missing keys, unloaded associations or empty maps.

Examples

iex> map = %{player: %{game: %{id: "game_id"}}}
iex> Unpack.get(map, [:player, :game, :id])
"game_id"

iex> struct = %{player: %Ecto.Association.NotLoaded{}}
iex> Unpack.get(struct, [:player, :game, :id])
nil
Link to this function get(data, list, default) View Source
get(map(), [any()], any()) :: any()

Traverses nested data map or struct in order of keys list to return a value. Returns given default parameter for missing keys, unloaded associations or empty maps.

Examples

iex> map = %{player: %{name: "George"}}
iex> Unpack.get(map, [:player, :email], "🙈")
"🙈"