-
Notifications
You must be signed in to change notification settings - Fork 20
/
string_equal_to.go
63 lines (48 loc) · 1.31 KB
/
string_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
56
57
58
59
60
61
62
63
package condition
import (
"bytes"
"context"
"encoding/json"
"github.com/brexhq/substation/v2/config"
"github.com/brexhq/substation/v2/message"
)
func newStringEqualTo(_ context.Context, cfg config.Config) (*stringEqualTo, error) {
conf := stringConfig{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, err
}
insp := stringEqualTo{
conf: conf,
b: []byte(conf.Value),
}
return &insp, nil
}
type stringEqualTo struct {
conf stringConfig
b []byte
}
func (insp *stringEqualTo) 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.Equal(msg.Data(), compare), nil
}
val := msg.GetValue(insp.conf.Object.SourceKey)
// for gjson's GetValue, if the path is empty string (indicating source key or target key is not present),
// the Result.Exists() will return false
// If source or target key is present but value cannot be found, always return false
if !val.Exists() {
return false, nil
}
target := msg.GetValue(insp.conf.Object.TargetKey)
if target.Exists() {
compare = target.Bytes()
}
return bytes.Equal(val.Bytes(), compare), nil
}
func (c *stringEqualTo) String() string {
b, _ := json.Marshal(c.conf)
return string(b)
}