-
Notifications
You must be signed in to change notification settings - Fork 782
/
number.go
176 lines (147 loc) · 4.12 KB
/
number.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
package condition
import (
"errors"
"fmt"
"strconv"
"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"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeNumber] = TypeSpec{
constructor: NewNumber,
Status: docs.StatusDeprecated,
Footnotes: `
## Alternatives
Consider using the [bloblang](/docs/components/conditions/bloblang) condition
instead as it offers a wide range of number processing options. For example, the
following condition:
` + "``` yaml" + `
check_interpolation:
value: ${!batch_size()}
condition:
number:
operator: greater_than
arg: 1
` + "```" + `
Can instead be expressed with:
` + "``` yaml" + `
bloblang: 'batch_size() > 1'
` + "```" + ``,
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 number condition.
var (
ErrInvalidNumberOperator = errors.New("invalid number operator type")
)
// NumberConfig is a configuration struct containing fields for the number
// condition.
type NumberConfig struct {
Operator string `json:"operator" yaml:"operator"`
Part int `json:"part" yaml:"part"`
Arg float64 `json:"arg" yaml:"arg"`
}
// NewNumberConfig returns a NumberConfig with default values.
func NewNumberConfig() NumberConfig {
return NumberConfig{
Operator: "equals",
Part: 0,
Arg: 0,
}
}
//------------------------------------------------------------------------------
type numberOperator func(c float64) bool
func numberEqualsOperator(arg float64) numberOperator {
return func(c float64) bool {
return arg == c
}
}
func numberGreaterThanOperator(arg float64) numberOperator {
return func(c float64) bool {
return c > arg
}
}
func numberLessThanOperator(arg float64) numberOperator {
return func(c float64) bool {
return c < arg
}
}
func strToNumberOperator(str string, arg float64) (numberOperator, error) {
switch str {
case "equals":
return numberEqualsOperator(arg), nil
case "greater_than":
return numberGreaterThanOperator(arg), nil
case "less_than":
return numberLessThanOperator(arg), nil
}
return nil, ErrInvalidNumberOperator
}
//------------------------------------------------------------------------------
// Number is a condition that checks messages interpretted as numbers against
// logical operators.
type Number struct {
stats metrics.Type
operator numberOperator
part int
log log.Modular
mCount metrics.StatCounter
mTrue metrics.StatCounter
mFalse metrics.StatCounter
}
// NewNumber returns a number condition.
func NewNumber(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
op, err := strToNumberOperator(conf.Number.Operator, conf.Number.Arg)
if err != nil {
return nil, fmt.Errorf("operator '%v': %v", conf.Number.Operator, err)
}
return &Number{
stats: stats,
operator: op,
part: conf.Number.Part,
log: log,
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 *Number) 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
}
floatVal, err := strconv.ParseFloat(string(msgPart), 64)
if err != nil {
c.log.Debugf("Failed to parse message as number: %v\n", err)
c.mFalse.Incr(1)
return false
}
res := c.operator(floatVal)
if res {
c.mTrue.Incr(1)
} else {
c.mFalse.Incr(1)
}
return res
}
//------------------------------------------------------------------------------