model

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 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

func (ID[M]) IsZero

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

func (ID[M]) MarshalText

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

func (ID[M]) String

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

func (*ID[M]) UnmarshalJSON

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

func (*ID[M]) UnmarshalText

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

type Identifiable

type Identifiable interface{ 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)

func (Identity) IsZero

func (i Identity) IsZero() bool

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)

func (Identity) ModelName

func (i Identity) ModelName() string

func (*Identity) UnmarshalJSON

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

func (Identity) Validate

func (i Identity) Validate() error

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(K) (driver.Value, error)
	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() 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)

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

func (Reference[M, K]) ModelName

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

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