-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
options.go
62 lines (55 loc) · 1.26 KB
/
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
package encoding
import "github.com/pkg/errors"
// EncodeOptions ...
type EncodeOptions struct {
// NoPadding, for encodings that can disable padding (base64, base32).
NoPadding bool
// Lowercase, for encodings that can be upper or lower case, use lowercase (base32).
Lowercase bool
}
// EncodeOption ...
type EncodeOption struct {
Apply func(*EncodeOptions)
Encodings []Encoding
Name string
}
func newEncodeOptions(opts []EncodeOption, encoding Encoding) (EncodeOptions, error) {
var options EncodeOptions
for _, o := range opts {
if !containsEncoding(o.Encodings, encoding) {
return options, errors.Errorf("invalid option: %s", o.Name)
}
o.Apply(&options)
}
return options, nil
}
// NoPadding ...
func NoPadding() EncodeOption {
apply := func(o *EncodeOptions) {
o.NoPadding = true
}
return EncodeOption{
Apply: apply,
Encodings: []Encoding{Base64, Base32},
Name: "no-padding",
}
}
// Lowercase ...
func Lowercase() EncodeOption {
apply := func(o *EncodeOptions) {
o.Lowercase = true
}
return EncodeOption{
Apply: apply,
Encodings: []Encoding{Base32},
Name: "lowercase",
}
}
func containsEncoding(encs []Encoding, enc Encoding) bool {
for _, e := range encs {
if e == enc {
return true
}
}
return false
}