Unpack v0.1.4 Unpack View Source

Unpack lets you “unpack” values from a nested map safely.

Link to this section Summary

Functions

Traverses any nested map or struct (data), in order of keys list (arg2), to return a value. Returns nil for bad keys, unloaded associations or empty maps. Can also take a 3rd param to change default return value

Link to this section Functions

Link to this function get(data, list, default \\ nil) View Source

Traverses any nested map or struct (data), in order of keys list (arg2), to return a value. Returns nil for bad keys, unloaded associations or empty maps. Can also take a 3rd param to change default return value.

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

iex> map = %{player: %{}}
iex> Unpack.get(map, [:player, :wrong_key], "eh!")
"eh!"