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 MapToKeywordList do | |
def map_to_keyword_list(map), do: convert(map) | |
def convert(map) when is_map(map), do: Enum.map(map, fn {k,v} -> {String.to_atom(k),convert(v)} end) | |
def convert(v), do: v | |
end | |
# e.g. map= %{"a" => "aa", "b" => %{"c" => "cc", "d" => %{"ee" => "eee", "ff" => "fff"}}} |
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 Service.MiscScripts do | |
@doc """ | |
Changes String Map to Map of Atoms e.g. %{"c"=> "d", "x" => %{"yy" => "zz"}} to | |
%{c: "d", x: %{yy: "zz"}}, i.e changes even the nested maps. | |
""" | |
def convert_to_atom_map(map), do: to_atom_map(map) | |
defp to_atom_map(map) when is_map(map), do: Map.new(map, fn {k,v} -> {String.to_atom(k),to_atom_map(v)} end) |
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 Nested do | |
def get_inner_element(input) do | |
Enum.at(input,0) | |
|> __MODULE__.get_map(["accessConfigs"] ) | |
|>Enum.at(0) | |
|> __MODULE__.get_element_from_map("natIP") | |
end | |
def get_map(map,[head|tail]) do | |
map[head] |> get_map tail |