Dextrin.Schema.Compiled (Dextrin v0.1.0)

Copy Markdown View Source

The result of compiling one %schema{} entry from a .dxns document (Dextrin.Schema.Compiler.compile/3) — enough to both validate a decoded value and convert it both directions between .dxnb's always-positional wire shape and a named field map.

fields is ordered — that order is the canonical position ↔ name mapping .dxnb's positional struct encoding needs, which is exactly why a schema's own fields: has to be an @ordered %{...} in the source document, not a plain map (.dxn's map type makes no ordering guarantee, but field order here is semantically load-bearing, not incidental).

Summary

Functions

Reads application_struct's fields (an arbitrary Elixir struct — not a Dextrin.Struct) out in this schema's own canonical field order — the same order .dxnb's positional wire encoding needs. A field this schema declares but application_struct doesn't have is nil, same as any other absent field; extra struct fields the schema doesn't know about are simply not included.

Types

refine_fn()

@type refine_fn() :: (%{optional(atom()) => term()} -> :ok | {:error, term()})

t()

@type t() :: %Dextrin.Schema.Compiled{
  closed: boolean(),
  fields: [Dextrin.Schema.Field.t()],
  forbidden: [String.t()],
  name: String.t(),
  refine_fn: refine_fn() | nil
}

Functions

field_names(compiled)

@spec field_names(t()) :: [String.t()]

field_values(compiled, application_struct)

@spec field_values(
  t(),
  struct()
) :: [{String.t(), term()}]

Reads application_struct's fields (an arbitrary Elixir struct — not a Dextrin.Struct) out in this schema's own canonical field order — the same order .dxnb's positional wire encoding needs. A field this schema declares but application_struct doesn't have is nil, same as any other absent field; extra struct fields the schema doesn't know about are simply not included.

What lets Dextrin.Text.Printer/Dextrin.Binary.Encoder serialize a struct registered via Dextrin.Registry.put_struct_module/3 directly — without a caller needing to hand-build a Dextrin.Struct themselves first, closing the loop Dextrin.Schema.Validator.materialize/4 already closes on the way in.