/
process.go
282 lines (256 loc) · 6.85 KB
/
process.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package process
import (
"context"
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/brexhq/substation/internal/errors"
)
// ByteInvalidFactoryConfig is used when an unsupported Byte is referenced in ByteFactory
const ByteInvalidFactoryConfig = errors.Error("ByteInvalidFactoryConfig")
// ChannelInvalidFactoryConfig is used when an unsupported Channel is referenced in ChannelFactory
const ChannelInvalidFactoryConfig = errors.Error("ChannelInvalidFactoryConfig")
// Config contains arbitrary JSON settings for Processors loaded via mapstructure.
type Config struct {
Type string
Settings map[string]interface{}
}
// Input is the default input setting for processors that accept a single JSON key. This can be overriden by each processor.
type Input struct {
Key string `mapstructure:"key"`
}
// Inputs is the default input setting for processors that accept multiple JSON keys. This can be overriden by each processor.
type Inputs struct {
Keys []string `mapstructure:"keys"`
}
// Output is the default output setting for processors that produce a single JSON key. This can be overriden by each processor.
type Output struct {
Key string `mapstructure:"key"`
}
// Byter is an interface for applying processors to bytes.
type Byter interface {
Byte(context.Context, []byte) ([]byte, error)
}
// Byte accepts an array of Byters and applies all processors to the data.
func Byte(ctx context.Context, byters []Byter, data []byte) ([]byte, error) {
var err error
for _, byter := range byters {
data, err = byter.Byte(ctx, data)
if err != nil {
return nil, err
}
}
return data, nil
}
// Channeler is an interface for applying processors to channels of bytes.
type Channeler interface {
Channel(context.Context, <-chan []byte) (<-chan []byte, error)
}
// Channel accepts a channel of bytes and applies all processors to data in the channel.
func Channel(ctx context.Context, channelers []Channeler, ch <-chan []byte) (<-chan []byte, error) {
var err error
for _, channeler := range channelers {
ch, err = channeler.Channel(ctx, ch)
if err != nil {
return nil, err
}
}
return ch, nil
}
// ReadOnlyChannel turns a write/read channel into a read-only channel.
func ReadOnlyChannel(ch chan []byte) <-chan []byte {
return ch
}
// ByterFactory loads a Byter from a Config. This is the recommended function for retrieving ready-to-use Byters.
func ByterFactory(cfg Config) (Byter, error) {
switch t := cfg.Type; t {
case "base64":
var p Base64
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "capture":
var p Capture
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "case":
var p Case
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "concat":
var p Concat
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "convert":
var p Convert
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "copy":
var p Copy
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "delete":
var p Delete
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "domain":
var p Domain
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "dynamodb":
var p DynamoDB
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "flatten":
var p Flatten
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "gzip":
var p Gzip
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "hash":
var p Hash
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "insert":
var p Insert
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "lambda":
var p Lambda
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "math":
var p Math
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "replace":
var p Replace
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "time":
var p Time
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "zip":
var p Zip
mapstructure.Decode(cfg.Settings, &p)
return p, nil
default:
return nil, fmt.Errorf("err retrieving %s from factory: %v", t, ByteInvalidFactoryConfig)
}
}
// ChannelerFactory loads Channeler from a Config. This is the recommended function for retrieving ready-to-use Channelers.
func ChannelerFactory(cfg Config) (Channeler, error) {
switch t := cfg.Type; t {
case "base64":
var p Base64
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "capture":
var p Capture
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "case":
var p Case
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "concat":
var p Concat
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "convert":
var p Convert
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "copy":
var p Copy
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "count":
var p Count
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "delete":
var p Delete
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "domain":
var p Domain
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "drop":
var p Drop
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "dynamodb":
var p DynamoDB
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "flatten":
var p Flatten
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "gzip":
var p Gzip
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "hash":
var p Hash
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "insert":
var p Insert
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "expand":
var p Expand
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "lambda":
var p Lambda
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "math":
var p Math
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "replace":
var p Replace
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "time":
var p Time
mapstructure.Decode(cfg.Settings, &p)
return p, nil
case "zip":
var p Zip
mapstructure.Decode(cfg.Settings, &p)
return p, nil
default:
return nil, fmt.Errorf("err retrieving %s from factory: %v", t, ChannelInvalidFactoryConfig)
}
}
// MakeAllByters accepts an array of Config and returns populated Byters. This a conveience function for loading many Byters.
func MakeAllByters(cfg []Config) ([]Byter, error) {
var byters []Byter
for _, c := range cfg {
byter, err := ByterFactory(c)
if err != nil {
return nil, err
}
byters = append(byters, byter)
}
return byters, nil
}
// MakeAllChannelers accepts an array of Config and returns populated Channelers. This a conveience function for loading many Channelers.
func MakeAllChannelers(cfg []Config) ([]Channeler, error) {
var channelers []Channeler
for _, c := range cfg {
channeler, err := ChannelerFactory(c)
if err != nil {
return nil, err
}
channelers = append(channelers, channeler)
}
return channelers, nil
}