forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
constructor.go
268 lines (229 loc) · 7.61 KB
/
constructor.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
// Copyright (c) 2018 Ashley Jeffs
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package condition
import (
"bytes"
"encoding/json"
"sort"
"strings"
"github.com/Jeffail/benthos/lib/types"
"github.com/Jeffail/benthos/lib/util/service/log"
"github.com/Jeffail/benthos/lib/util/service/metrics"
)
//------------------------------------------------------------------------------
// TypeSpec Constructor and a usage description for each condition type.
type TypeSpec struct {
constructor func(
conf Config,
mgr types.Manager,
log log.Modular,
stats metrics.Type,
) (Type, error)
description string
}
// Constructors is a map of all condition types with their specs.
var Constructors = map[string]TypeSpec{}
//------------------------------------------------------------------------------
// Config is the all encompassing configuration struct for all condition types.
type Config struct {
Type string `json:"type" yaml:"type"`
And AndConfig `json:"and" yaml:"and"`
Content ContentConfig `json:"content" yaml:"content"`
JMESPath JMESPathConfig `json:"jmespath" yaml:"jmespath"`
Not NotConfig `json:"not" yaml:"not"`
Or OrConfig `json:"or" yaml:"or"`
Resource string `json:"resource" yaml:"resource"`
Xor XorConfig `json:"xor" yaml:"xor"`
}
// NewConfig returns a configuration struct fully populated with default values.
func NewConfig() Config {
return Config{
Type: "content",
And: NewAndConfig(),
Content: NewContentConfig(),
JMESPath: NewJMESPathConfig(),
Not: NewNotConfig(),
Or: NewOrConfig(),
Resource: "",
Xor: NewXorConfig(),
}
}
// SanitiseConfig returns a sanitised version of the Config, meaning sections
// that aren't relevant to behaviour are removed.
func SanitiseConfig(conf Config) (interface{}, error) {
cBytes, err := json.Marshal(conf)
if err != nil {
return nil, err
}
hashMap := map[string]interface{}{}
if err = json.Unmarshal(cBytes, &hashMap); err != nil {
return nil, err
}
outputMap := map[string]interface{}{}
outputMap["type"] = hashMap["type"]
outputMap[conf.Type] = hashMap[conf.Type]
return outputMap, nil
}
//------------------------------------------------------------------------------
// UnmarshalJSON ensures that when parsing configs that are in a slice the
// default values are still applied.
func (m *Config) UnmarshalJSON(bytes []byte) error {
type confAlias Config
aliased := confAlias(NewConfig())
if err := json.Unmarshal(bytes, &aliased); err != nil {
return err
}
*m = Config(aliased)
return nil
}
// UnmarshalYAML ensures that when parsing configs that are in a slice the
// default values are still applied.
func (m *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
type confAlias Config
aliased := confAlias(NewConfig())
if err := unmarshal(&aliased); err != nil {
return err
}
*m = Config(aliased)
return nil
}
//------------------------------------------------------------------------------
var header = "This document was generated with `benthos --list-conditions`" + `
Within the list of Benthos [processors][0] you will find the [condition][1]
processor, which applies a condition to every message and only propagates them
if the condition passes. Conditions themselves can modify ('not') and combine
('and', 'or') other conditions, and can therefore be used to create complex
filters.
The format of a condition is similar to other Benthos types:
` + "``` yaml" + `
condition:
type: content
content:
operator: equals
part: 0
arg: hello world
` + "```" + `
And using boolean condition types we can combine multiple conditions together:
` + "``` yaml" + `
condition:
type: and
and:
- type: content
content:
operator: contains
arg: hello world
- type: or
or:
- type: content
content:
operator: contains
arg: foo
- type: not
not:
type: content
content:
operator: contains
arg: bar
` + "```" + `
The above example could be summarised as 'content contains "hello world" and
also either contains "foo" or does _not_ contain "bar"'.
Conditions can be extremely useful for creating filters on an output. By using a
fan out output broker with 'condition' processors on the brokered outputs it is
possible to build curated data streams that filter on the content of each
message.
Here is an example config, where we have an output that receives only 'foo'
messages, an output that receives only 'bar' messages, and a third output that
receives everything:
` + "``` yaml" + `
output:
type: broker
broker:
pattern: fan_out
outputs:
- type: file
file:
path: ./foo.txt
processors:
- type: condition
condition:
type: content
content:
operator: contains
part: 0
arg: foo
- type: file
file:
path: ./bar.txt
processors:
- type: condition
condition:
type: content
content:
operator: contains
part: 0
arg: bar
- type: file
file:
path: ./everything.txt
` + "```" + `
Sometimes large chunks of logic are reused across processors, or nested multiple
times as branches of a larger condition. It is possible to avoid writing
duplicate condition configs by using the [resource condition][2].`
var footer = `
[0]: ../processors/README.md
[1]: ../processors/README.md#condition
[2]: #resource`
// Descriptions returns a formatted string of collated descriptions of each
// type.
func Descriptions() string {
// Order our buffer types alphabetically
names := []string{}
for name := range Constructors {
names = append(names, name)
}
sort.Strings(names)
buf := bytes.Buffer{}
buf.WriteString("CONDITIONS\n")
buf.WriteString(strings.Repeat("=", 10))
buf.WriteString("\n\n")
buf.WriteString(header)
buf.WriteString("\n\n")
// Append each description
for i, name := range names {
buf.WriteString("## ")
buf.WriteString("`" + name + "`")
buf.WriteString("\n")
buf.WriteString(Constructors[name].description)
buf.WriteString("\n")
if i != (len(names) - 1) {
buf.WriteString("\n")
}
}
buf.WriteString(footer)
return buf.String()
}
// New creates a condition type based on a condition configuration.
func New(conf Config, mgr types.Manager, log log.Modular, stats metrics.Type) (Type, error) {
if c, ok := Constructors[conf.Type]; ok {
return c.constructor(conf, mgr, log, stats)
}
return nil, types.ErrInvalidConditionType
}
//------------------------------------------------------------------------------