♻️ Modify UUID to allow for other versions.

This commit is contained in:
Dan Jones 2025-03-16 12:38:36 -05:00
commit fee2e3cc2f
4 changed files with 55 additions and 24 deletions

View file

@ -8,17 +8,44 @@ import (
"github.com/google/uuid"
)
func uuidGen(*Config) (string, error) {
u, err := uuid.NewRandom()
if err != nil {
return "", err
}
return u.String(), nil
// UUIDer is an interface for generating UUIDs.
// It is recommended that you use either the UUIDv4 or UUIDv7 variables.
type UUIDer interface {
UUID() (uuid.UUID, error)
}
// UUID generates a UUIDv4.
func UUID() Generator {
return uuidGen
// UUIDFunc is a function that generates a UUID.
type UUIDFunc func() (uuid.UUID, error)
// UUID allows UUIDFunc to be used as a UUIDer.
func (u UUIDFunc) UUID() (uuid.UUID, error) {
return u()
}
var (
// UUIDv1. You probably don't want to use this. It is included for completeness sake.
UUIDv1 = UUIDFunc(uuid.NewUUID)
// UUIDv4 is the default.
UUIDv4 = UUIDFunc(uuid.NewRandom)
// UUIDv6 is primarily a replacement for UUIDv1. You probably should use 4 or 7.
UUIDv6 = UUIDFunc(uuid.NewV6)
// UUIDv7 should be used if you want it sortable by time.
UUIDv7 = UUIDFunc(uuid.NewV7)
)
// UUID generates a UUID. If nil.is passed as an argument,
// a UUIDv4 is generated.
func UUID(u UUIDer) Generator {
if u == nil {
u = UUIDv4
}
return func(*Config) (string, error) {
uu, err := u.UUID()
if err != nil {
return "", err
}
return uu.String(), nil
}
}
type randConf struct {