Example of implementing Access protocol (behaviour?) in Struct

I stumbled across the desire to want to use get_in while accessing deeply nested parts of a custom struct while needing to provide a default value. I could make a simple defp function to achieve this, but I wanted to see how to handle it using the Access protocol, so I tried something like this:

get_in(my_struct, [:x, Access.key(:y, "my_default)])

Where my struct was shaped something like

%MyStruct{x: %{y: "My Value"}}
# or maybe 
%MyStruct{x: %{}}

This results in an error:

** (UndefinedFunctionError) function MyStruct.fetch/2 is undefined (MyStruct does not implement the Access behaviour)

I have 2 followup questions to this:

  1. Is there an example somewhere in some library that demonstrates how to implement the Access behaviour?
  2. I’ve read somewhere that structs don’t implement this behaviour for performance reasons – what considerations should be made before implementing the behavior in a struct?

Thanks as always!

1 Like

Not performance I’d say… Semantic reasons. It doesn’t make sense for all structs.

Also let’s take a fictive set which implements access and returns either true or false, depending on if the value is a member.

The struct itself has only a single field, which shall not be made accessible, but instead access is just a Map.get(s.m, value, false).

I’m not aware of any examples though, but perhaps you can find something in elixir code base for maps or keywordlists.

I think you’ll find this will do the trick:

iex> get_in(my_struct, [Access.key(:x, Access.key(:y, "my_default"))])
%{y: "My Value"}

You can wrap struct keys in Access.key/2 if required without implementing the whole Access protocol.

That looks promising… however, it seems to return not the value for :y (or its default), but instead, the entire map stored at :x :thinking:

Wasn’t quite sure what you were after. Probably you wanted:

iex> my_struct = %MyStruct{x: %{y: "this thing"}}
%MyStruct{x: %{y: "this thing"}}
iex> get_in(my_struct, [Access.key(:x), Access.key(:y, "my_default")])
"this thing"

Note that since :x is just a map you can do the following if you don’t need to supply a default (which you shouldn’t have to so since this is about a struct:

iex> get_in(my_struct, [Access.key(:x), :y])                          
"this thing"

That works! I think I tried a dozen things that were close to that. Thank you!