schema

package
v0.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package schema provides a generic versioned-YAML loader for any file that embeds a schema version field. It handles version probing, unknown-field rejection, and in-memory migration to the latest struct shape.

The pattern, extracted once so every versioned file behaves identically:

  1. Probe the version field (Versioned.VersionKey, default "schemaVersion") from raw YAML before decoding the body.
  2. Normalise an absent/zero version to 1 (files predating versioning).
  3. Reject any version greater than the running build supports (a file written by a newer binary) with a human-readable "newer binary" error rather than a surprising decode failure against the current shape.
  4. Strict-decode (KnownFields, single document) into the sealed per-version struct registered for that version.
  5. Walk the migration chain via Migratable.MigrateToLatest() to produce the current in-memory type T.

The genuinely domain-specific parts — the sealed vN structs and their MigrateToLatest field transforms — stay in each consuming package. Only the cross-cutting orchestration lives here.

Index

Constants

View Source
const DefaultVersionKey = "schemaVersion"

DefaultVersionKey is the YAML field solo-weaver owned state files use to carry their schema version. It is the default when Versioned.VersionKey is empty; external schemas may override it with any key they prefer.

Variables

View Source
var (
	// ErrNamespace groups all schema-loading errors.
	ErrNamespace = errorx.NewNamespace("schema")

	// ErrMalformed is returned when the document cannot be probed or strict-decoded.
	ErrMalformed = ErrNamespace.NewType("malformed")

	// ErrUnsupportedVersion is returned when the document declares a schemaVersion
	// the running build does not support (typically a file written by a newer binary).
	ErrUnsupportedVersion = ErrNamespace.NewType("unsupported_version")
)

Functions

This section is empty.

Types

type Migratable

type Migratable[T any] interface {
	MigrateToLatest() T
}

Migratable is a sealed, versioned on-disk struct that knows how to migrate itself up to the latest in-memory shape T. Each owned state file defines one implementation per historical version; the terminal version's MigrateToLatest is the identity-style transform into T, and earlier versions delegate down the chain (vN.migrate().MigrateToLatest()).

type Versioned

type Versioned[T any] struct {
	// VersionKey is the YAML field carrying the schema version. Empty means
	// DefaultVersionKey ("schemaVersion"); external schemas may set any key.
	VersionKey string

	// CurrentVersion is the highest schema version this build understands and writes.
	CurrentVersion int

	// Factories maps a supported version to a constructor returning a fresh
	// sealed struct (as a Migratable[T]) to decode that version's document into.
	Factories map[int]func() Migratable[T]
}

Versioned describes one owned-state-file schema: the YAML key that carries the version, the highest version this build writes, and a factory per supported version that returns a fresh pointer to strict-decode into.

func (Versioned[T]) Decode

func (s Versioned[T]) Decode(data []byte) (T, error)

Decode runs the full owned-state-file load pattern on raw YAML bytes and returns the migrated current type T. See the package doc for the steps.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL