I was looking for information on maps that might have more nesting and couldn’t find anything. I came up with this and hope it helps others also searching for that:
def merge_nested(map1, map2) do
Map.merge(map1, map2, &merge_nested/3)
end
def merge_nested(_k, %{} = v1, %{} = v2) do
Map.merge(v1, v2, &merge_nested/3)
end
def merge_nested(_k, _v1, v2), do: v2
For example:
a = %{a: %{b: %{c: 1}}}
b = %{a: %{b: %{d: 2}}}
merge_nested(a, b)
#> %{a: %{b: %{c: 1, d: 2}}}