This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomizer_opts.go
More file actions
140 lines (119 loc) · 2.98 KB
/
randomizer_opts.go
File metadata and controls
140 lines (119 loc) · 2.98 KB
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
package fastrand
import "strings"
type Engine interface {
Randomizer([]byte) []byte
RandomizerString(string) string
}
type FastEngine struct {
defaultLength int
minLength int
maxLength int
inputEncoding RandomizerEncoding
outputEncoding RandomizerEncoding
rangesEnabled bool
keywordChoicesEnabled bool
lengthChoicesEnabled bool
enabledKeywords map[string]bool
mailProviders []string
customCharsets map[string][]byte
customKeywords map[string]CustomKeywordGenerator
}
type Option func(*FastEngine)
func NewEngine(opts ...Option) *FastEngine {
enabledKeywords := make(map[string]bool, len(allKeywords))
for _, kw := range allKeywords {
enabledKeywords[kw] = true
}
e := &FastEngine{
defaultLength: 16,
minLength: 1,
maxLength: 99,
inputEncoding: RandomizerEncodingURL | RandomizerEncodingHTML,
outputEncoding: RandomizerEncodingNone,
rangesEnabled: true,
keywordChoicesEnabled: true,
lengthChoicesEnabled: true,
enabledKeywords: enabledKeywords,
mailProviders: SafeMailProviders,
customCharsets: make(map[string][]byte),
customKeywords: make(map[string]CustomKeywordGenerator),
}
for _, opt := range opts {
opt(e)
}
return e
}
func (e *FastEngine) Reset() {
freshEngine := NewEngine()
*e = *freshEngine
}
func WithDefaultLength(length int) Option {
return func(e *FastEngine) {
if length > 0 {
e.defaultLength = length
}
}
}
func WithMinLength(length int) Option {
return func(e *FastEngine) {
if length > 0 {
e.minLength = length
}
}
}
func WithMaxLength(length int) Option {
return func(e *FastEngine) {
if length > 0 {
e.maxLength = length
}
}
}
func WithDisabledKeywords(keywords ...string) Option {
return func(e *FastEngine) {
for _, kw := range keywords {
e.enabledKeywords[strings.ToUpper(kw)] = false
}
}
}
func WithMailProviders(providers []string) Option {
return func(e *FastEngine) {
if len(providers) > 0 {
e.mailProviders = providers
}
}
}
func WithCustomCharset(keyword string, charset []byte) Option {
return func(e *FastEngine) {
e.customCharsets[strings.ToUpper(keyword)] = charset
}
}
func WithCustomKeyword(keyword string, generator CustomKeywordGenerator) Option {
return func(e *FastEngine) {
e.customKeywords[strings.ToUpper(keyword)] = generator
}
}
func WithInputEncoding(encoding RandomizerEncoding) Option {
return func(e *FastEngine) {
e.inputEncoding = encoding
}
}
func WithOutputEncoding(encoding RandomizerEncoding) Option {
return func(e *FastEngine) {
e.outputEncoding = encoding
}
}
func WithRanges(enabled bool) Option {
return func(e *FastEngine) {
e.rangesEnabled = enabled
}
}
func WithKeywordChoices(enabled bool) Option {
return func(e *FastEngine) {
e.keywordChoicesEnabled = enabled
}
}
func WithLengthChoices(enabled bool) Option {
return func(e *FastEngine) {
e.lengthChoicesEnabled = enabled
}
}