-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmatcher.go
91 lines (86 loc) · 2.26 KB
/
matcher.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 herd
import (
"fmt"
"reflect"
"regexp"
"strconv"
)
type MatchAttribute struct {
Name string
FuzzyTyping bool
Negate bool
Regex bool
Value interface{}
}
func (m MatchAttribute) String() string {
c1, c2 := '=', '='
if m.FuzzyTyping {
c1, c2 = '≈', '≈'
}
if m.Negate {
c1 = '!'
}
if m.Regex {
c2 = '~'
}
return fmt.Sprintf("%v %c%c %v", m.Name, c1, c2, m.Value)
}
func (m MatchAttribute) Match(value interface{}) (matches bool) {
defer func() {
if m.Negate {
matches = !matches
}
}()
if svalue := reflect.ValueOf(value); svalue.Kind() == reflect.Slice {
// Here we ignore Negate to make sure we filter for any/none matching
mx := MatchAttribute{Name: m.Name, Value: m.Value, FuzzyTyping: m.FuzzyTyping, Regex: m.Regex}
for i := 0; i < svalue.Len(); i++ {
if mx.Match(svalue.Index(i).Interface()) {
return true
}
}
return false
}
if m.Value == value {
return true
}
if m.Regex {
svalue, ok := value.(string)
return ok && m.Value.(*regexp.Regexp).MatchString(svalue)
}
if m.FuzzyTyping {
if bvalue, ok := value.(bool); ok && (m.Value == "true" || m.Value == "false") {
return bvalue == (m.Value == "true")
}
if m.Value == "nil" {
return value == nil
}
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
myival, err := strconv.ParseInt(m.Value.(string), 0, 64)
if err != nil {
return false
}
return v.Int() == myival
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
myival, err := strconv.ParseUint(m.Value.(string), 0, 64)
if err != nil {
return false
}
return v.Uint() == myival
}
}
// Let's be gentle on all the int types in attributes
if myival, ok := m.Value.(int64); ok {
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == myival
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return int64(v.Uint()) == myival // nolint:gosec // FIXME I need to take another look at this. Security implications are limited as this only affects which hosts to match.
}
}
return false
}
type MatchAttributes []MatchAttribute