-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathstring_greater_than.go
56 lines (42 loc) · 1.08 KB
/
string_greater_than.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
package condition
import (
"bytes"
"context"
"encoding/json"
"github.com/brexhq/substation/v2/config"
"github.com/brexhq/substation/v2/message"
)
func newStringGreaterThan(_ context.Context, cfg config.Config) (*stringGreaterThan, error) {
conf := stringConfig{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, err
}
insp := stringGreaterThan{
conf: conf,
b: []byte(conf.Value),
}
return &insp, nil
}
type stringGreaterThan struct {
conf stringConfig
b []byte
}
func (insp *stringGreaterThan) Condition(ctx context.Context, msg *message.Message) (output bool, err error) {
if msg.HasFlag(message.IsControl) {
return false, nil
}
compare := insp.b
if insp.conf.Object.SourceKey == "" {
return bytes.Compare(msg.Data(), compare) > 0, nil
}
target := msg.GetValue(insp.conf.Object.TargetKey)
if target.Exists() {
compare = target.Bytes()
}
value := msg.GetValue(insp.conf.Object.SourceKey)
return bytes.Compare(value.Bytes(), compare) > 0, nil
}
func (c *stringGreaterThan) String() string {
b, _ := json.Marshal(c.conf)
return string(b)
}