View Source Rivet.Utils.Dig (rivet_utils v2.0.1)
Contributor: Brandon Gillespie
Summary
Functions
dig a value using a string index digKeys notation This takes both a.b.c and a[{index}].b.c, where index in the second case is an indirect key/value lookup
[see also: frontend code base for mirror code in js]
dig a value using pre-parsed digKeys
This is the inverse of dig -- put a value in place
Functions
dig a value using a string index digKeys notation This takes both a.b.c and a[{index}].b.c, where index in the second case is an indirect key/value lookup
iex> data = %{a: [ %{c: 1}, %{d: 2}, %{b: "name", c: %{d: "meep"}}]} iex> dig(data, "a[{b:name}].c.d") "meep" iex> dig(data, "a[{b:name}].d.d") nil iex> dig(data, "a[{b:bigglesworth}].c.d") nil
[see also: frontend code base for mirror code in js]
keys must be a list of {direct: true|false, key: y} where direct=true is simply obj[key], and indirect does a search of an array for the first key/value match
NOTE: keys are always converted to atoms
iex> dig_keys("a[{b:name}].capKey.d") [ %{ direct: true, key: :a }, %{ direct: false, key: [:b, "name"] }, %{ direct: true, key: :capKey }, %{ direct: true, key: :d } ] iex> dig_keys("a[{b:name}].capKey.d", snake_case: true) [ %{ direct: true, key: :a }, %{ direct: false, key: [:b, "name"] }, %{ direct: true, key: :cap_key }, %{ direct: true, key: :d } ]
dig a value using pre-parsed digKeys
This is the inverse of dig -- put a value in place
iex> data = %{a: [ %{c: 1}, %{d: 2}, %{b: "road", c: %{d: "meep"}}]} iex> dug(data, "a[{b:road}].c.d", "meep meep") {:ok, %{a: [ %{c: 1}, %{d: 2}, %{b: "road", c: %{d: "meep meep"}}]}} iex> dug(data, "a[{b:runner}].c.d", "meep meep") {:ok, %{a: [%{c: 1}, %{d: 2}, %{b: "road", c: %{d: "meep"}}, %{b: "runner", c: %{d: "meep meep"}}]}} iex> dug(%{i: 10}, "i", 5) {:ok, %{i: 5}} iex> dug(%{a: [%{b: "z"}, %{b: "z"}]}, "a[{b:z}].c", 5) {:ok, %{a: [%{b: "z", c: 5}, %{b: "z"}]}}