forked from ovh/ldp-tail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.go
200 lines (188 loc) · 5 KB
/
match.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"fmt"
"log"
"regexp"
"strconv"
"strings"
)
var supportedMatchOperatorsMap = map[string]struct {
description string
descriptionNot string
}{
"present": {"Field %[1]q is present", "Field %[1]q is not present"},
"begin": {"Field %[1]q begins with %[2]v", "Field %[1]q does not begins with %[2]v"},
"contain": {"Field %[1]q contains %[2]v", "Field %[1]q does not contains %[2]v"},
"lt": {"Field %[1]q is less than %[2]v", "Field %[1]q is greater than or equal to %[2]v"},
"le": {"Field %[1]q is less than or equal to %[2]v", "Field %[1]q is greater than %[2]v"},
"eq": {"Field %[1]q is equal to %[2]v", "Field %[1]q is not equal to %[2]v"},
"ge": {"Field %[1]q is greater than or equal to %[2]v", "Field %[1]q is less than %[2]v"},
"gt": {"Field %[1]q is greater than %[2]v", "Field %[1]q is less than or equal to %[2]v"},
"regex": {"Field %[1]q match %[2]q", "Field %[1]q does not match %[2]q"},
}
var supportedMatchOperators = func() []string {
a := make([]string, 0, len(supportedMatchOperatorsMap))
for k := range supportedMatchOperatorsMap {
a = append(a, k)
}
return a
}()
type matchCriterion struct {
Key string
Operator string
Value interface{}
Not bool
}
func isValidMatchCriteria(criteria []matchCriterion) (bool, error) {
for k, m := range criteria {
if _, ok := supportedMatchOperatorsMap[m.Operator]; !ok {
return false, fmt.Errorf("invalid operator %q in %+v", m.Operator, m)
}
if m.Operator == "begin" {
if _, ok := m.Value.(string); !ok {
return false, fmt.Errorf("invalid value for operator 'begin' in %+v", m)
}
}
if m.Operator == "lt" || m.Operator == "le" || m.Operator == "ge" || m.Operator == "gt" {
f, err := toNumber(m.Value)
if err != nil {
return false, fmt.Errorf("invalid value for operator '%s' in %+v: %s", m.Operator, m, err.Error())
}
// Overwrite value
criteria[k].Value = f
}
if m.Operator == "regex" {
// Be sure it's a string
v, ok := m.Value.(string)
if !ok {
return false, fmt.Errorf("invalid value for operator 'regex' in %+v", m)
}
// Check regular expression
r, err := regexp.Compile(v)
if err != nil {
return false, fmt.Errorf("invalid regular expression in %+v: %s", m, err.Error())
}
// Overwrite value
criteria[k].Value = r
}
}
return true, nil
}
func match(value map[string]interface{}, criteria []matchCriterion) bool {
for _, m := range criteria {
v, ok := value[m.Key]
// Key is not present, don't match
if !ok && m.Operator != "present" {
return false
}
switch m.Operator {
case "present":
if !ok != m.Not {
return false
}
case "eq":
if (v != m.Value) != m.Not {
return false
}
case "begin":
vString, ok := v.(string)
if !ok {
// Stringify value
vString = fmt.Sprintf("%v", v)
}
if (!strings.HasPrefix(vString, m.Value.(string))) != m.Not {
return false
}
case "contain":
vString, ok := v.(string)
if !ok {
// Stringify value
vString = fmt.Sprintf("%v", v)
}
if (!strings.Contains(vString, m.Value.(string))) != m.Not {
return false
}
case "lt":
vFloat, err := toNumber(v)
if err != nil {
log.Printf("lt: incompatible value for comparison: %s", err.Error())
}
if (vFloat >= m.Value.(float64)) != m.Not {
return false
}
case "le":
vFloat, err := toNumber(v)
if err != nil {
log.Printf("lt: incompatible value for comparison: %s", err.Error())
}
if (vFloat > m.Value.(float64)) != m.Not {
return false
}
case "ge":
vFloat, err := toNumber(v)
if err != nil {
log.Printf("lt: incompatible value for comparison: %s", err.Error())
}
if (vFloat < m.Value.(float64)) != m.Not {
return false
}
case "gt":
vFloat, err := toNumber(v)
if err != nil {
log.Printf("lt: incompatible value for comparison: %s", err.Error())
}
if (vFloat <= m.Value.(float64)) != m.Not {
return false
}
case "regex":
vString, ok := v.(string)
if !ok {
// Stringify value
vString = fmt.Sprintf("%v", v)
}
if (!m.Value.(*regexp.Regexp).MatchString(vString)) != m.Not {
return false
}
default:
panic("Unhandled operator")
}
}
return true
}
func toNumber(v interface{}) (float64, error) {
switch value := v.(type) {
case string:
// Try to parse value as float64
f, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, fmt.Errorf("'%v' can't be parsed as a number", v)
}
return f, nil
case uint:
return float64(value), nil
case uint8:
return float64(value), nil
case uint16:
return float64(value), nil
case uint32:
return float64(value), nil
case uint64:
return float64(value), nil
case int:
return float64(value), nil
case int8:
return float64(value), nil
case int16:
return float64(value), nil
case int32:
return float64(value), nil
case int64:
return float64(value), nil
case float32:
return float64(value), nil
case float64:
return value, nil
default:
return 0, fmt.Errorf("can't parse type %T as a number", v)
}
}