-
Notifications
You must be signed in to change notification settings - Fork 20
/
pattern.go
266 lines (243 loc) · 6.51 KB
/
pattern.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package quamina
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
)
type valType int
const (
stringType valType = iota
numberType
literalType
existsTrueType
existsFalseType
shellStyleType
anythingButType
prefixType
monocaseType
wildcardType
)
// typedVal represents the value of a field in a pattern, giving the value and the type of pattern.
// list is used to handle anything-but matches with multiple values.
type typedVal struct {
vType valType
val string
list [][]byte
}
// patternField represents a field in a pattern.
// vals is a list because field values are always given as a JSON array.
type patternField struct {
path string
vals []typedVal
}
// patternBuild tracks the progress of patternFromJSON through a pattern-compilation project.
type patternBuild struct {
jd *json.Decoder
path []string
results []*patternField
}
// patternFromJSON compiles a JSON text provided in jsonBytes into a list of patternField structures.
// I love naked returns and I cannot lie
func patternFromJSON(jsonBytes []byte) (fields []*patternField, err error) {
// we can't use json.Unmarshal because it round-trips numbers through float64 and %f, so they won't end up matching
// what the caller actually wrote in the patternField. json.Decoder is kind of slow due to excessive
// memory allocation, but I haven't got around to prematurely optimizing the patternFromJSON code path
var pb patternBuild
pb.jd = json.NewDecoder(bytes.NewReader(jsonBytes))
pb.jd.UseNumber()
// we use the tokenizer rather than pulling the pattern in with UnMarshall
t, err := pb.jd.Token()
if errors.Is(err, io.EOF) {
err = errors.New("empty Pattern")
return
} else if err != nil {
err = errors.New("patternField is not a JSON object" + err.Error())
return
}
switch tt := t.(type) {
case json.Delim:
if tt != '{' {
err = errors.New("patternField is not a JSON object")
return
}
default:
err = errors.New("event is not a JSON object: doesn't start with '{'")
return
}
err = readPatternObject(&pb)
fields = pb.results
return
}
func readPatternObject(pb *patternBuild) error {
for {
t, err := pb.jd.Token()
if errors.Is(err, io.EOF) {
return errors.New("event atEnd mid-object")
} else if err != nil {
return errors.New("pattern malformed: " + err.Error())
}
switch tt := t.(type) {
case string:
pb.path = append(pb.path, tt)
err = readPatternMember(pb)
if err != nil {
return err
}
pb.path = pb.path[:len(pb.path)-1]
case json.Delim:
// has to be '}' or the tokenizer would have thrown an error
return nil
}
}
}
func readPatternMember(pb *patternBuild) error {
t, err := pb.jd.Token()
if errors.Is(err, io.EOF) {
return errors.New("pattern ends mid-field")
} else if err != nil {
return errors.New("pattern malformed: " + err.Error())
}
switch tt := t.(type) {
case json.Delim:
switch tt {
case '[':
return readPatternArray(pb)
case '{':
return readPatternObject(pb)
default: // can't happen
return fmt.Errorf("pattern malformed, illegal %v", tt)
}
default:
return fmt.Errorf("pattern malformed, illegal %v", tt)
}
}
func readPatternArray(pb *patternBuild) error {
pathName := strings.Join(pb.path, SegmentSeparator)
var containsExclusive string
elementCount := 0
var pathVals []typedVal
for {
t, err := pb.jd.Token()
if errors.Is(err, io.EOF) {
return errors.New("patternField atEnd mid-field")
} else if err != nil {
// can't happen
return errors.New("pattern malformed: " + err.Error())
}
switch tt := t.(type) {
case json.Delim:
if tt == ']' {
if (containsExclusive != "") && (elementCount > 1) {
return fmt.Errorf(`%s cannot be combined with other values in pattern`, containsExclusive)
}
pb.results = append(pb.results, &patternField{path: pathName, vals: pathVals})
return nil
} else if tt == '{' {
var ce string
pathVals, ce, err = readSpecialPattern(pb, pathVals)
if ce != "" {
containsExclusive = ce
}
if err != nil {
return err
}
} else {
return fmt.Errorf("pattern malformed, illegal %v", tt)
}
case string:
pathVals = append(pathVals, typedVal{vType: stringType, val: `"` + tt + `"`})
case json.Number:
pathVals = append(pathVals, typedVal{vType: numberType, val: tt.String()})
case bool:
if tt {
pathVals = append(pathVals, typedVal{vType: literalType, val: "true"})
} else {
pathVals = append(pathVals, typedVal{vType: literalType, val: "false"})
}
case nil:
pathVals = append(pathVals, typedVal{vType: literalType, val: "null"})
}
elementCount++
}
}
func readSpecialPattern(pb *patternBuild, valsIn []typedVal) (pathVals []typedVal, containsExclusive string, err error) {
containsExclusive = ""
pathVals = valsIn
t, err := pb.jd.Token()
if err != nil {
return
}
// tokenizer will throw an error if it's not a string
tt := t.(string)
switch tt {
case "anything-but":
containsExclusive = tt
pathVals, err = readAnythingButSpecial(pb, pathVals)
case "exists":
containsExclusive = tt
pathVals, err = readExistsSpecial(pb, pathVals)
case "shellstyle":
pathVals, err = readShellStyleSpecial(pb, pathVals)
case "wildcard":
pathVals, err = readWildcardSpecial(pb, pathVals)
case "prefix":
pathVals, err = readPrefixSpecial(pb, pathVals)
case "equals-ignore-case":
pathVals, err = readMonocaseSpecial(pb, pathVals)
default:
err = errors.New("unrecognized in special pattern: " + tt)
}
return
}
func readPrefixSpecial(pb *patternBuild, valsIn []typedVal) (pathVals []typedVal, err error) {
t, err := pb.jd.Token()
if err != nil {
return
}
pathVals = valsIn
prefixString, ok := t.(string)
if !ok {
err = errors.New("value for 'prefix' must be a string")
return
}
val := typedVal{
vType: prefixType,
val: `"` + prefixString + `"`,
}
pathVals = append(pathVals, val)
// has to be } or tokenizer will throw error
_, err = pb.jd.Token()
return
}
func readExistsSpecial(pb *patternBuild, valsIn []typedVal) (pathVals []typedVal, err error) {
t, err := pb.jd.Token()
if err != nil {
return
}
pathVals = valsIn
switch tt := t.(type) {
case bool:
if tt {
pathVals = append(pathVals, typedVal{vType: existsTrueType})
} else {
pathVals = append(pathVals, typedVal{vType: existsFalseType})
}
default:
err = errors.New("value for 'exists' pattern must be true or false")
return
}
t, err = pb.jd.Token()
if err != nil {
return
}
switch t.(type) {
case json.Delim:
// no-op, has to be }
default:
err = errors.New("trailing garbage in 'existsMatches' pattern")
}
return
}