Gno.CommitMiddleware behaviour (Gno v0.1.0)

Copy Markdown View Source

Behaviour for pluggable components in the Gno.Commit.Processor pipeline.

The processor calls handle_state/3 on each middleware at every state transition (see the state machine in Gno.Commit.Processor). The callback receives the new state atom, the middleware struct, and the processor, and must return {:ok, processor} (optionally with an updated middleware) or an error tuple to abort. On failure, rollback/2 is called on all middlewares that were already invoked.

The :state field on the middleware struct tracks the last state it was called with, managed automatically by the processor.

Defining a Middleware

Use def_middleware/2 to define a Grax schema subclassing this module:

defmodule MyMiddleware do
  use Gno.CommitMiddleware

  def_middleware MyNS.MyMiddleware do
    property :my_option, type: :string
  end

  @impl true
  def handle_state(:initialized, middleware, processor) do
    # custom logic
    {:ok, processor}
  end
end

Middlewares are configured in the Gno.CommitOperation of a Gno.Service. See Gno.CommitLogger for a built-in example.

Summary

Types

middleware()

@type middleware() :: %{
  :__struct__ => type(),
  :__id__ => term(),
  :state => term(),
  optional(atom()) => term()
}

t()

@type t() :: %Gno.CommitMiddleware{
  __additional_statements__: term(),
  __id__: term(),
  state: term()
}

type()

@type type() :: module()

Callbacks

handle_state(state, middleware, t)

@callback handle_state(state :: atom(), middleware(), Gno.Commit.Processor.t()) ::
  {:ok, Gno.Commit.Processor.t()}
  | {:ok, Gno.Commit.Processor.t(), middleware()}
  | {:error, term()}
  | {:error, term(), Gno.Commit.Processor.t()}

rollback(middleware, t)

@callback rollback(middleware(), Gno.Commit.Processor.t()) ::
  {:ok, Gno.Commit.Processor.t()}
  | {:ok, Gno.Commit.Processor.t(), middleware()}
  | {:error, term()}
  | {:error, term(), Gno.Commit.Processor.t()}

Functions

build(id)

build(id, initial)

build!(id)

build!(id, initial)

build_id(attributes)

def_middleware(class, list)

(macro)

from(value)

@spec from(Grax.Schema.t()) :: {:ok, t()} | {:error, any()}

from!(value)

@spec from!(Grax.Schema.t()) :: t()

load(graph, id, opts \\ [])

@spec load(
  RDF.Graph.t() | RDF.Description.t(),
  RDF.IRI.coercible() | RDF.BlankNode.t(),
  opts :: keyword()
) :: {:ok, t()} | {:error, any()}

load!(graph, id, opts \\ [])

@spec load!(
  RDF.Graph.t() | RDF.Description.t(),
  RDF.IRI.coercible() | RDF.BlankNode.t(),
  opts :: keyword()
) :: t()

set_state(middleware, state, from_state \\ nil)

type(iri)

Returns the middleware module for the given IRI, or nil if not a middleware.

type?(module)

@spec type?(module()) :: boolean()

Checks if the given module is a Gno.CommitMiddleware.

Example

iex> Gno.CommitMiddleware.type?(Gno.CommitLogger)
true

iex> Gno.CommitMiddleware.type?(Gno.Commit)
false

iex> Gno.CommitMiddleware.type?(NonExisting)
false