Help us shape the future of Go! Take the 2026 Go Developer Survey

model

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package model defines model-owned foundational values without depending on a database driver or runtime. Generated model APIs build on these contracts.

Index

Constants

View Source
const MaxIdentityKeyBytes = 4096

MaxIdentityKeyBytes bounds the encoded SQL key, including its kind and JSON envelope. It limits attribution metadata, not database column capacity.

Variables

This section is empty.

Functions

This section is empty.

Types

type ID

type ID[M any] struct {
	// contains filtered or unexported fields
}

ID is a comparable UUID whose owner is checked by the Go compiler. The zero value is the nil UUID, not a newly generated identity. M need not be comparable. Bytes/ParseID are explicit serialization boundaries that discard/reapply owner information; they do not establish record existence or authorization.

func IDFromBytes

func IDFromBytes[M any](bytes [16]byte) ID[M]

IDFromBytes is the explicit binary decoding boundary.

func NewIDAt

func NewIDAt[M any](now time.Time) (ID[M], error)

NewIDAt generates a UUIDv7 from an explicit clock reading and 74 random bits, following RFC 9562 section 5.7. Ordering within a millisecond, across clock rollback, or across hosts is not guaranteed. IDs are not authentication secrets.

func ParseID

func ParseID[M any](text string) (ID[M], error)

ParseID accepts exactly the canonical 8-4-4-4-12 hexadecimal UUID shape, case-insensitively. Existing UUID versions and the nil UUID are preserved.

func (ID[M]) Bytes

func (id ID[M]) Bytes() [16]byte

Bytes returns the identifier's 16 bytes by value.

func (ID[M]) IsZero

func (id ID[M]) IsZero() bool

IsZero reports whether the identifier is all zero bytes.

func (ID[M]) MarshalText

func (id ID[M]) MarshalText() ([]byte, error)

MarshalText encodes the identifier's UUID string as text.

func (ID[M]) String

func (id ID[M]) String() string

String returns the identifier as a lowercase, hyphenated UUID string.

func (*ID[M]) UnmarshalJSON

func (id *ID[M]) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a JSON identifier, replacing the receiver only on success.

func (*ID[M]) UnmarshalText

func (id *ID[M]) UnmarshalText(data []byte) error

UnmarshalText parses an identifier, replacing the receiver only on success.

type Identifiable

type Identifiable interface {
	// FoundryIdentity returns the model's persistence identity.
	FoundryIdentity() (Identity, error)
}

Identifiable is implemented by generated model metadata. Framework adapters capture the stored identity without another database lookup or an Actor.

type Identity

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

Identity is immutable serialized model attribution. Model/key types are erased only at this explicit transport boundary. Received metadata grants no authority. Routine formatting omits the key; JSON deliberately exports it.

func ParseIdentity

func ParseIdentity(name, keyJSON string) (Identity, error)

ParseIdentity validates an explicit model namespace and tagged SQL key JSON. Generated references perform the typed restoration; parsing alone grants no trust. Scalar Go JSON and presentation values are not accepted as stored keys.

func (Identity) Format

func (Identity) Format(state fmt.State, _ rune)

Format writes a fixed label without exposing identity data.

func (Identity) IsZero

func (i Identity) IsZero() bool

IsZero reports whether both model name and key are uninitialized.

func (Identity) KeyJSON

func (i Identity) KeyJSON() (string, error)

KeyJSON exports the tagged SQL key for a deliberate storage/transport boundary. The kind and exact text preserve integers, bytes and times without JSON number rounding. Ordinary application code uses the concrete Reference.Key value.

func (Identity) MarshalJSON

func (i Identity) MarshalJSON() ([]byte, error)

MarshalJSON validates and encodes the model identity as JSON.

func (Identity) ModelName

func (i Identity) ModelName() string

ModelName returns the identity's model name.

func (*Identity) UnmarshalJSON

func (i *Identity) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a bounded JSON identity, replacing the receiver only after key normalization succeeds.

func (Identity) Validate

func (i Identity) Validate() error

Validate checks the model name and bounded key and requires a canonical identity representation.

func (Identity) WithModelName

func (i Identity) WithModelName(name string) (Identity, error)

WithModelName returns the same stored key attributed to another model name. It is the explicit boundary for a declared storage identity that predates a table rename; parsing still decodes the key through the expected codec.

type KeyCodec

type KeyCodec[K any] interface {
	// Bind converts a typed key to a database driver value.
	Bind(K) (driver.Value, error)
	// Decode decodes a database value into a typed key.
	Decode(any) (K, error)
}

KeyCodec is the small persistence boundary implemented by database codecs. Generated references reuse the primary field's existing codec. Custom codecs must be pure, safe for concurrent use, and return owned decoded values.

type MorphName

type MorphName string

MorphName is the stored discriminator naming one model in polymorphic relationships. Store it in a model field of this type.

type Morphable

type Morphable interface {
	// MorphName returns the model's polymorphic name.
	MorphName() MorphName
}

Morphable is implemented by a model that takes part in polymorphic relationships. Declaring the name once on the model is the typed morph map: relationship constructors read it from the model type, so call sites never pass discriminator strings.

func (Post) MorphName() model.MorphName { return "post" }

type Reference

type Reference[M, K any] struct {
	// contains filtered or unexported fields
}

Reference retains a model and its concrete stored primary-key type. Generated FoundryReference methods supply its table, key and codec without calling getters. A reference does not hydrate a model or establish existence or authorization.

func NewReference

func NewReference[M, K any](name string, key K, codec KeyCodec[K]) Reference[M, K]

NewReference is the explicit generated metadata boundary. Applications normally obtain references from a model's FoundryReference method rather than repeating names or codecs. Identity freezes the key when transport metadata is needed.

func (Reference[M, K]) Format

func (Reference[M, K]) Format(state fmt.State, _ rune)

Format writes a fixed label without exposing the reference.

func (Reference[M, K]) Identity

func (r Reference[M, K]) Identity() (Identity, error)

Identity snapshots only the stored key through its database codec. Read getters and presentation JSON marshalers do not run. Custom codec failures are isolated without exposing panic payloads. SQL NULL is never a valid identity.

func (Reference[M, K]) Key

func (r Reference[M, K]) Key() K

Key returns the reference's typed key.

func (Reference[M, K]) ModelName

func (r Reference[M, K]) ModelName() string

ModelName returns the reference's model name.

func (Reference[M, K]) Parse

func (r Reference[M, K]) Parse(identity Identity) (Reference[M, K], error)

Parse restores a serialized identity using this reference's expected model and primary-key codec. Its current key is ignored. At a transport boundary use a generated zero model's FoundryReference().Parse(identity); no lookup occurs. Parsing validates metadata, never authentication, authorization or existence.

func (Reference[M, K]) Validate

func (r Reference[M, K]) Validate() error

Validate checks generated model/key metadata without encoding a key or doing I/O. The reference may hold a zero key, as it does when used to parse stored identities.

Jump to

Keyboard shortcuts

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