A2UI.DataModel (A2UI v0.3.0)

Copy Markdown View Source

JSON document store for A2UI surface data.

Provides get/set/delete operations using JSON Pointer (RFC 6901) paths.

Summary

Functions

Deletes a value at the given JSON Pointer path.

Gets a value at the given JSON Pointer path.

Creates a new empty data model.

Creates a data model with initial data.

Sets a value at the given JSON Pointer path.

Types

t()

@type t() :: %A2UI.DataModel{data: map()}

Functions

delete(model, path)

@spec delete(t(), String.t()) :: {:ok, t()} | {:error, String.t()}

Deletes a value at the given JSON Pointer path.

Examples

iex> model = A2UI.DataModel.new(%{"a" => 1, "b" => 2})
iex> {:ok, model} = A2UI.DataModel.delete(model, "/a")
iex> model.data
%{"b" => 2}

get(data_model, path)

@spec get(t(), String.t()) :: {:ok, any()} | :error

Gets a value at the given JSON Pointer path.

Returns {:ok, value} if found, :error if the path doesn't exist.

Examples

iex> model = A2UI.DataModel.new(%{"user" => %{"name" => "Alice"}})
iex> A2UI.DataModel.get(model, "/user/name")
{:ok, "Alice"}

iex> model = A2UI.DataModel.new(%{"a" => 1})
iex> A2UI.DataModel.get(model, "/")
{:ok, %{"a" => 1}}

new()

@spec new() :: t()

Creates a new empty data model.

new(data)

@spec new(map()) :: t()

Creates a data model with initial data.

set(model, path, value)

@spec set(t(), String.t(), any()) :: {:ok, t()} | {:error, String.t()}

Sets a value at the given JSON Pointer path.

Auto-vivifies intermediate maps as needed.

Examples

iex> model = A2UI.DataModel.new()
iex> {:ok, model} = A2UI.DataModel.set(model, "/user/name", "Alice")
iex> A2UI.DataModel.get(model, "/user/name")
{:ok, "Alice"}