Skip to content

Instantly share code, notes, and snippets.

@unixc3t
Forked from gvaughn/deeply_nested.exs
Created May 28, 2019 03:54
Show Gist options
  • Save unixc3t/ce8356d4ce85934eaf978e47a405543d to your computer and use it in GitHub Desktop.
Save unixc3t/ce8356d4ce85934eaf978e47a405543d to your computer and use it in GitHub Desktop.
Elixir get_in with nested maps and lists
defmodule DeeplyNested do
def get_nat_ip(input) do
steps = [first_map_with_key("accessConfigs"),
first_map_with_key("natIP")
]
get_in(input, steps)
end
defp first_map_with_key(key) do
fn :get, data, next ->
Enum.find_value(data, fn %{^key => val} -> next.(val) end)
end
end
end
input = [%{"accessConfigs" => [%{"kind" => "compute#accessConfig",
"name" => "External NAT", "natIP" => "146.148.23.208",
"type" => "ONE_TO_ONE_NAT"}]}]
IO.inspect DeeplyNested.get_nat_ip(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment