-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
114 lines (90 loc) · 2.98 KB
/
generator.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package idgen
import (
"sync"
"github.com/pkg/errors"
)
// SafeGenerator generates a new ID or returns an error.
// Although receiving errors is highly unlikely, reading from random generator sources could in theory fail.
// Generator implementations should be safe by default.
type SafeGenerator interface {
Generate() (string, error)
}
// SafeGeneratorFunc generates a new ID or returns an error.
type SafeGeneratorFunc func() (string, error)
func (fn SafeGeneratorFunc) Generate() (string, error) {
return fn()
}
// Generator generates a new ID and panics in case of any error.
type Generator interface {
Generate() string
}
// GeneratorFunc generates a new ID and panics in case of any error.
type GeneratorFunc func() string
func (fn GeneratorFunc) Generate() string {
return fn()
}
// Must panics if a safe generator function returns with an error, otherwise it returns the generated ID.
func Must(id string, err error) string {
if err != nil {
panic(err)
}
return id
}
// ConstantGenerator will return the same ID over and over again.
// If no ID is configured, it will return an error.
type ConstantGenerator struct {
id string
}
// NewConstantGenerator returns a new ConstantGenerator.
func NewConstantGenerator(id string) *ConstantGenerator {
return &ConstantGenerator{id: id}
}
// Generate returns the same ID over and over again.
// If no ID is configured, it will return an error.
func (g *ConstantGenerator) Generate() (string, error) {
if g.id == "" {
return "", errors.New("no id is configured")
}
return g.id, nil
}
// NewGenerator turns a SafeGenerator into a generator that panics when an error occurs.
func NewGenerator(generator SafeGenerator) Generator {
return &mustGenerator{generator: generator}
}
// mustGenerator wraps another generator and delegates the ID generation to it.
// It panics if the delegated generator returns an error.
type mustGenerator struct {
generator SafeGenerator
}
// Generate panics if the underlying generator returns an error, otherwise it returns the generated ID.
func (g *mustGenerator) Generate() string {
if g.generator == nil {
panic("generator is not configured")
}
return Must(g.generator.Generate())
}
// ListGenerator will return an ID from a user-defined list.
// If no ID list is configured or if it reaches the end of the list, it will return an error.
// It is safe to use the ListGenerator in separate goroutines.
type ListGenerator struct {
ids []string
cur int
mu sync.Mutex
}
// NewListGenerator returns a new ListGenerator.
func NewListGenerator(ids []string) *ListGenerator {
return &ListGenerator{ids: ids}
}
// Generate returns an ID from a user-defined list.
// If no ID list is configured or if it reaches the end of the list, it will return an error.
// It is safe to call this method in separate goroutines.
func (g *ListGenerator) Generate() (string, error) {
g.mu.Lock()
defer g.mu.Unlock()
if len(g.ids) < g.cur+1 {
return "", errors.New("no more ids left")
}
id := g.ids[g.cur]
g.cur++
return id, nil
}