valueobjects

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package valueobjects holds the strongly-typed identifiers and value types shared across flexitype's domain. Every internal identifier is a distinct ULID newtype so aggregates can never be cross-wired at compile time.

Index

Constants

View Source
const (
	DateFormat = "2006-01-02"
	TimeFormat = "15:04:05"
)

Wire formats for temporal values.

View Source
const DefaultTenant = TenantID("default")

DefaultTenant is the tenant used when a caller does not segment data.

Variables

This section is empty.

Functions

This section is empty.

Types

type AttributeDefinitionID

type AttributeDefinitionID struct {
	ulid.ID
}

AttributeDefinitionID identifies an attribute definition.

func MustParseAttributeDefinitionID

func MustParseAttributeDefinitionID(s string) AttributeDefinitionID

MustParseAttributeDefinitionID parses s, panicking on error.

func NewAttributeDefinitionID

func NewAttributeDefinitionID() AttributeDefinitionID

NewAttributeDefinitionID mints a new AttributeDefinitionID.

func ParseAttributeDefinitionID

func ParseAttributeDefinitionID(s string) (AttributeDefinitionID, error)

ParseAttributeDefinitionID parses a string into an AttributeDefinitionID.

func (AttributeDefinitionID) Equals

Equals checks if two AttributeDefinitionIDs are equal.

type AttributeValueID

type AttributeValueID struct {
	ulid.ID
}

AttributeValueID identifies a stored attribute value.

func MustParseAttributeValueID

func MustParseAttributeValueID(s string) AttributeValueID

MustParseAttributeValueID parses s, panicking on error.

func NewAttributeValueID

func NewAttributeValueID() AttributeValueID

NewAttributeValueID mints a new AttributeValueID.

func ParseAttributeValueID

func ParseAttributeValueID(s string) (AttributeValueID, error)

ParseAttributeValueID parses a string into an AttributeValueID.

func (AttributeValueID) Equals

func (id AttributeValueID) Equals(other AttributeValueID) bool

Equals checks if two AttributeValueIDs are equal.

type DataType

type DataType string

DataType enumerates the soft types an attribute definition can declare.

const (
	DataTypeBool     DataType = "bool"
	DataTypeString   DataType = "string"
	DataTypeInteger  DataType = "integer"
	DataTypeFloat    DataType = "float"
	DataTypeDecimal  DataType = "decimal"
	DataTypeDate     DataType = "date"
	DataTypeTime     DataType = "time"
	DataTypeDateTime DataType = "datetime"
	DataTypeEnum     DataType = "enum"
	DataTypeURL      DataType = "url"
	DataTypeEmail    DataType = "email"
	DataTypeJSON     DataType = "json"
	// DataTypeMedia stores a reference to a file/image in object storage:
	// the value is metadata (object key, mime, size, checksum, filename),
	// never the bytes.
	DataTypeMedia DataType = "media"
	// DataTypeQuantity is a magnitude with a unit (10 kg, 250 mm). Values
	// normalize to a base unit for comparison; the original unit is kept
	// for display.
	DataTypeQuantity DataType = "quantity"
)

The supported soft data types.

func ParseDataType

func ParseDataType(s string) (DataType, error)

ParseDataType validates a data type name.

func (DataType) IsOrdered

func (d DataType) IsOrdered() bool

IsOrdered reports whether values of this type have a total order and support min/max value and range constraints.

func (DataType) IsTemporal

func (d DataType) IsTemporal() bool

IsTemporal reports whether values of this type carry a timestamp and support dynamic (relative-time) defaults and conditions.

func (DataType) IsTextual

func (d DataType) IsTextual() bool

IsTextual reports whether values of this type are backed by text and support length/pattern constraints.

func (DataType) String

func (d DataType) String() string

String returns the data type name.

type Default

type Default struct {
	Static  *Value
	Dynamic *DynamicValue
}

Default declares an attribute's default: either a static typed value or a dynamic one resolved when the default is applied. Exactly one side is set.

