-
Notifications
You must be signed in to change notification settings - Fork 14
/
meta_switch.go
140 lines (114 loc) · 2.98 KB
/
meta_switch.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
package transform
import (
"context"
"encoding/json"
"fmt"
"github.com/brexhq/substation/condition"
"github.com/brexhq/substation/config"
iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/errors"
"github.com/brexhq/substation/message"
)
type metaSwitchConfig struct {
// Cases are the transforms that are conditionally applied. If
// no condition is configured, then the transform is always
// applied.
Cases []struct {
Condition condition.Config `json:"condition"`
Transform config.Config `json:"transform"`
} `json:"cases"`
}
func (c *metaSwitchConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func (c *metaSwitchConfig) Validate() error {
if len(c.Cases) == 0 {
return fmt.Errorf("cases: %v", errors.ErrMissingRequiredOption)
}
return nil
}
func newMetaSwitch(ctx context.Context, cfg config.Config) (*metaSwitch, error) {
conf := metaSwitchConfig{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, fmt.Errorf("transform: meta_switch: %v", err)
}
if err := conf.Validate(); err != nil {
return nil, fmt.Errorf("transform: meta_switch: %v", err)
}
var conditional []struct {
op condition.Operator
tf Transformer
}
for _, s := range conf.Cases {
op, err := condition.New(ctx, s.Condition)
if err != nil {
return nil, fmt.Errorf("transform: meta_switch: %v", err)
}
tf, err := New(ctx, s.Transform)
if err != nil {
return nil, fmt.Errorf("transform: meta_switch: %v", err)
}
conditional = append(conditional, struct {
op condition.Operator
tf Transformer
}{
op: op,
tf: tf,
})
}
tf := metaSwitch{
conf: conf,
conditional: conditional,
}
return &tf, nil
}
type metaSwitch struct {
conf metaSwitchConfig
conditional []struct {
op condition.Operator
tf Transformer
}
}
func (tf *metaSwitch) Transform(ctx context.Context, msg *message.Message) ([]*message.Message, error) {
if msg.IsControl() {
var messages []*message.Message
for _, c := range tf.conditional {
res, err := c.tf.Transform(ctx, msg)
if err != nil {
return nil, fmt.Errorf("transform: meta_pipeline: %v", err)
}
messages = append(messages, res...)
}
// This is required to deduplicate the control messages that
// were sent to the conditional transforms.
var msgs []*message.Message
for _, m := range messages {
if m.IsControl() {
continue
}
msgs = append(msgs, m)
}
msgs = append(msgs, msg)
return msgs, nil
}
for _, c := range tf.conditional {
ok, err := c.op.Operate(ctx, msg)
if err != nil {
return nil, fmt.Errorf("transform: meta_switch: %v", err)
}
if !ok {
continue
}
msgs, err := c.tf.Transform(ctx, msg)
if err != nil {
return nil, fmt.Errorf("transform: meta_switch: %v", err)
}
return msgs, nil
}
// If no conditions match, then return the original message.
return []*message.Message{msg}, nil
}
func (tf *metaSwitch) String() string {
b, _ := json.Marshal(tf.conf)
return string(b)
}