refs

package
v0.9.7 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package refs supplies optional, single-valued cross-domain reference maps and versioned resource definitions. It is independent of persistence drivers and identity context: a resource may explicitly include Values, while an application shares a Schema contract with its producers and consumers.

Values has a strict flat JSON object representation and detached snapshots. Schema validates allowed keys, ID spelling, required roles and write-once roles. Its validation does not verify target existence, ownership, tenant membership or authorization; those checks belong to explicit domain contracts. Refs are never implicitly propagated through callctx or used as identity.

The package provides neither an ORM nor SQL query/index generation. A domain can persist the JSON representation in its own adapter or project hot roles into native columns. Schema versions and any migrations remain explicit resource-contract decisions; no global registry is installed.

Index

Constants

View Source
const (
	CodeInvalidSchema      = "REFS_SCHEMA_INVALID"
	CodeInvalidValues      = "REFS_VALUES_INVALID"
	CodeUnknownReference   = "REFS_UNKNOWN"
	CodeRequiredReference  = "REFS_REQUIRED"
	CodeInvalidID          = "REFS_ID_INVALID"
	CodeImmutableReference = "REFS_IMMUTABLE"
)

Stable error codes work with apperr.Is and survive the contract boundary.

View Source
const (
	MaxEntries   = 64
	MaxKeyBytes  = 64
	MaxIDBytes   = 256
	MaxJSONBytes = 128 * 1024
)

Intrinsic limits bound the work and storage of a single reference collection. They are enforced independently of a resource's Schema.

Variables

This section is empty.

Functions

This section is empty.

Types

type Definition

type Definition struct {
	Key       string `json:"key"`
	Target    string `json:"target"`
	Format    Format `json:"format"`
	Required  bool   `json:"required"`
	Immutable bool   `json:"immutable"`
}

Definition declares one single-valued reference role. Target names the referenced domain/resource (for example merchant.account), not a Go type or a database table. Format must be explicit.

type Format

type Format string

Format specifies the spelling of an ID, not its existence or ownership.

const (
	FormatOpaque Format = "opaque"
	// FormatUUID accepts nonzero, lowercase, hyphenated UUID spellings. It does
	// not restrict the UUID version or variant.
	FormatUUID Format = "uuid"
	// MaxTargetBytes bounds a definition's namespaced target name.
	MaxTargetBytes = 128
)

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

Schema is an immutable, concurrency-safe structural validator. Construct it with NewSchema; its zero value is not configured. It performs no I/O and does not establish identity, tenant membership, authorization or referential integrity.

func NewSchema

func NewSchema(spec Spec) (*Schema, error)

NewSchema validates and copies spec. The stored definitions are sorted by key so validation and Spec output are deterministic. Names are bounded by MaxKeyBytes; namespaced targets by MaxTargetBytes; definitions by MaxEntries.

func (*Schema) DecodeJSON

func (s *Schema) DecodeJSON(data []byte) (Values, error)

DecodeJSON strictly decodes a refs object and validates it as a full snapshot. On any error it returns empty Values, not a partially validated value.

func (*Schema) Spec

func (s *Schema) Spec() Spec

Spec returns a detached copy of the descriptor, with definitions sorted by key. For a nil or unconfigured Schema it returns a zero Spec.

func (*Schema) Validate

func (s *Schema) Validate(values Values) error

Validate checks a full resource snapshot: declared keys, ID formats and all required references. Unknown keys are rejected, not preserved as metadata.

func (*Schema) ValidateFilter

func (s *Schema) ValidateFilter(values Values) error

ValidateFilter checks declared keys and ID formats in a partial equality filter. It does not require Required references; an empty filter is valid. Callers must separately enforce tenant scope, authorization, supported query plans and query cost. This method does not execute or compile queries.

func (*Schema) ValidateUpdate

func (s *Schema) ValidateUpdate(before, after Values) error

ValidateUpdate validates both complete snapshots, then rejects changing or removing an Immutable reference already present in before. Initially absent optional immutable refs may be assigned once. Read before from trusted persisted state, never from a caller-supplied claimed previous value. This is not a patch operation or a concurrency control: persist with a version check or equivalent atomic guard, and validate ownership through domain contracts.

type Spec

type Spec struct {
	Domain      string       `json:"domain"`
	Resource    string       `json:"resource"`
	Version     uint32       `json:"version"`
	Definitions []Definition `json:"definitions"`
}

Spec is the versioned contract for a resource's references. Domain and Resource use lowercase ASCII letters, digits and underscores, starting with a letter. Version is nonzero. Empty Definitions permit only empty Values. Distribute the same spec to producers and consumers; there is no registry or automatic schema-version selection in Values.

type Values

type Values struct {
	// contains filtered or unexported fields
}

Values is a flat collection of named, opaque reference IDs. Its zero value is an empty collection. Constructors and Map detach their maps, so values may be shared for reading without callers changing the stored references. UnmarshalJSON replaces the receiver and must not run concurrently with readers of that receiver. A reference is data, never proof of existence, ownership, or authorization.

func DecodeJSON

func DecodeJSON(data []byte) (Values, error)

DecodeJSON accepts exactly one flat JSON object with string values. It rejects duplicate decoded keys, malformed UTF-8, unpaired escaped UTF-16 surrogates, null, nested structures, trailing input, and values exceeding the intrinsic limits. Errors never include reference IDs or excerpts from the input.

func NewValues

func NewValues(input map[string]string) (Values, error)

NewValues validates and copies input. Names are lowercase ASCII segments separated by dots, for example merchant_account_id or merchant.account_id. IDs are nonempty UTF-8 strings without Unicode spaces or control characters. No normalization, trimming, or business relationship resolution is performed.

func (Values) Get

func (v Values) Get(key string) (string, bool)

Get returns an ID and whether its key is present. An absent key is not a null or empty-string reference: those values are never accepted.

func (Values) Len

func (v Values) Len() int

Len returns the number of references.

func (Values) Map

func (v Values) Map() map[string]string

Map returns a detached, non-nil map, including for an empty Values.

func (Values) MarshalJSON

func (v Values) MarshalJSON() ([]byte, error)

MarshalJSON emits a deterministic JSON object with lexically sorted keys. Empty values encode as {}, never null.

func (*Values) UnmarshalJSON

func (v *Values) UnmarshalJSON(data []byte) error

UnmarshalJSON strictly decodes a flat object, replacing the receiver only on success. It never merges existing references or treats null as an empty object.

Jump to

Keyboard shortcuts

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