-
Notifications
You must be signed in to change notification settings - Fork 15
/
generator_options.go
90 lines (78 loc) · 1.77 KB
/
generator_options.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
package vcgen
import (
"errors"
)
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
}
}