Skip to content

Instantly share code, notes, and snippets.

@greenboxal
Created August 31, 2017 03:16
Show Gist options
  • Save greenboxal/11baa308c83c302366f9f7e74202af39 to your computer and use it in GitHub Desktop.
Save greenboxal/11baa308c83c302366f9f7e74202af39 to your computer and use it in GitHub Desktop.
defp cast_args(args) when is_map(args) do
Enum.map args, fn {k, v} ->
{key, rest} = case k do
":" <> k ->
{String.to_atom(k), k}
_ ->
{k, k}
end
{key, cast_arg(rest, value)}
end
end
defp cast_args(arg) when is_list(args) do
Enum.map args, &cast_args/1
end
defp cast_args(arg) do: arg
defp cast_arg(arg, name) do
case name do
"%" <> _ ->
do_cast_arg(:map, arg)
"{}" <> _ ->
do_cast_arg(:tuple, arg)
"[]" <> _ ->
do_cast_arg(:list, arg)
_ ->
arg
end
end
defp do_cast_arg(:map, arg) when is_map(arg) do
arg
end
defp do_cast_arg(:map, arg) when is_list(arg) do
arg
|> Enum.with_indx
|> Enum.reduce(%{}, fn ({i, v}, acc) -> Map.put(acc, i, v) end)
end
defp do_cast_arg(:tuple, arg) when is_list(arg) do
List.to_tuple(arg)
end
defp do_cast_arg(:list, arg) when is_list(arg) do
arg
end
defp do_cast_arg(:list, arg) when is_map(arg) do
arg
|> cast_args(arg)
|> Map.to_list
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment