/
meta_pipeline.go
131 lines (102 loc) · 3.26 KB
/
meta_pipeline.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
package transform
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"
)
// errMetaPipelineArrayInput is returned when the transform is configured to process
// an object, but the input is an array. Array values are not supported by this transform,
// instead the input should be run through the metaForEach transform (which can encapsulate
// the pipeline transform).
var errMetaPipelineArrayInput = fmt.Errorf("input is an array")
type metaPipelineConfig struct {
// Transforms that is applied in series to the data.
Transforms []config.Config `json:"transforms"`
Object iconfig.Object `json:"object"`
}
func (c *metaPipelineConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func (c *metaPipelineConfig) Validate() error {
if c.Object.SourceKey == "" && c.Object.TargetKey != "" {
return fmt.Errorf("object_source_key: %v", errors.ErrMissingRequiredOption)
}
if c.Object.SourceKey != "" && c.Object.TargetKey == "" {
return fmt.Errorf("object_target_key: %v", errors.ErrMissingRequiredOption)
}
if len(c.Transforms) == 0 {
return fmt.Errorf("transforms: %v", errors.ErrMissingRequiredOption)
}
return nil
}
func newMetaPipeline(ctx context.Context, cfg config.Config) (*metaPipeline, error) {
conf := metaPipelineConfig{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, fmt.Errorf("transform: meta_pipeline: %v", err)
}
if err := conf.Validate(); err != nil {
return nil, fmt.Errorf("transform: meta_pipeline: %v", err)
}
tf := metaPipeline{
conf: conf,
isObject: conf.Object.SourceKey != "" && conf.Object.TargetKey != "",
}
var tform []Transformer
for _, c := range conf.Transforms {
t, err := New(ctx, c)
if err != nil {
return nil, fmt.Errorf("transform: meta_pipeline: transform %+v: %v", c, err)
}
tform = append(tform, t)
}
tf.tf = tform
return &tf, nil
}
type metaPipeline struct {
conf metaPipelineConfig
isObject bool
tf []Transformer
}
func (tf *metaPipeline) Transform(ctx context.Context, msg *message.Message) ([]*message.Message, error) {
if msg.IsControl() {
msgs, err := Apply(ctx, tf.tf, msg)
if err != nil {
return nil, fmt.Errorf("transform: meta_pipeline: %v", err)
}
return msgs, nil
}
if !tf.isObject {
msgs, err := Apply(ctx, tf.tf, msg)
if err != nil {
return nil, fmt.Errorf("transform: meta_pipeline: %v", err)
}
return msgs, nil
}
value := msg.GetValue(tf.conf.Object.SourceKey)
if !value.Exists() {
return []*message.Message{msg}, nil
}
if value.IsArray() {
return nil, fmt.Errorf("transform: meta_pipeline: key %s: %v", tf.conf.Object.SourceKey, errMetaPipelineArrayInput)
}
res, err := Apply(ctx, tf.tf, message.New().SetData(value.Bytes()))
if err != nil {
return nil, fmt.Errorf("transform: meta_pipeline: %v", err)
}
var output []*message.Message
for _, msg := range res {
if err := msg.SetValue(tf.conf.Object.TargetKey, msg.Data()); err != nil {
return nil, fmt.Errorf("transform: meta_pipeline: %v", err)
}
output = append(output, msg)
}
return output, nil
}
func (tf *metaPipeline) String() string {
b, _ := json.Marshal(tf.conf)
return string(b)
}