-
Notifications
You must be signed in to change notification settings - Fork 15
/
generator.go
100 lines (79 loc) · 2.02 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
package vcgen
import (
"errors"
"strings"
)
var (
ErrNotFeasible = errors.New("Not feasible to generate requested number of codes")
ErrPatternIsNotMatch = errors.New("Pattern is not match with the length value")
)
const (
// Charset types
CharsetNumbers = "0123456789"
CharsetAlphabetic = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
CharsetAlphanumeric = CharsetNumbers + CharsetAlphabetic
)
const (
// Default minimums
minCount uint16 = 1
minLength uint16 = 6
patternChar = "#"
)
type Generator struct {
// Length of the code to generate
Length uint16 `json:"length"`
// Count of the codes
// How many codes to generate
Count uint16 `json:"count"`
// Charset to use
// `CharsetNumbers`, `CharsetAlphabetic`, and `CharsetAlphanumeric`
// are already defined and you can use them.
Charset string `json:"charset"`
// Prefix of the code
Prefix string `json:"prefix"`
// Suffix of the code
Suffix string `json:"suffix"`
// Pattern of the code
// # is the placeholder for the charset
Pattern string `json:"pattern"`
}
// 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: CharsetAlphanumeric,
Pattern: repeatStr(minLength, patternChar),
}
}
// 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
return g.Prefix + strings.Join(pts, "") + suffix
}