func (Default) MarshalJSON

func (d Default) MarshalJSON() ([]byte, error)

MarshalJSON persists the static side in its self-describing typed form so the default survives storage round-trips.

func (Default) Resolve

func (d Default) Resolve(dt DataType, now time.Time) (Value, error)

Resolve produces the concrete default Value at instant now.

func (*Default) UnmarshalJSON

func (d *Default) UnmarshalJSON(b []byte) error

UnmarshalJSON is the inverse of MarshalJSON.

func (Default) Validate

func (d Default) Validate(dt DataType) error

Validate checks the default's shape against the attribute's data type.

type DependencyID

type DependencyID struct {
	ulid.ID
}

DependencyID identifies an attribute value dependency.

func MustParseDependencyID

func MustParseDependencyID(s string) DependencyID

MustParseDependencyID parses s, panicking on error.

func NewDependencyID

func NewDependencyID() DependencyID

NewDependencyID mints a new DependencyID.

func ParseDependencyID

func ParseDependencyID(s string) (DependencyID, error)

ParseDependencyID parses a string into a DependencyID.

func (DependencyID) Equals

func (id DependencyID) Equals(other DependencyID) bool

Equals checks if two DependencyIDs are equal.

type DynamicKind

type DynamicKind string

DynamicKind enumerates the runtime-computed value kinds.

const (
	// DynamicNow resolves to the current instant (datetime attributes).
	DynamicNow DynamicKind = "now"
	// DynamicToday resolves to the current date (date attributes).
	DynamicToday DynamicKind = "today"
	// DynamicRelative resolves to now offset by Amount * Period.
	DynamicRelative DynamicKind = "relative_time"
)

type DynamicValue

type DynamicValue struct {
	Kind   DynamicKind    `json:"kind"`
	Period RelativePeriod `json:"period,omitempty"`
	Amount int            `json:"amount,omitempty"`
}

DynamicValue is a value computed at evaluation time rather than stored: "now", "today", or "now plus/minus an offset". Used for temporal defaults and dynamic dependency conditions.

func (DynamicValue) Resolve

func (d DynamicValue) Resolve(dt DataType, now time.Time) (Value, error)

Resolve computes the concrete Value for the given data type at instant now.

func (DynamicValue) Validate

func (d DynamicValue) Validate(dt DataType) error

Validate checks the dynamic value's shape and its compatibility with the attribute's data type.

type EntityID

type EntityID string

EntityID anchors attribute values to the consumer's own domain object (a product, a part, a user, ...). It is deliberately an opaque string — consumers keep their identifier scheme; ULIDs are recommended.

func ParseEntityID

func ParseEntityID(s string) (EntityID, error)

ParseEntityID validates a consumer entity identifier.

func (EntityID) IsZero

func (e EntityID) IsZero() bool

IsZero reports whether the entity ID is unset.

func (EntityID) String

func (e EntityID) String() string

String returns the raw entity identifier.

type MediaMeta

type MediaMeta struct {
	ObjectKey string `json:"object_key"`
	MIME      string `json:"mime"`
	Size      int64  `json:"size"`
	Checksum  string `json:"checksum,omitempty"`
	Filename  string `json:"filename,omitempty"`
}

MediaMeta describes a stored file/image. The bytes live in object storage; this metadata is the attribute value.

type Quantity

type Quantity struct {
	Magnitude string  `json:"magnitude"`
	Unit      string  `json:"unit"`
	Base      float64 `json:"base"`
}

Quantity is a magnitude with a unit. Base is the magnitude converted to the family's base unit; comparisons and constraints use it, while Magnitude/Unit are kept for display and round-trip.

type RelationshipDefinitionID

type RelationshipDefinitionID struct {
	ulid.ID
}

RelationshipDefinitionID identifies a relationship definition.

func MustParseRelationshipDefinitionID

func MustParseRelationshipDefinitionID(s string) RelationshipDefinitionID

MustParseRelationshipDefinitionID parses s, panicking on error.

func NewRelationshipDefinitionID

func NewRelationshipDefinitionID() RelationshipDefinitionID

NewRelationshipDefinitionID mints a new RelationshipDefinitionID.

func ParseRelationshipDefinitionID

func ParseRelationshipDefinitionID(s string) (RelationshipDefinitionID, error)

ParseRelationshipDefinitionID parses a string into a RelationshipDefinitionID.

func (RelationshipDefinitionID) Equals

Equals checks if two RelationshipDefinitionIDs are equal.

type RelationshipID

type RelationshipID struct {
	ulid.ID
}

RelationshipID identifies one relationship instance (a link).

func MustParseRelationshipID

func MustParseRelationshipID(s string) RelationshipID

MustParseRelationshipID parses s, panicking on error.

func NewRelationshipID

func NewRelationshipID() RelationshipID

NewRelationshipID mints a new RelationshipID.

func ParseRelationshipID

func ParseRelationshipID(s string) (RelationshipID, error)

ParseRelationshipID parses a string into a RelationshipID.

func (RelationshipID) Equals

func (id RelationshipID) Equals(other RelationshipID) bool

Equals checks if two RelationshipIDs are equal.

type RelativePeriod

type RelativePeriod string

RelativePeriod is the unit for DynamicRelative offsets.

const (
	PeriodSeconds RelativePeriod = "seconds"
	PeriodMinutes RelativePeriod = "minutes"
	PeriodHours   RelativePeriod = "hours"
	PeriodDays    RelativePeriod = "days"
	PeriodWeeks   RelativePeriod = "weeks"
)

The supported relative periods.

type Scope

type Scope struct {
	Locale  string `json:"locale,omitempty"`
	Channel string `json:"channel,omitempty"`
}

Scope locates a value along optional presentation dimensions: a locale (en_AU, de_DE) and a channel/context (web, print, marketplace). The zero Scope is the base/unscoped value. A value's identity within an entity and attribute is (locale, channel), so one attribute can hold a different value per market and channel.

func (Scope) Equals

func (s Scope) Equals(o Scope) bool

Equals reports scope identity.

func (Scope) IsZero

func (s Scope) IsZero() bool

IsZero reports whether the scope is the base (unscoped) value.

type TenantID

type TenantID string

TenantID segments all data for multi-tenant deployments. It is a consumer controlled identifier, not a ULID, so hosted tiers can map it onto their own account model.

func ParseTenantID

func ParseTenantID(s string) (TenantID, error)

ParseTenantID validates a tenant identifier. Empty resolves to DefaultTenant.

func (TenantID) IsZero

func (t TenantID) IsZero() bool

IsZero reports whether the tenant is unset.

func (TenantID) String

func (t TenantID) String() string

String returns the raw tenant identifier.

type TypeDefinitionID

type TypeDefinitionID struct {
	ulid.ID
}

TypeDefinitionID identifies a type definition.

func MustParseTypeDefinitionID

func MustParseTypeDefinitionID(s string) TypeDefinitionID

MustParseTypeDefinitionID parses s, panicking on error.

func NewTypeDefinitionID

func NewTypeDefinitionID() TypeDefinitionID

NewTypeDefinitionID mints a new TypeDefinitionID.

func ParseTypeDefinitionID

func ParseTypeDefinitionID(s string) (TypeDefinitionID, error)

ParseTypeDefinitionID parses a string into a TypeDefinitionID.

func (TypeDefinitionID) Equals

func (id TypeDefinitionID) Equals(other TypeDefinitionID) bool

Equals checks if two TypeDefinitionIDs are equal.

type Value

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

Value is an immutable, strongly-typed attribute value: a tagged union across every DataType. Construct through the typed constructors or ParseValue; the zero Value means "absent".

func NewBoolValue

func NewBoolValue(b bool) Value

NewBoolValue builds a bool value.

func NewDateTimeValue

func NewDateTimeValue(t time.Time) Value

NewDateTimeValue builds a datetime value in UTC.

func NewDateValue

