Last active
May 28, 2019 03:54
-
-
Save gvaughn/497377d4195818c7e4c3caf22158c21e to your computer and use it in GitHub Desktop.
Elixir get_in with nested maps and lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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