-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
rules.go
91 lines (74 loc) · 2.25 KB
/
rules.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
package random
import (
"fmt"
"github.com/mitchellh/mapstructure"
)
// Rule to assert on string values.
type Rule interface {
// Pass should return true if the provided value passes any assertions this Rule is making.
Pass(value []rune) bool
// Type returns the name of the rule as associated in the registry
Type() string
}
// CharsetRule requires a certain number of characters from the specified charset.
type CharsetRule struct {
// CharsetRule is the list of rules that candidate strings must contain a minimum number of.
Charset runes `mapstructure:"charset" json:"charset"`
// MinChars indicates the minimum (inclusive) number of characters from the charset that should appear in the string.
MinChars int `mapstructure:"min-chars" json:"min-chars"`
}
// ParseCharset from the provided data map. The data map is expected to be parsed from HCL.
func ParseCharset(data map[string]interface{}) (rule Rule, err error) {
cr := &CharsetRule{}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Metadata: nil,
Result: cr,
DecodeHook: stringToRunesFunc,
})
if err != nil {
return nil, fmt.Errorf("unable to decode charset restriction: %w", err)
}
err = decoder.Decode(data)
if err != nil {
return nil, fmt.Errorf("failed to parse charset restriction: %w", err)
}
return *cr, nil
}
func (c CharsetRule) Type() string {
return "charset"
}
// Chars returns the charset that this rule is looking for.
func (c CharsetRule) Chars() []rune {
return c.Charset
}
func (c CharsetRule) MinLength() int {
return c.MinChars
}
// Pass returns true if the provided candidate string has a minimum number of chars in it.
// This adheres to the Rule interface
func (c CharsetRule) Pass(value []rune) bool {
if c.MinChars <= 0 {
return true
}
count := 0
for _, r := range value {
// charIn is sometimes faster than a map lookup because the data is so small
// This is being kept rather than converted to a map to keep the code cleaner,
// otherwise there would need to be additional parsing logic.
if charIn(r, c.Charset) {
count++
if count >= c.MinChars {
return true
}
}
}
return false
}
func charIn(search rune, charset []rune) bool {
for _, r := range charset {
if search == r {
return true
}
}
return false
}