-
Notifications
You must be signed in to change notification settings - Fork 20
/
uuid.go
78 lines (62 loc) · 1.91 KB
/
uuid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package uuids
import (
"fmt"
"math/rand"
"regexp"
"github.com/nyaruka/goflow/utils/random"
"github.com/gofrs/uuid"
)
// V4Regex matches a string containing a valid v4 UUID
var V4Regex = regexp.MustCompile(`[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}`)
// V4OnlyRegex matches a string containing only a valid v4 UUID
var V4OnlyRegex = regexp.MustCompile(`^` + V4Regex.String() + `$`)
// New returns a new v4 UUID
func New() UUID {
return currentGenerator.Next()
}
// IsV4 returns whether the given string contains only a valid v4 UUID
func IsV4(s string) bool {
return V4OnlyRegex.MatchString(s)
}
// UUID is a 36 character UUID
type UUID string
// Generator is something that can generate a UUID
type Generator interface {
Next() UUID
}
// defaultGenerator generates a random v4 UUID using a 3rd party library
type defaultGenerator struct{}
// Next returns the next random UUID
func (g defaultGenerator) Next() UUID {
u, err := uuid.NewV4()
if err != nil {
// if we can't generate a UUID.. we're done
panic(fmt.Sprintf("unable to generate UUID: %s", err))
}
return UUID(u.String())
}
// DefaultGenerator is the default generator for calls to NewUUID
var DefaultGenerator Generator = defaultGenerator{}
var currentGenerator = DefaultGenerator
// SetGenerator sets the generator used by UUID4()
func SetGenerator(generator Generator) {
currentGenerator = generator
}
// generates a seedable random v4 UUID using math/rand
type seededGenerator struct {
rnd *rand.Rand
}
// NewSeededGenerator creates a new seeded UUID4 generator from the given seed
func NewSeededGenerator(seed int64) Generator {
return &seededGenerator{rnd: random.NewSeededGenerator(seed)}
}
// Next returns the next random UUID
func (g *seededGenerator) Next() UUID {
u := uuid.UUID{}
if _, err := g.rnd.Read(u[:]); err != nil {
panic(err)
}
u.SetVersion(uuid.V4)
u.SetVariant(uuid.VariantRFC4122)
return UUID(u.String())
}