func NewDateValue(t time.Time) Value

NewDateValue builds a date value; the time-of-day portion is discarded.

func NewDecimalValue

func NewDecimalValue(s string) (Value, error)

NewDecimalValue builds an arbitrary-precision decimal from its canonical string form, e.g. "1234.560000".

func NewEmailValue

func NewEmailValue(s string) (Value, error)

NewEmailValue builds a validated email address value.

func NewEnumValue

func NewEnumValue(s string) Value

NewEnumValue builds an enum member value. Membership of the allowed set is enforced by the attribute definition's OneOf constraint.

func NewFloatValue

func NewFloatValue(f float64) Value

NewFloatValue builds a float value.

func NewIntegerValue

func NewIntegerValue(i int64) Value

NewIntegerValue builds an integer value.

func NewJSONValue

func NewJSONValue(raw json.RawMessage) (Value, error)

NewJSONValue builds a JSON document value, stored in compact form.

func NewMediaValue

func NewMediaValue(raw json.RawMessage) (Value, error)

NewMediaValue builds a media value from its metadata JSON. The object key, MIME type and a non-negative size are required — the caller (an upload handler) has already put the bytes in storage.

func NewQuantityValue

func NewQuantityValue(magnitude, unit string, base float64) (Value, error)

NewQuantityValue builds a quantity value. base is the magnitude expressed in the unit family's base unit (the caller resolves the conversion factor from the tenant's unit registry).

func NewStringValue

func NewStringValue(s string) Value

NewStringValue builds a string value.

func NewTimeValue

func NewTimeValue(t time.Time) Value

NewTimeValue builds a time-of-day value; the date portion is discarded.

func NewURLValue

func NewURLValue(s string) (Value, error)

NewURLValue builds a validated absolute URL value.

func ParseValue

func ParseValue(dt DataType, raw json.RawMessage) (Value, error)

ParseValue decodes a raw JSON scalar into a typed Value according to the declared data type. This is the single entry point for values arriving over the API.

func UnmarshalTypedValue

func UnmarshalTypedValue(raw json.RawMessage) (Value, error)

UnmarshalTypedValue decodes the self-describing form written by MarshalTyped.

func (Value) Bool

func (v Value) Bool() bool

Bool returns the boolean payload (bool values only).

func (Value) Compare

func (v Value) Compare(other Value) (int, error)

Compare orders two values of the same ordered data type (-1, 0, 1).

func (Value) DataType

func (v Value) DataType() DataType

DataType returns the value's declared type.

func (Value) Equal

func (v Value) Equal(other Value) bool

Equal reports whether two values have the same type and content. Decimals compare numerically ("1.50" equals "1.5").

func (Value) Float

func (v Value) Float() float64

Float returns the float payload (float values only).

func (Value) Int

func (v Value) Int() int64

Int returns the integer payload (integer values only).

func (Value) IsZero

func (v Value) IsZero() bool

IsZero reports whether the value is absent.

func (Value) JSON

func (v Value) JSON() json.RawMessage

JSON returns the raw document payload (json values only).

func (Value) Length

func (v Value) Length() int

Length is the rune count of textual values, used by length constraints.

func (Value) MarshalJSON

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

MarshalJSON renders the natural JSON form of the value.

func (Value) MarshalTyped

func (v Value) MarshalTyped() (json.RawMessage, error)

MarshalTyped encodes the value with its data type so it can be decoded without external context.

func (Value) Media

func (v Value) Media() MediaMeta

Media decodes the media metadata payload (media values only).

func (Value) Quantity

func (v Value) Quantity() Quantity

Quantity decodes the magnitude/unit payload and pairs it with the stored base (quantity values only).

func (Value) String

func (v Value) String() string

String renders the value for humans and logs.

func (Value) Text

func (v Value) Text() string

Text returns the textual payload (string, enum, url, email and decimal).

func (Value) Time

func (v Value) Time() time.Time

Time returns the temporal payload (date, time and datetime values).

Jump to

Keyboard shortcuts

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