Using Kernel.update_in with a programatically determined key

As stated Access exists to provide

Key-based access to data structures using the data[key] syntax.

The OP also contained attempts to use the static access operator which will work on structs that do not implement the Access callbacks. But you are correct that the structs would have to implement Access callbacks if the supplied list of keys contained “regular keys”.

However get_and_update_in/3 and get_in/2 (and cousins) will work with non-Access structs just fine if they are provided with a list of custom functions - an approach that seems appropriate when interacting with a struct or map “in a programmatic fashion”. Now in most cases the functions generated by Access.key/2 will be sufficient but it can be instructive to DIY just to see what is going on:

# file: info.ex
#
# Some code trying to demonstrate the mechanics of using "functions instead of keys"
# with Kernel.get_and_update_in/3 and Kernel.get_in/2, attempting to expand on the
# somewhat terse examples found in
# https://hexdocs.pm/elixir/Kernel.html#get_and_update_in/3-examples
# https://hexdocs.pm/elixir/Kernel.html#get_in/2-examples
#
# fn(:get_and_update, data, next) - Clause used for Kernel.get_and_update_in/3
# 1. Obtain "value" of implied key from "data"
# 2. Hand "next" that "value" i.e. {replaced_info, updated_value} = next.(value)
# 3. Update the "data" with the "updated_value" under the implied key to get "updated_data"
# 4. Return {replaced_info, updated_data}
#
# fn(:get, data, next) - Clause used for Kernel.get_in/3
# 1. Obtain "value" of implied key from "data"
# 2. Hand "next" that "value" i.e. info = next.(value)
# 3. Return "info"
#
# Note that Access.key/2 will generate these type of functions automatically - however
# that source accommodates all possible scenarios which can easily obscure
# the fundamental operation. This code focuses on navigating a nested struct.
#
# Essentially both "get_and_update_in" and "get_in" process the keys list recursively
# via the "next" callback handed to each "key function" - which is expected to extract
# the value for the key it is responsible for - invoking "next" with that value
# so that (recursive) navigation on the next level "key function" can continue.
#

defmodule Info do
  defstruct [:name, :age]
  @age_default 0

  def age_access(:get_and_update, info, next) do
    age = Map.get(info, :age, @age_default)
    {old, new} = next.(age) # i.e. age_and_update function
    {old, (Map.put info, :age, new)}
  end
  def age_access(:get, info, next) do
    age = Map.get(info, :age, @age_default)
    next.(age) # i.e. identity function
  end

  def all_list(:get_and_update, info_list, next) when is_list(info_list) do
    old_new_list = (Enum.map info_list, next) # e.g. (get_and_update_in &1, [age_access], age_and_update) on each item
                                              #      resulting in {original_age, updated_info} for each item in the list
    :lists.unzip old_new_list                 # need a list of original ages and a list of updated infos
  end
  def all_list(:get, info_list, next) when is_list(info_list) do
    Enum.map info_list, next                  # e.g. (get_in &1, [age_access]) on each item
  end
end

defmodule Role do
  defstruct [:name, :info]
  @info_default %Info{name: "unknown", age: 0}

  def info_access(:get_and_update, role, next) do
    info = Map.get(role, :info, @info_default)
    {old, new} = next.(info) # e.g. (get_and_update_in &1, [age_access], age_and_update)
    {old, (Map.put role, :info, new)}
  end
  def info_access(:get, role, next) do
    info = Map.get(role, :info, @info_default)
    next.(info) # e.g. (get_in &1, [age_access])
  end
end

defmodule Trial do
  def run do
    john = %Info{name: "john", age: 27}
    meg = %Info{name: "meg", age: 23}
    info_list = [john, meg]
    owner = %Role{name: "owner", info: john}

    # NOTE: these take the value NOT the key as stated in
    # https://hexdocs.pm/elixir/Kernel.html#get_and_update_in/3
    # This code is consistent with the examples shown where the
    # passed parameter IS a value NOT a key
    age_and_update = fn age -> {age, age + 1} end
    owner_and_replacement = fn owner -> {owner, meg} end # i.e. meg replaces owner

    age_access = &Info.age_access/3
    info_access = &Role.info_access/3
    all_list = &Info.all_list/3

    # IO.inspect (Kernel.get_and_update_in john, [:age], age_and_update)
    IO.inspect (Kernel.get_and_update_in john, [age_access], age_and_update)
    IO.inspect (Kernel.get_in john, [age_access])
    IO.inspect (Kernel.get_and_update_in owner, [info_access, age_access], age_and_update)
    IO.inspect (Kernel.get_and_update_in owner, [info_access], owner_and_replacement)
    IO.inspect (Kernel.get_in owner, [info_access, age_access])
    IO.inspect (Kernel.get_in owner, [info_access])
    IO.inspect info_list
    IO.inspect (Kernel.get_and_update_in info_list, [all_list, age_access], age_and_update)
    IO.inspect (Kernel.get_in info_list, [all_list, age_access])
  end
end

Trial.run()

1 Like