-
Notifications
You must be signed in to change notification settings - Fork 14
/
meta_condition.go
79 lines (62 loc) · 1.57 KB
/
meta_condition.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
package condition
import (
"context"
"encoding/json"
"fmt"
"github.com/brexhq/substation/config"
iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/errors"
"github.com/brexhq/substation/message"
)
type metaConditionConfig struct {
// Condition used to inspect the message.
Condition Config `json:"condition"`
}
func (c *metaConditionConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func (c *metaConditionConfig) Validate() error {
if c.Condition.Operator == "" {
return fmt.Errorf("type: %v", errors.ErrMissingRequiredOption)
}
return nil
}
func newMetaCondition(ctx context.Context, cfg config.Config) (*metaCondition, error) {
conf := metaConditionConfig{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, err
}
if err := conf.Validate(); err != nil {
return nil, err
}
op, err := New(ctx, conf.Condition)
if err != nil {
return nil, err
}
meta := metaCondition{
conf: conf,
op: op,
}
return &meta, nil
}
type metaCondition struct {
conf metaConditionConfig
op Operator
}
func (c *metaCondition) Inspect(ctx context.Context, msg *message.Message) (output bool, err error) {
if msg.IsControl() {
return false, nil
}
// This inspector does not directly interpret data, instead the
// message is passed through and each configured inspector
// applies its own data interpretation.
match, err := c.op.Operate(ctx, msg)
if err != nil {
return false, err
}
return match, nil
}
func (c *metaCondition) String() string {
b, _ := json.Marshal(c.conf)
return string(b)
}