/
generator.go
269 lines (219 loc) · 5.11 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package vcgen
import (
"errors"
"math"
"math/rand"
"strings"
"time"
)
var (
// charset types
numbers = "0123456789"
alphabetic = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
alphanumeric = numbers + alphabetic
// defaults
minCount uint16 = 1
minLength uint16 = 6
)
const patternChar = "#"
var (
ErrNotFeasible = errors.New("Not feasible to generate requested number of codes")
ErrPatternIsNotMatch = errors.New("Pattern is not match with the length value")
)
// initialize random seed
func init() {
rand.Seed(time.Now().UnixNano())
}
type Generator struct {
// Length of the code
Length uint16 `json:"length"`
// Count of the codes
Count uint16 `json:"count"`
// Charset to use
Charset string `json:"charset"`
// Prefix of the code
Prefix string `json:"prefix"`
// Suffix of the code
Suffix string `json:"suffix"`
// Pattern of the code
Pattern string `json:"pattern"`
// Suffix of the code
//
// Deprecated: use Suffix instead
Postfix string `json:"postfix"`
}
// Creates a new generator with options
func NewWithOptions(opts ...Option) (*Generator, error) {
g := Default()
if err := setOptions(opts...)(g); err != nil {
return nil, err
}
return g, nil
}
// Creates a new generator with default values
func Default() *Generator {
return &Generator{
Length: minLength,
Count: minCount,
Charset: alphanumeric,
Pattern: repeatStr(minLength, patternChar),
}
}
// New generator config
//
// Deprecated: use NewWithOptions or Default instead
func New(i *Generator) *Generator {
// check functione entry args
if i == nil {
rps := repeatStr(minLength, "#")
// --
i = &Generator{
Length: minLength,
Count: minCount,
Charset: alphanumeric,
Pattern: rps,
}
goto AfterChecker
}
// check pattern
if i.Pattern == "" {
rps := repeatStr(i.Length, "#")
i.Pattern = rps
}
// check length
if i.Length == 0 {
i.Length = uint16(strings.Count(i.Pattern, "#"))
}
// check count
if i.Count == 0 {
i.Count = minCount
}
// check charset
if i.Charset == "" {
i.Charset = alphanumeric
}
AfterChecker:
// return
return i
}
// Generates a list of codes
func (g *Generator) Run() ([]string, error) {
if !isFeasible(g.Charset, g.Pattern, patternChar, g.Count) {
return nil, ErrNotFeasible
}
result := make([]string, g.Count)
var i uint16
for i = 0; i < g.Count; i++ {
result[i] = g.one()
}
return result, nil
}
// one generates one code
func (g *Generator) one() string {
pts := strings.Split(g.Pattern, "")
for i, v := range pts {
if v == patternChar {
pts[i] = randomChar([]byte(g.Charset))
}
}
suffix := g.Suffix
// TODO: Remove it when we deprecate Postfix
if g.Postfix != "" {
suffix = g.Postfix
}
return g.Prefix + strings.Join(pts, "") + suffix
}
var (
ErrInvalidCount = errors.New("invalid count, It should be greater than 0")
ErrInvalidCharset = errors.New("invalid charset, charset length should be greater than 0")
ErrInvalidPattern = errors.New("invalid pattern, pattern cannot be empty")
)
type Option func(*Generator) error
// SetLength sets the length of the code
func SetLength(length uint16) Option {
return func(g *Generator) error {
if length == 0 {
length = numberOfChar(g.Pattern, patternChar)
}
g.Length = length
return nil
}
}
// SetCount sets the count of the code
func SetCount(count uint16) Option {
return func(g *Generator) error {
if count == 0 {
return ErrInvalidCount
}
g.Count = count
return nil
}
}
// SetCharset sets the charset of the code
func SetCharset(charset string) Option {
return func(g *Generator) error {
if len(charset) == 0 {
return ErrInvalidCharset
}
g.Charset = charset
return nil
}
}
// SetPrefix sets the prefix of the code
func SetPrefix(prefix string) Option {
return func(g *Generator) error {
g.Prefix = prefix
return nil
}
}
// SetSuffix sets the suffix of the code
func SetSuffix(suffix string) Option {
return func(g *Generator) error {
g.Suffix = suffix
return nil
}
}
// SetPattern sets the pattern of the code
func SetPattern(pattern string) Option {
return func(g *Generator) error {
if pattern == "" {
return ErrInvalidPattern
}
numPatternChar := numberOfChar(pattern, patternChar)
if g.Length == 0 || g.Length != numPatternChar {
g.Length = numPatternChar
}
g.Pattern = pattern
return nil
}
}
func setOptions(opts ...Option) Option {
return func(g *Generator) error {
for _, opt := range opts {
if err := opt(g); err != nil {
return err
}
}
return nil
}
}
// HELPER
// return random int in the range min...max
func randomInt(min, max int) int {
return min + rand.Intn(1+max-min)
}
// return random char string from charset
func randomChar(cs []byte) string {
return string(cs[randomInt(0, len(cs)-1)])
}
// repeat string with one str (#)
func repeatStr(count uint16, str string) string {
return strings.Repeat(str, int(count))
}
func numberOfChar(str, char string) uint16 {
return uint16(strings.Count(str, char))
}
func isFeasible(charset, pattern, char string, count uint16) bool {
ls := numberOfChar(pattern, char)
return math.Pow(float64(len(charset)), float64(ls)) >= float64(count)
}