Hourglass.Codec behaviour (hourglass v0.1.0)

Copy Markdown View Source

The typed-payload boundary for Hourglass workflow/activity I/O.

A type is either a module implementing this behaviour (typically via use Hourglass.Schema) or one of the built-in scalar atoms: :string, :integer, :float, :boolean, :map, :any.

cast/2 turns a JSON-decoded term (string-keyed maps, primitives) into a validated value; dump/2 turns a value into a JSON-encodable term. These are the functions the workflow/activity entry wrappers call at the wire boundary.

Summary

Callbacks

Cast a JSON-decoded term into a validated value, or an error.

Serialize a value to a JSON-encodable term.

Functions

Cast a JSON-decoded raw term against type.

Like cast/2 but returns the value or raises on error.

Dump value to a JSON-encodable term according to type.

Types

scalar()

@type scalar() :: :string | :integer | :float | :boolean | :map | :any

type()

@type type() :: module() | scalar()

Callbacks

cast(term)

@callback cast(term()) :: {:ok, term()} | {:error, Ecto.Changeset.t()}

Cast a JSON-decoded term into a validated value, or an error.

dump(term)

@callback dump(term()) :: term()

Serialize a value to a JSON-encodable term.

Functions

cast(type, raw)

@spec cast(type(), term()) :: {:ok, term()} | {:error, term()}

Cast a JSON-decoded raw term against type.

cast!(type, raw)

@spec cast!(type(), term()) :: term()

Like cast/2 but returns the value or raises on error.

dump(type, value)

@spec dump(type(), term()) :: term()

Dump value to a JSON-encodable term according to type.