-
Notifications
You must be signed in to change notification settings - Fork 782
/
text.go
355 lines (313 loc) · 8.46 KB
/
text.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package condition
import (
"bytes"
"errors"
"fmt"
"net"
"regexp"
"github.com/Jeffail/benthos/v3/internal/docs"
"github.com/Jeffail/benthos/v3/lib/log"
"github.com/Jeffail/benthos/v3/lib/metrics"
"github.com/Jeffail/benthos/v3/lib/types"
radix "github.com/armon/go-radix"
"github.com/spf13/cast"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeText] = TypeSpec{
constructor: NewText,
Status: docs.StatusDeprecated,
Footnotes: `
## Alternatives
Consider using the [bloblang](/docs/components/conditions/bloblang) condition
instead as it offers a wide range of text processing options. For example, the
following text condition:
` + "``` yaml" + `
check_field:
path: foo.bar
condition:
text:
operator: enum
arg:
- foo
- bar
` + "```" + `
Can instead be expressed with:
` + "``` yaml" + `
bloblang: '["foo","bar"].contains(foo.bar)'
` + "```" + ``,
FieldSpecs: docs.FieldSpecs{
docs.FieldCommon("operator", "An [operator](#operators) to apply."),
docs.FieldCommon("arg", "An argument to check against. For some operators this field not be required."),
partFieldSpec,
},
}
}
//------------------------------------------------------------------------------
// Errors for the text condition.
var (
ErrInvalidTextOperator = errors.New("invalid text operator type")
)
// TextConfig is a configuration struct containing fields for the text
// condition.
type TextConfig struct {
Operator string `json:"operator" yaml:"operator"`
Part int `json:"part" yaml:"part"`
Arg interface{} `json:"arg" yaml:"arg"`
}
// NewTextConfig returns a TextConfig with default values.
func NewTextConfig() TextConfig {
return TextConfig{
Operator: "equals_cs",
Part: 0,
Arg: "",
}
}
//------------------------------------------------------------------------------
type textOperator func(c []byte) bool
func textEqualsOperator(arg []byte) textOperator {
return func(c []byte) bool {
return bytes.Equal(c, arg)
}
}
func textEqualsFoldOperator(arg []byte) textOperator {
return func(c []byte) bool {
return bytes.EqualFold(c, arg)
}
}
func textContainsOperator(arg []byte) textOperator {
return func(c []byte) bool {
return bytes.Contains(c, arg)
}
}
func textContainsFoldOperator(arg []byte) textOperator {
argLower := bytes.ToLower(arg)
return func(c []byte) bool {
return bytes.Contains(bytes.ToLower(c), argLower)
}
}
func textContainsAnyOperator(arg interface{}) (textOperator, error) {
entries, err := cast.ToStringSliceE(arg)
if err != nil {
return nil, fmt.Errorf("failed to parse argument as string slice: %v", err)
}
var entriesBytes [][]byte
for _, entry := range entries {
entriesBytes = append(entriesBytes, []byte(entry))
}
return func(c []byte) bool {
for _, entry := range entriesBytes {
if bytes.Contains(c, entry) {
return true
}
}
return false
}, nil
}
func textContainsFoldAnyOperator(arg interface{}) (textOperator, error) {
entries, err := cast.ToStringSliceE(arg)
if err != nil {
return nil, fmt.Errorf("failed to parse argument as string slice: %v", err)
}
var entriesBytes [][]byte
for _, entry := range entries {
entriesBytes = append(entriesBytes, bytes.ToLower([]byte(entry)))
}
return func(c []byte) bool {
for _, entry := range entriesBytes {
if bytes.Contains(bytes.ToLower(c), entry) {
return true
}
}
return false
}, nil
}
func textPrefixOperator(arg []byte) textOperator {
return func(c []byte) bool {
return bytes.HasPrefix(c, arg)
}
}
func textPrefixFoldOperator(arg []byte) textOperator {
argLower := bytes.ToLower(arg)
return func(c []byte) bool {
return bytes.HasPrefix(bytes.ToLower(c), argLower)
}
}
func textSuffixOperator(arg []byte) textOperator {
return func(c []byte) bool {
return bytes.HasSuffix(c, arg)
}
}
func textSuffixFoldOperator(arg []byte) textOperator {
argLower := bytes.ToLower(arg)
return func(c []byte) bool {
return bytes.HasSuffix(bytes.ToLower(c), argLower)
}
}
func textRegexpPartialOperator(arg []byte) (textOperator, error) {
compiled, err := regexp.Compile(string(arg))
if err != nil {
return nil, err
}
return func(c []byte) bool {
return compiled.Match(c)
}, nil
}
func textRegexpExactOperator(arg []byte) (textOperator, error) {
compiled, err := regexp.Compile(string(arg))
if err != nil {
return nil, err
}
return func(c []byte) bool {
return len(compiled.Find(c)) == len(c)
}, nil
}
func textEnumOperator(arg interface{}) (textOperator, error) {
entries, err := cast.ToStringSliceE(arg)
if err != nil {
return nil, fmt.Errorf("failed to parse argument as string slice: %v", err)
}
tree := radix.New()
for _, entry := range entries {
tree.Insert(entry, struct{}{})
}
return func(c []byte) bool {
_, ok := tree.Get(string(c))
return ok
}, nil
}
func textIsOperatorArgIP() textOperator {
return func(c []byte) bool {
if err := net.ParseIP(string(c)); err != nil {
return true
}
return false
}
}
func textIsOperatorArgIPV4() textOperator {
return func(c []byte) bool {
if err := net.ParseIP(string(c)); err != nil && bytes.Contains(c, []byte(".")) {
return true
}
return false
}
}
func textIsOperatorArgIPV6() textOperator {
return func(c []byte) bool {
if err := net.ParseIP(string(c)); err != nil && bytes.Contains(c, []byte(":")) {
return true
}
return false
}
}
func textIsOperator(arg interface{}) (textOperator, error) {
str, ok := arg.(string)
if !ok {
return nil, fmt.Errorf("expected string as operator argument, received: %T", arg)
}
switch str {
case "ip":
return textIsOperatorArgIP(), nil
case "ipv4":
return textIsOperatorArgIPV4(), nil
case "ipv6":
return textIsOperatorArgIPV6(), nil
}
return nil, fmt.Errorf("invalid argument for 'is' operator: %s", str)
}
func strToTextOperator(str string, arg interface{}) (textOperator, error) {
bytesArgErr := func(ctor func([]byte) (textOperator, error)) (textOperator, error) {
str, ok := arg.(string)
if !ok {
return nil, fmt.Errorf("expected string as operator argument, received: %T", arg)
}
return ctor([]byte(str))
}
bytesArg := func(ctor func([]byte) textOperator) (textOperator, error) {
return bytesArgErr(func(b []byte) (textOperator, error) {
return ctor(b), nil
})
}
switch str {
case "equals_cs":
return bytesArg(textEqualsOperator)
case "equals":
return bytesArg(textEqualsFoldOperator)
case "contains_cs":
return bytesArg(textContainsOperator)
case "contains":
return bytesArg(textContainsFoldOperator)
case "contains_any":
return textContainsFoldAnyOperator(arg)
case "contains_any_cs":
return textContainsAnyOperator(arg)
case "is":
return textIsOperator(arg)
case "prefix_cs":
return bytesArg(textPrefixOperator)
case "prefix":
return bytesArg(textPrefixFoldOperator)
case "suffix_cs":
return bytesArg(textSuffixOperator)
case "suffix":
return bytesArg(textSuffixFoldOperator)
case "regexp_partial":
return bytesArgErr(textRegexpPartialOperator)
case "regexp_exact":
return bytesArgErr(textRegexpExactOperator)
case "enum":
return textEnumOperator(arg)
}
return nil, ErrInvalidTextOperator
}
//------------------------------------------------------------------------------
// Text is a condition that checks message text against logical operators.
type Text struct {
stats metrics.Type
operator textOperator
part int
mCount metrics.StatCounter
mTrue metrics.StatCounter
mFalse metrics.StatCounter
}
// NewText returns a Text condition.
func NewText(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
op, err := strToTextOperator(conf.Text.Operator, conf.Text.Arg)
if err != nil {
return nil, fmt.Errorf("operator '%v': %v", conf.Text.Operator, err)
}
return &Text{
stats: stats,
operator: op,
part: conf.Text.Part,
mCount: stats.GetCounter("count"),
mTrue: stats.GetCounter("true"),
mFalse: stats.GetCounter("false"),
}, nil
}
//------------------------------------------------------------------------------
// Check attempts to check a message part against a configured condition.
func (c *Text) Check(msg types.Message) bool {
c.mCount.Incr(1)
index := c.part
lParts := msg.Len()
if lParts == 0 {
c.mFalse.Incr(1)
return false
}
msgPart := msg.Get(index).Get()
if msgPart == nil {
c.mFalse.Incr(1)
return false
}
res := c.operator(msgPart)
if res {
c.mTrue.Incr(1)
} else {
c.mFalse.Incr(1)
}
return res
}
//------------------------------------------------------------------------------