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 ¶
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.
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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) MarshalJSON ¶
MarshalJSON emits a deterministic JSON object with lexically sorted keys. Empty values encode as {}, never null.
func (*Values) UnmarshalJSON ¶
UnmarshalJSON strictly decodes a flat object, replacing the receiver only on success. It never merges existing references or treats null as an empty object.