Grove.Data (Grove v0.1.1)

View Source

Conversion between plain data and Grove trees.

This module provides functions to convert hierarchical plain Elixir data structures to and from Grove.Tree CRDT structures.

Plain Data Format

Plain data is represented as nested maps with the following structure:

%{
  id: "form_1",           # optional, auto-generated if missing
  type: :form,            # required
  attrs: %{title: "..."},  # optional, defaults to %{}
  children: [...]         # optional, defaults to []
}

Example

# Convert plain data to tree
plain = %{
  id: "form_1",
  type: :form,
  attrs: %{title: "Survey"},
  children: [
    %{id: "field_1", type: :text, attrs: %{label: "Name"}}
  ]
}

tree = Grove.Data.from_data(plain, replica_id: "node_a")

# Convert back to plain data
plain = Grove.Data.to_data(tree)

Round-Trip Guarantee

When all nodes have explicit IDs:

data == Grove.Data.to_data(Grove.Data.from_data(data, replica_id: "a"))

Summary

Functions

Converts plain hierarchical data to a Grove tree.

Converts a Grove tree back to plain hierarchical data.

Types

plain_node()

@type plain_node() :: %{
  optional(:id) => String.t(),
  :type => atom(),
  optional(:attrs) => map(),
  optional(:children) => [plain_node()]
}

Functions

from_data(plain_data, opts)

@spec from_data(
  plain_node(),
  keyword()
) :: Grove.Tree.t()

Converts plain hierarchical data to a Grove tree.

Options

  • :replica_id - Required. Unique identifier for this replica.

Example

tree = Grove.Data.from_data(plain_data, replica_id: "node_a")

to_data(tree)

@spec to_data(Grove.Tree.t()) :: plain_node() | nil

Converts a Grove tree back to plain hierarchical data.

Deleted nodes are excluded from the output.

Example

plain_data = Grove.Data.to_data(tree)