{"modulePath":"github.com/forgeplex/appkit","version":"v0.14.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package refs\n\nPackage 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.\n\nValues 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.\n\nThe 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.\n\n## Constants\n\n```go\nconst (\n\tCodeInvalidSchema      = \"REFS_SCHEMA_INVALID\"\n\tCodeInvalidValues      = \"REFS_VALUES_INVALID\"\n\tCodeUnknownReference   = \"REFS_UNKNOWN\"\n\tCodeRequiredReference  = \"REFS_REQUIRED\"\n\tCodeInvalidID          = \"REFS_ID_INVALID\"\n\tCodeImmutableReference = \"REFS_IMMUTABLE\"\n)\n```\nStable error codes work with apperr.Is and survive the contract boundary.\n\n```go\nconst (\n\tMaxEntries   = 64\n\tMaxKeyBytes  = 64\n\tMaxIDBytes   = 256\n\tMaxJSONBytes = 128 * 1024\n)\n```\nIntrinsic limits bound the work and storage of a single reference collection. They are enforced independently of a resource's Schema.\n\n## Types\n\n```go\ntype Definition struct {\n\tKey       string `json:\"key\"`\n\tTarget    string `json:\"target\"`\n\tFormat    Format `json:\"format\"`\n\tRequired  bool   `json:\"required\"`\n\tImmutable bool   `json:\"immutable\"`\n}\n```\nDefinition 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.\n\n```go\ntype Format string\n```\nFormat specifies the spelling of an ID, not its existence or ownership.\n\n```go\nconst (\n\tFormatOpaque Format = \"opaque\"\n\t// FormatUUID accepts nonzero, lowercase, hyphenated UUID spellings. It does\n\t// not restrict the UUID version or variant.\n\tFormatUUID Format = \"uuid\"\n\t// MaxTargetBytes bounds a definition's namespaced target name.\n\tMaxTargetBytes = 128\n)\n```\n\n```go\ntype Schema struct {\n\t// contains filtered or unexported fields\n}\n```\nSchema 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.\n\n```go\nfunc NewSchema(spec Spec) (*Schema, error)\n```\nNewSchema 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.\n\n```go\nfunc (s *Schema) DecodeJSON(data []byte) (Values, error)\n```\nDecodeJSON strictly decodes a refs object and validates it as a full snapshot. On any error it returns empty Values, not a partially validated value.\n\n```go\nfunc (s *Schema) Spec() Spec\n```\nSpec returns a detached copy of the descriptor, with definitions sorted by key. For a nil or unconfigured Schema it returns a zero Spec.\n\n```go\nfunc (s *Schema) Validate(values Values) error\n```\nValidate checks a full resource snapshot: declared keys, ID formats and all required references. Unknown keys are rejected, not preserved as metadata.\n\n```go\nfunc (s *Schema) ValidateFilter(values Values) error\n```\nValidateFilter 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.\n\n```go\nfunc (s *Schema) ValidateUpdate(before, after Values) error\n```\nValidateUpdate 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.\n\n```go\ntype Spec struct {\n\tDomain      string       `json:\"domain\"`\n\tResource    string       `json:\"resource\"`\n\tVersion     uint32       `json:\"version\"`\n\tDefinitions []Definition `json:\"definitions\"`\n}\n```\nSpec 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.\n\n```go\ntype Values struct {\n\t// contains filtered or unexported fields\n}\n```\nValues 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.\n\n```go\nfunc DecodeJSON(data []byte) (Values, error)\n```\nDecodeJSON 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.\n\n```go\nfunc NewValues(input map[string]string) (Values, error)\n```\nNewValues 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.\n\n```go\nfunc (v Values) Get(key string) (string, bool)\n```\nGet 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.\n\n```go\nfunc (v Values) Len() int\n```\nLen returns the number of references.\n\n```go\nfunc (v Values) Map() map[string]string\n```\nMap returns a detached, non-nil map, including for an empty Values.\n\n```go\nfunc (v Values) MarshalJSON() ([]byte, error)\n```\nMarshalJSON emits a deterministic JSON object with lexically sorted keys. Empty values encode as {}, never null.\n\n```go\nfunc (v *Values) UnmarshalJSON(data []byte) error\n```\nUnmarshalJSON strictly decodes a flat object, replacing the receiver only on success. It never merges existing references or treats null as an empty object.\n\n","path":"github.com/forgeplex/appkit/refs","name":"refs","synopsis":"Package refs supplies optional, single-valued cross-domain reference maps and versioned resource definitions.","isRedistributable":true}
