49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package ids
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
)
|
|
|
|
// StableID is a transport-neutral opaque identifier primitive. Domain packages
|
|
// wrap it in strong semantic ID types; do not pass bare StableID across
|
|
// semantic boundaries.
|
|
type StableID string
|
|
|
|
// ID is retained as a compatibility name for the V1 primitive.
|
|
type ID = StableID
|
|
|
|
// Revision is the optimistic version of a versioned semantic record.
|
|
type Revision uint64
|
|
|
|
const InitialRevision Revision = 1
|
|
|
|
func (r Revision) Valid() bool { return r >= InitialRevision }
|
|
|
|
// SemanticRef is a technical cross-owner reference envelope. It identifies a
|
|
// record but does not grant storage ownership or provide generic CRUD.
|
|
type SemanticRef struct {
|
|
OwnerNamespace string
|
|
SemanticKind string
|
|
StableID StableID
|
|
Revision *Revision
|
|
}
|
|
|
|
func (r SemanticRef) Valid() bool {
|
|
return r.OwnerNamespace != "" && r.SemanticKind != "" && r.StableID != "" &&
|
|
(r.Revision == nil || r.Revision.Valid())
|
|
}
|
|
|
|
func NewStableID() (StableID, error) {
|
|
var b [16]byte
|
|
if _, err := rand.Read(b[:]); err != nil {
|
|
return "", fmt.Errorf("generate id: %w", err)
|
|
}
|
|
return StableID(hex.EncodeToString(b[:])), nil
|
|
}
|
|
|
|
func New() (ID, error) {
|
|
return NewStableID()
|
|
}
|