deep_merge v0.1.1 DeepMerge.Resolver protocol

Protocol defining how conflicts during deep_merge should be resolved.

As part of the DeepMerge library this protocol is already implemented for Map and List as well as a fallback to Any.

Summary

Functions

Defines what happens when a merge conflict occurs on this data type during a deep_merge

Types

t()
t() :: term

Functions

resolve(original, override, resolver)

Defines what happens when a merge conflict occurs on this data type during a deep_merge.

Can be implemented for additional data types to implement custom deep merging behavior.

The passed in values are:

  • original - the value in the original data structure, usually left side argument
  • override - the value with which original would be overridden in a normal merge/2
  • resolver - the function used by DeepMerge to resolve merge conflicts, i.e. what you can pass to Map.merge/3 and Keyword.merge/3 to continue deeply merging.

An example implementation might look like this if you want to deeply merge your struct if the other value also is a struct:

defmodule MyStruct do
  defstruct [:attrs]
end

defimpl DeepMerge.Resolver, for: MyStruct do
  def resolve(original, override = %{__struct__: MyStruct}, resolver) do
    Map.merge(original, override, resolver)
  end
  def resolve(_, override, _) do
    override
  end
end