ExGAM.Portable (ExGAM v0.1.0)

Copy Markdown View Source

Portable value boundary for the governed-act record layer.

Durable records are reduced to plain maps and values before they enter the ledger. This module rejects runtime-local capabilities (PID, port, reference and function values), improper lists, structs, non-byte-aligned bitstrings and non-finite floats. Accepted values are encoded through the versioned ExGAM.Canonical.Value codec, so map iteration order never affects a digest.

Content references have the form "prefix:sha256". They are identifiers for immutable content, not proof that the content is authoritative.

Summary

Functions

Alias for canonical_value/1, useful at generic record boundaries.

Projects known atom fields into the strict string-keyed record representation.

Validates and returns a plain canonical map.

Returns the deterministic, versioned canonical bytes for a portable value.

Returns canonical bytes or raises when value is not portable.

Builds a stable prefix:digest reference for portable content.

Builds a stable content reference or raises.

Returns the lowercase SHA-256 digest of a portable value.

Returns a digest or raises when value is not portable.

Guard for opaque non-empty binary identifiers, without claiming content validity.

Guard shared by revisions, generations and elapsed millisecond windows.

Guard for a canonical object container, excluding runtime structs.

Guard for strictly positive revisions and quantities.

Checks host keyword options without raising on non-list input.

Normalizes map or keyword attributes and rejects unknown/duplicate keys.

Validates, de-duplicates and sorts a reference list.

Computes a content reference and rejects a supplied reference that does not match it.

Restores a record only from the exact plain representation emitted by it.

Recursively converts atom map keys to strings without changing atom values.

Validates a plain record value within the canonical codec's default resource limits.

Checks a prefix:<lowercase sha256> content-addressed reference.

Checks a required non-empty binary field.

Checks an optional opaque record reference.

Checks an opaque record reference. References are non-empty binaries.

Checks a list of opaque record references.

Types

path()

@type path() :: [term()]

reason()

@type reason() :: term()

Functions

canonical(value)

@spec canonical(term()) :: {:ok, binary()} | {:error, reason()}

Alias for canonical_value/1, useful at generic record boundaries.

canonical_fields(source, fields)

@spec canonical_fields(map(), [atom()]) :: map()

Projects known atom fields into the strict string-keyed record representation.

canonical_map(value)

@spec canonical_map(map()) :: {:ok, map()} | {:error, reason()}

Validates and returns a plain canonical map.

canonical_value(value)

@spec canonical_value(term()) :: {:ok, binary()} | {:error, reason()}

Returns the deterministic, versioned canonical bytes for a portable value.

canonical_value!(value)

@spec canonical_value!(term()) :: binary()

Returns canonical bytes or raises when value is not portable.

content_ref(prefix, value)

@spec content_ref(String.t() | atom(), term()) ::
  {:ok, String.t()} | {:error, reason()}

Builds a stable prefix:digest reference for portable content.

content_ref!(prefix, value)

@spec content_ref!(String.t() | atom(), term()) :: String.t()

Builds a stable content reference or raises.

digest(value)

@spec digest(term()) :: {:ok, String.t()} | {:error, reason()}

Returns the lowercase SHA-256 digest of a portable value.

digest!(value)

@spec digest!(term()) :: String.t()

Returns a digest or raises when value is not portable.

is_non_empty_binary(value)

(macro)

Guard for opaque non-empty binary identifiers, without claiming content validity.

is_non_negative_integer(value)

(macro)

Guard shared by revisions, generations and elapsed millisecond windows.

is_plain_map(value)

(macro)

Guard for a canonical object container, excluding runtime structs.

is_positive_integer(value)

(macro)

Guard for strictly positive revisions and quantities.

keyword?(value)

@spec keyword?(term()) :: boolean()

Checks host keyword options without raising on non-list input.

normalize_attrs(attrs, fields, record_name)

@spec normalize_attrs(term(), [atom()], atom()) :: {:ok, map()} | {:error, reason()}

Normalizes map or keyword attributes and rejects unknown/duplicate keys.

normalize_refs(values, field)

@spec normalize_refs(term(), atom()) :: {:ok, [String.t()]} | {:error, reason()}

Validates, de-duplicates and sorts a reference list.

ref(prefix, value)

@spec ref(String.t() | atom(), term()) :: {:ok, String.t()} | {:error, reason()}

Short alias for content_ref/2.

resolve_content_ref(prefix, supplied, value)

@spec resolve_content_ref(String.t() | atom(), nil | String.t(), term()) ::
  {:ok, String.t()} | {:error, reason()}

Computes a content reference and rejects a supplied reference that does not match it.

restore_canonical(value, builder, canonicalizer, record_name)

@spec restore_canonical(
  term(),
  (term() -> {:ok, term()} | {:error, reason()}),
  (term() -> map()),
  atom()
) :: {:ok, term()} | {:error, reason()}

Restores a record only from the exact plain representation emitted by it.

Record constructors may accept atom-keyed maps, keyword lists and omitted defaults for ergonomic in-memory construction. Durable decoding must be stricter: accepting those alternate shapes would give one semantic record more than one canonical byte representation and digest.

stringify_atom_keys(value)

@spec stringify_atom_keys(term()) :: {:ok, term()} | {:error, reason()}

Recursively converts atom map keys to strings without changing atom values.

This is intended for portable, host-authored semantic maps whose ergonomic atom and transport string spellings mean the same thing. It rejects a map containing both spellings instead of silently choosing one. Other portable key types are preserved.

validate(value)

@spec validate(term()) :: :ok | {:error, reason()}

Validates a plain record value within the canonical codec's default resource limits.

validate_content_ref(value, prefix, field)

@spec validate_content_ref(term(), String.t() | atom(), atom()) ::
  :ok | {:error, reason()}

Checks a prefix:<lowercase sha256> content-addressed reference.

validate_non_empty_binary(value, field)

@spec validate_non_empty_binary(term(), atom()) :: :ok | {:error, reason()}

Checks a required non-empty binary field.

validate_optional_ref(value, field)

@spec validate_optional_ref(term(), atom()) :: :ok | {:error, reason()}

Checks an optional opaque record reference.

validate_ref(value, field)

@spec validate_ref(term(), atom()) :: :ok | {:error, reason()}

Checks an opaque record reference. References are non-empty binaries.

validate_refs(values, field)

@spec validate_refs(term(), atom()) :: :ok | {:error, reason()}

Checks a list of opaque record references.