-
Notifications
You must be signed in to change notification settings - Fork 20
/
number_length_equal_to.go
55 lines (43 loc) · 1.18 KB
/
number_length_equal_to.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
package condition
import (
"context"
"encoding/json"
"github.com/brexhq/substation/v2/config"
"github.com/brexhq/substation/v2/message"
)
func newNumberLengthEqualTo(_ context.Context, cfg config.Config) (*numberLengthEqualTo, error) {
conf := numberLengthConfig{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, err
}
insp := numberLengthEqualTo{
conf: conf,
}
return &insp, nil
}
type numberLengthEqualTo struct {
conf numberLengthConfig
}
func (insp *numberLengthEqualTo) Condition(ctx context.Context, msg *message.Message) (output bool, err error) {
if msg.HasFlag(message.IsControl) {
return false, nil
}
if insp.conf.Object.SourceKey == "" {
llm := numberLengthMeasurement(msg.Data(), insp.conf.Measurement)
return insp.match(llm), nil
}
value := msg.GetValue(insp.conf.Object.SourceKey)
if value.IsArray() {
l := len(value.Array())
return insp.match(l), nil
}
llm := numberLengthMeasurement(value.Bytes(), insp.conf.Measurement)
return insp.match(llm), nil
}
func (c *numberLengthEqualTo) match(length int) bool {
return length == c.conf.Value
}
func (c *numberLengthEqualTo) String() string {
b, _ := json.Marshal(c.conf)
return string(b)
}