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 MustParse ¶
MustParse is Parse for identifiers known to be well formed, such as those in tests; it panics on any other.
func (Of[T]) MarshalText ¶
MarshalText writes the canonical form.
func (*Of[T]) UnmarshalText ¶
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 ¶
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.