I came here looking for a simple way to implement Access behaviour by all structs, but I agree with @NobbZ, it doesn’t make sense to do it for all structs.
So I thought of a simpler solution, just ‘unstructure’ those structs where I need to use get_in to access them deeply.
I made this little function:
def destructuring(struct) when is_struct(struct) do
struct |> Map.from_struct() |> destructuring
end
def destructuring(list) when is_list(list) do
list |> Enum.map(fn v ->
destructuring(v)
end)
end
def destructuring(map) when is_map(map) do
map |> Map.keys() |> Enum.reduce(%{}, fn (k,acc) ->
Map.put(acc, k, destructuring(map[k]))
end)
end
def destructuring(val), do: val
so when I need it I can do:
iex> my_struct |> destructuring() |> get_in([:x, :y, :z])


















