♻️ Modify UUID to allow for other versions.
This commit is contained in:
parent
f121b7dbce
commit
fee2e3cc2f
4 changed files with 55 additions and 24 deletions
|
|
@ -14,7 +14,7 @@ func NewConfig(options ...Option) Config {
|
||||||
conf := Config{
|
conf := Config{
|
||||||
extension: ".txt",
|
extension: ".txt",
|
||||||
separator: "_",
|
separator: "_",
|
||||||
generator: uuidGen,
|
generator: UUID(nil),
|
||||||
}
|
}
|
||||||
for _, opt := range options {
|
for _, opt := range options {
|
||||||
opt(&conf)
|
opt(&conf)
|
||||||
|
|
|
||||||
43
gen_rand.go
43
gen_rand.go
|
|
@ -8,17 +8,44 @@ import (
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func uuidGen(*Config) (string, error) {
|
// UUIDer is an interface for generating UUIDs.
|
||||||
u, err := uuid.NewRandom()
|
// It is recommended that you use either the UUIDv4 or UUIDv7 variables.
|
||||||
|
type UUIDer interface {
|
||||||
|
UUID() (uuid.UUID, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return u.String(), nil
|
return uu.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UUID generates a UUIDv4.
|
|
||||||
func UUID() Generator {
|
|
||||||
return uuidGen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type randConf struct {
|
type randConf struct {
|
||||||
|
|
|
||||||
|
|
@ -7,30 +7,34 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleUUID() {
|
func ExampleUUID() {
|
||||||
option := nomino.WithGenerator(nomino.UUID())
|
gen := nomino.UUID(nil)
|
||||||
|
|
||||||
str, _ := nomino.Make(nomino.NewConfig(option))
|
str, _ := gen.Make()
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
|
|
||||||
str, _ = nomino.Make(nomino.NewConfig(option))
|
str, _ = gen.Make()
|
||||||
|
fmt.Println(str)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleUUID_v7() {
|
||||||
|
gen := nomino.UUID(nomino.UUIDv7)
|
||||||
|
|
||||||
|
str, _ := gen.Make()
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
|
|
||||||
str, _ = nomino.Make(nomino.NewConfig(option))
|
str, _ = gen.Make()
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = gen.Make()
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleRandom() {
|
func ExampleRandom() {
|
||||||
option := nomino.WithGenerator(nomino.Random())
|
str, _ := nomino.Random().Make()
|
||||||
|
|
||||||
str, _ := nomino.Make(nomino.NewConfig(option))
|
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleRandomLength() {
|
func ExampleRandomLength() {
|
||||||
option := nomino.WithGenerator(nomino.Random(
|
str, _ := nomino.Random(nomino.RandomLength(32)).Make()
|
||||||
nomino.RandomLength(32),
|
|
||||||
))
|
|
||||||
|
|
||||||
str, _ := nomino.Make(nomino.NewConfig(option))
|
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUUID(t *testing.T) {
|
func TestUUID(t *testing.T) {
|
||||||
st, err := UUID()(nil)
|
st, err := UUID(nil)(nil)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
_, parseErr := uuid.Parse(st)
|
_, parseErr := uuid.Parse(st)
|
||||||
assert.NoError(t, parseErr)
|
assert.NoError(t, parseErr)
|
||||||
|
|
@ -25,7 +25,7 @@ func TestUUIDFail(t *testing.T) {
|
||||||
uuid.SetRand(badRead{})
|
uuid.SetRand(badRead{})
|
||||||
defer uuid.SetRand(nil)
|
defer uuid.SetRand(nil)
|
||||||
|
|
||||||
_, err := UUID()(nil)
|
_, err := UUID(nil)(nil)
|
||||||
assert.Equal(t, errors.New("sorry"), err)
|
assert.Equal(t, errors.New("sorry"), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue