forked from benthosdev/benthos
/
docs.go
302 lines (256 loc) · 8.65 KB
/
docs.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package query
// ExampleSpec provides a mapping example and some input/output results to
// display.
type ExampleSpec struct {
Mapping string `json:"mapping"`
Summary string `json:"summary"`
Results [][2]string `json:"results"`
SkipTesting bool `json:"skip_testing"`
}
// NewExampleSpec creates a new example spec.
func NewExampleSpec(summary, mapping string, results ...string) ExampleSpec {
structuredResults := make([][2]string, 0, len(results)/2)
for i, res := range results {
if i%2 == 1 {
structuredResults = append(structuredResults, [2]string{results[i-1], res})
}
}
return ExampleSpec{
Mapping: mapping,
Summary: summary,
Results: structuredResults,
}
}
// NewNotTestedExampleSpec creates a new not tested example spec.
func NewNotTestedExampleSpec(summary, mapping string, results ...string) ExampleSpec {
structuredResults := make([][2]string, 0, len(results)/2)
for i, res := range results {
if i%2 == 1 {
structuredResults = append(structuredResults, [2]string{results[i-1], res})
}
}
return ExampleSpec{
Mapping: mapping,
Summary: summary,
Results: structuredResults,
SkipTesting: true,
}
}
//------------------------------------------------------------------------------
// Status of a function or method.
type Status string
// Component statuses.
var (
StatusStable Status = "stable"
StatusBeta Status = "beta"
StatusExperimental Status = "experimental"
StatusDeprecated Status = "deprecated"
StatusHidden Status = "hidden"
)
//------------------------------------------------------------------------------
// Function categories.
var (
FunctionCategoryGeneral = "General"
FunctionCategoryMessage = "Message Info"
FunctionCategoryEnvironment = "Environment"
FunctionCategoryDeprecated = "Deprecated"
FunctionCategoryPlugin = "Plugin"
FunctionCategoryFakeData = "Fake Data Generation"
)
// FunctionSpec describes a Bloblang function.
type FunctionSpec struct {
// The release status of the function.
Status Status `json:"status"`
// A category to place the function within.
Category string `json:"category"`
// Name of the function (as it appears in config).
Name string `json:"name"`
// Description of the functions purpose (in markdown).
Description string `json:"description,omitempty"`
// Params defines the expected arguments of the function.
Params Params `json:"params"`
// Examples shows general usage for the function.
Examples []ExampleSpec `json:"examples,omitempty"`
// Impure indicates that a function accesses or interacts with the outer
// environment, and is therefore unsafe to execute in shared environments.
Impure bool `json:"impure"`
// Version is the Benthos version this component was introduced.
Version string `json:"version,omitempty"`
}
// NewFunctionSpec creates a new function spec.
func NewFunctionSpec(category, name, description string, examples ...ExampleSpec) FunctionSpec {
return FunctionSpec{
Status: StatusStable,
Category: category,
Name: name,
Description: description,
Examples: examples,
Params: NewParams(),
}
}
// Experimental flags the function as an experimental component.
func (s FunctionSpec) Experimental() FunctionSpec {
s.Status = StatusExperimental
return s
}
// Beta flags the function as a beta component.
func (s FunctionSpec) Beta() FunctionSpec {
s.Status = StatusBeta
return s
}
// AtVersion sets the Benthos version this component was introduced.
func (s FunctionSpec) AtVersion(v string) FunctionSpec {
s.Version = v
return s
}
// MarkImpure flags the function as being impure, meaning it access or interacts
// with the environment.
func (s FunctionSpec) MarkImpure() FunctionSpec {
s.Impure = true
return s
}
// Param adds a parameter to the function.
func (s FunctionSpec) Param(def ParamDefinition) FunctionSpec {
s.Params = s.Params.Add(def)
return s
}
// NewDeprecatedFunctionSpec creates a new function spec that is deprecated.
func NewDeprecatedFunctionSpec(name, description string, examples ...ExampleSpec) FunctionSpec {
return FunctionSpec{
Status: StatusDeprecated,
Category: FunctionCategoryDeprecated,
Name: name,
Description: description,
Examples: examples,
Params: NewParams(),
}
}
// NewHiddenFunctionSpec creates a new function spec that is hidden from the docs.
func NewHiddenFunctionSpec(name string) FunctionSpec {
return FunctionSpec{
Status: StatusHidden,
Name: name,
Params: NewParams(),
}
}
//------------------------------------------------------------------------------
// Method categories.
var (
MethodCategoryStrings = "String Manipulation"
MethodCategoryNumbers = "Number Manipulation"
MethodCategoryTime = "Timestamp Manipulation"
MethodCategoryRegexp = "Regular Expressions"
MethodCategoryEncoding = "Encoding and Encryption"
MethodCategoryCoercion = "Type Coercion"
MethodCategoryParsing = "Parsing"
MethodCategoryObjectAndArray = "Object & Array Manipulation"
MethodCategoryJWT = "JSON Web Tokens"
MethodCategoryGeoIP = "GeoIP"
MethodCategoryDeprecated = "Deprecated"
MethodCategoryPlugin = "Plugin"
)
// MethodCatSpec describes how a method behaves in the context of a given
// category.
type MethodCatSpec struct {
Category string
Description string
Examples []ExampleSpec
}
// MethodSpec describes a Bloblang method.
type MethodSpec struct {
// The release status of the function.
Status Status `json:"status"`
// Name of the method (as it appears in config).
Name string `json:"name"`
// Description of the method purpose (in markdown).
Description string `json:"description,omitempty"`
// Params defines the expected arguments of the method.
Params Params `json:"params"`
// Examples shows general usage for the method.
Examples []ExampleSpec `json:"examples,omitempty"`
// Categories that this method fits within.
Categories []MethodCatSpec `json:"categories,omitempty"`
// Impure indicates that a method accesses or interacts with the outer
// environment, and is therefore unsafe to execute in shared environments.
Impure bool `json:"impure"`
// Version is the Benthos version this component was introduced.
Version string `json:"version,omitempty"`
}
// NewMethodSpec creates a new method spec.
func NewMethodSpec(name, description string, examples ...ExampleSpec) MethodSpec {
return MethodSpec{
Name: name,
Status: StatusStable,
Description: description,
Examples: examples,
Params: NewParams(),
}
}
// NewDeprecatedMethodSpec creates a new method spec that is deprecated. The
// method will not appear in docs or searches but will still be usable in
// mappings.
func NewDeprecatedMethodSpec(name, description string, examples ...ExampleSpec) MethodSpec {
return MethodSpec{
Name: name,
Status: StatusDeprecated,
Examples: examples,
Params: NewParams(),
}
}
// NewHiddenMethodSpec creates a new method spec that is hidden from docs.
func NewHiddenMethodSpec(name string) MethodSpec {
return MethodSpec{
Name: name,
Status: StatusHidden,
Params: NewParams(),
}
}
// Experimental flags the method as an experimental component.
func (m MethodSpec) Experimental() MethodSpec {
m.Status = StatusExperimental
return m
}
// Beta flags the function as a beta component.
func (m MethodSpec) Beta() MethodSpec {
m.Status = StatusBeta
return m
}
// AtVersion sets the Benthos version this component was introduced.
func (m MethodSpec) AtVersion(v string) MethodSpec {
m.Version = v
return m
}
// MarkImpure flags the method as being impure, meaning it access or interacts
// with the environment.
func (m MethodSpec) MarkImpure() MethodSpec {
m.Impure = true
return m
}
// Param adds a parameter to the function.
func (m MethodSpec) Param(def ParamDefinition) MethodSpec {
m.Params = m.Params.Add(def)
return m
}
// VariadicParams configures the method spec to allow variadic parameters.
func (m MethodSpec) VariadicParams() MethodSpec {
m.Params = VariadicParams()
return m
}
// InCategory describes the methods behaviour in the context of a given
// category, methods can belong to multiple categories. For example, the
// `contains` method behaves differently in the object and array category versus
// the strings one, but belongs in both.
func (m MethodSpec) InCategory(category, description string, examples ...ExampleSpec) MethodSpec {
if m.Status == StatusDeprecated {
category = MethodCategoryDeprecated
}
cats := make([]MethodCatSpec, 0, len(m.Categories)+1)
cats = append(cats, m.Categories...)
cats = append(cats, MethodCatSpec{
Category: category,
Description: description,
Examples: examples,
})
m.Categories = cats
return m
}