-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
66 lines (55 loc) · 1.49 KB
/
config.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
package redact
type monitor interface {
Redacted(count int)
}
type configuration struct {
MaxLength int
BufferSize int
Monitor monitor
}
func New(options ...option) *Redactor {
var config configuration
Options.apply(options...)(&config)
matched := &matched{
used: make([]bool, config.MaxLength),
matches: make([]match, 0, config.BufferSize),
}
return &Redactor{
matched: matched,
phone: &phoneRedaction{matched: matched},
ssn: &ssnRedaction{matched: matched},
credit: &creditCardRedaction{matched: matched},
dob: &dobRedaction{matched: matched},
email: &emailRedaction{matched: matched},
monitor: config.Monitor,
maxLength: config.MaxLength,
}
}
var Options singleton
type singleton struct{}
type option func(*configuration)
func (singleton) MaxLength(value int) option {
return func(this *configuration) { this.MaxLength = value }
}
func (singleton) BufferSize(value int) option {
return func(this *configuration) { this.BufferSize = value }
}
func (singleton) Monitor(value monitor) option {
return func(this *configuration) { this.Monitor = value }
}
func (singleton) apply(options ...option) option {
return func(this *configuration) {
for _, option := range Options.defaults(options...) {
option(this)
}
}
}
func (singleton) defaults(options ...option) []option {
return append([]option{
Options.MaxLength(512),
Options.BufferSize(16),
Options.Monitor(nop{}),
}, options...)
}
type nop struct{}
func (nop) Redacted(int) {}