id

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package id is the identifiers of every entity: UUIDv7 values minted by the application before the domain sees them, typed per entity.

Each entity declares its identifier type in its domain package,

type ProjectID = id.Of[Project]

so passing one entity's identifier where another's is expected does not compile. Application handlers mint identifiers from a Source they receive, which reads the application clock: identifiers are time-ordered, so index inserts stay local and keyset cursors follow creation order. The creation time of a row is therefore readable from its key, which is not a secret.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Of

type Of[T any] struct {
	// contains filtered or unexported fields
}

Of is the identifier of an entity of type T. The zero value is no identifier.

Example

Identifiers are typed per entity, so passing one entity's identifier where another's is expected does not compile:

deleteFlag(flag, project) // cannot use flag (FlagID) as ProjectID
package main

import (
	"fmt"
	"time"

	"github.com/azverinus/flagnetic/internal/shared/clock"
	"github.com/azverinus/flagnetic/internal/shared/id"
)

type (
	Project struct{}
	Flag    struct{}

	ProjectID = id.Of[Project]
	FlagID    = id.Of[Flag]
)

func deleteFlag(project ProjectID, flag FlagID) string {
	return fmt.Sprintf("delete %s from %s", flag, project)
}

// Identifiers are typed per entity, so passing one entity's identifier where
// another's is expected does not compile:
//
//	deleteFlag(flag, project) // cannot use flag (FlagID) as ProjectID
func main() {
	src := id.NewDeterministicSource(clock.NewFake(time.UnixMilli(1469918264193)))
	project := id.New[Project](src)
	flag := id.New[Flag](src)

	fmt.Println(deleteFlag(project, flag))
}
Output:
delete 01563df4-bb81-7000-8000-000000000001 from 01563df4-bb81-7000-8000-000000000000

func FromBytes

func FromBytes[T any](b [16]byte) Of[T]

FromBytes returns the identifier held in b, as read from a uuid column.

func MustParse

func MustParse[T any](s string) Of[T]

MustParse is Parse for identifiers known to be well formed, such as those in tests; it panics on any other.

func New

func New[T any](src *Source) Of[T]

New mints an identifier of T from src.

func Parse

func Parse[T any](s string) (Of[T], error)

Parse reads the canonical hyphenated form, in either case.

func (Of[T]) Bytes

func (i Of[T]) Bytes() [16]byte

Bytes returns the identifier's 16 bytes, as written to a uuid column.

func (Of[T]) IsZero

func (i Of[T]) IsZero() bool

IsZero reports whether i is no identifier.

func (Of[T]) MarshalText

func (i Of[T]) MarshalText() ([]byte, error)

MarshalText writes the canonical form.

func (Of[T]) String

func (i Of[T]) String() string

String returns the canonical form: lowercase hyphenated hexadecimal.

func (Of[T]) Time

func (i Of[T]) Time() time.Time

Time returns the millisecond the identifier was minted at.

func (*Of[T]) UnmarshalText

func (i *Of[T]) UnmarshalText(b []byte) error

UnmarshalText reads the canonical form.

type Source

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

Source mints UUIDv7 values (RFC 9562) from a clock. Within one millisecond, and when the clock steps back, it increments the random part of the last value instead of drawing a new one, so one process's identifiers strictly increase. It is safe for concurrent use.

func NewDeterministicSource

func NewDeterministicSource(c clock.Clock) *Source

NewDeterministicSource returns a source for tests whose random bits start at zero in every millisecond, so the same clock readings always give the same identifiers. They are still valid UUIDv7 and still strictly increase.

func NewSource

func NewSource(c clock.Clock) *Source

NewSource returns a source drawing its random bits from crypto/rand.

Jump to

Keyboard shortcuts

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