Ecto.Schema and Access behaviour

If you really need it, maybe you can implement this behaviour yourself by using either Map.from_struct or some metaprogramming (it’s just 4 callbacks).

defmodule User do
  @behaviour Access

  # ... schema and stuff

  # Access callbacks the easy way
  def fetch(term, key) do
    term
    |> Map.from_struct()
    |> Map.fetch(key)
  end

  def get(term, key, default) do
    term
    |> Map.from_struct()
    |> Map.get(key, default)
  end

  def get_and_update(data, key, function) do
    data
    |> Map.from_struct()
    |> Map.get_and_update(key, function)
  end

  def pop(data, key) do
    data
    |> Map.from_struct()
    |> Map.pop(key)
  end
end

You might also want to read this

But in ecto the schema structs usually represent rows in a table, and modifications to these rows are then done via changesets. So your example can be probably expressed via them.

https://hexdocs.pm/ecto/Ecto.Changeset.html#module-associations-embeds-and-on-replace

^^^ see maybe_mark_for_deletion/1

2 Likes