View Source Chord.Delta.Formatter.Default (Chord v0.2.0)
Default implementation for delta formatting in Chord.
This module transforms delta maps into a standardized format that is easy to process,
serialize, and consume. Each change is flattened into a list of maps, providing details such as
the action performed (:added
, :modified
, :removed
), the affected key path, and associated metadata
like context_id
.
Customization
Developers can implement their own delta formatter by defining a module that adheres to
the Chord.Delta.Formatter.Behaviour
and setting it in the application configuration.
defmodule MyApp.CustomFormatter do
@behaviour Chord.Delta.Formatter.Behaviour
def format(delta, metadata) do
# Custom formatting logic here
end
end
To configure your formatter:
config :chord, delta_formatter: MyApp.CustomFormatter
Summary
Functions
Formats a delta map into a standardized format with metadata.
Functions
Formats a delta map into a standardized format with metadata.
This function processes deltas, flattening key paths into lists and associating
each change with additional metadata such as context_id
.
Parameters
delta
(map): The delta map representing changes to the context.metadata
(map): A keyword list or map containing additional information, such as::context_id
- The identifier of the context.:version
- The version of the context being formatted.
Returns
- A map with the following structure:
:version
- The version number provided inmetadata
(optional).:changes
- A list of formatted changes, each represented as a map.
Examples
iex> delta = %{
...> status: %{action: :added, value: "online"},
...> metadata: %{
...> language: %{action: :added, value: "en-US"},
...> theme: %{action: :modified, old_value: "light", value: "dark"}
...> }
...> }
iex> metadata = %{context_id: "user:369", version: 2}
iex> Chord.Delta.Formatter.Default.format(delta, metadata)
%{
version: 2,
changes: [
%{value: "online", key: :status, action: :added, context_id: "user:369"},
%{
value: "en-US",
key: [:metadata, :language],
action: :added,
context_id: "user:369"
},
%{
value: "dark",
key: [:metadata, :theme],
action: :modified,
context_id: "user:369",
old_value: "light"
}
]
}