forked from benthosdev/benthos
/
methods.go
510 lines (451 loc) · 14.4 KB
/
methods.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package query
import (
"errors"
"fmt"
"strconv"
"github.com/Jeffail/gabs/v2"
)
var _ = registerMethod(
NewMethodSpec(
"apply",
"Apply a declared mapping to a target value.",
NewExampleSpec("",
`map thing {
root.inner = this.first
}
root.foo = this.doc.apply("thing")`,
`{"doc":{"first":"hello world"}}`,
`{"foo":{"inner":"hello world"}}`,
),
NewExampleSpec("",
`map create_foo {
root.name = "a foo"
root.purpose = "to be a foo"
}
root = this
root.foo = null.apply("create_foo")`,
`{"id":"1234"}`,
`{"foo":{"name":"a foo","purpose":"to be a foo"},"id":"1234"}`,
),
).Param(ParamString("mapping", "The mapping to apply.")),
applyMethod,
)
func applyMethod(target Function, args *ParsedParams) (Function, error) {
targetMap, err := args.FieldString("mapping")
if err != nil {
return nil, err
}
return ClosureFunction("map "+targetMap, func(ctx FunctionContext) (any, error) {
res, err := target.Exec(ctx)
if err != nil {
return nil, err
}
ctx = ctx.WithValue(res)
if ctx.Maps == nil {
return nil, errors.New("no maps were found")
}
m, ok := ctx.Maps[targetMap]
if !ok {
return nil, fmt.Errorf("map %v was not found", targetMap)
}
// ISOLATED VARIABLES
ctx.Vars = map[string]any{}
return m.Exec(ctx)
}, func(ctx TargetsContext) (TargetsContext, []TargetPath) {
mapFn, ok := ctx.Maps[targetMap]
if !ok {
return target.QueryTargets(ctx)
}
mapCtx, targets := target.QueryTargets(ctx)
mapCtx = mapCtx.WithValues(targets).WithValuesAsContext()
returnCtx, mapTargets := mapFn.QueryTargets(mapCtx)
return returnCtx, append(targets, mapTargets...)
}), nil
}
//------------------------------------------------------------------------------
var _ = registerMethod(
NewMethodSpec("bool", "").InCategory(
MethodCategoryCoercion,
"Attempt to parse a value into a boolean. An optional argument can be provided, in which case if the value cannot be parsed the argument will be returned instead. If the value is a number then any non-zero value will resolve to `true`, if the value is a string then any of the following values are considered valid: `1, t, T, TRUE, true, True, 0, f, F, FALSE`.",
NewExampleSpec("",
`root.foo = this.thing.bool()
root.bar = this.thing.bool(true)`,
),
).Param(ParamBool("default", "An optional value to yield if the target cannot be parsed as a boolean.").Optional()),
boolMethod,
)
func boolMethod(target Function, args *ParsedParams) (Function, error) {
defaultBool, err := args.FieldOptionalBool("default")
if err != nil {
return nil, err
}
return ClosureFunction("method bool", func(ctx FunctionContext) (any, error) {
v, err := target.Exec(ctx)
if err != nil {
if defaultBool != nil {
return *defaultBool, nil
}
return nil, err
}
f, err := IToBool(v)
if err != nil {
if defaultBool != nil {
return *defaultBool, nil
}
return nil, ErrFrom(err, target)
}
return f, nil
}, target.QueryTargets), nil
}
//------------------------------------------------------------------------------
var _ = registerMethod(
NewMethodSpec(
"catch",
"If the result of a target query fails (due to incorrect types, failed parsing, etc) the argument is returned instead.",
NewExampleSpec("",
`root.doc.id = this.thing.id.string().catch(uuid_v4())`,
),
NewExampleSpec("The fallback argument can be a mapping, allowing you to capture the error string and yield structured data back.",
`root.url = this.url.parse_url().catch(err -> {"error":err,"input":this.url})`,
`{"url":"invalid %&# url"}`,
`{"url":{"error":"field `+"`this.url`"+`: parse \"invalid %&\": invalid URL escape \"%&\"","input":"invalid %&# url"}}`,
),
NewExampleSpec("When the input document is not structured attempting to reference structured fields with `this` will result in an error. Therefore, a convenient way to delete non-structured data is with a catch.",
`root = this.catch(deleted())`,
`{"doc":{"foo":"bar"}}`,
`{"doc":{"foo":"bar"}}`,
`not structured data`,
`<Message deleted>`,
),
).Param(ParamQuery("fallback", "A value to yield, or query to execute, if the target query fails.", true)),
catchMethod,
)
func catchMethod(fn Function, args *ParsedParams) (Function, error) {
catchFn, err := args.FieldQuery("fallback")
if err != nil {
return nil, err
}
return ClosureFunction("method catch", func(ctx FunctionContext) (any, error) {
res, err := fn.Exec(ctx)
if err != nil {
return catchFn.Exec(ctx.WithValue(err.Error()))
}
return res, err
}, aggregateTargetPaths(fn, catchFn)), nil
}
//------------------------------------------------------------------------------
var _ = registerMethod(
NewMethodSpec(
"from",
"Modifies a target query such that certain functions are executed from the perspective of another message in the batch. This allows you to mutate events based on the contents of other messages. Functions that support this behaviour are `content`, `json` and `meta`.",
NewExampleSpec("For example, the following map extracts the contents of the JSON field `foo` specifically from message index `1` of a batch, effectively overriding the field `foo` for all messages of a batch to that of message 1:",
`root = this
root.foo = json("foo").from(1)`,
),
).Param(ParamInt64("index", "The message index to use as a perspective.")),
func(target Function, args *ParsedParams) (Function, error) {
i64, err := args.FieldInt64("index")
if err != nil {
return nil, err
}
return &fromMethod{
index: int(i64),
target: target,
}, nil
},
)
type fromMethod struct {
index int
target Function
}
func (f *fromMethod) Annotation() string {
return f.target.Annotation() + " from " + strconv.Itoa(f.index)
}
func (f *fromMethod) Exec(ctx FunctionContext) (any, error) {
ctx.Index = f.index
return f.target.Exec(ctx)
}
func (f *fromMethod) QueryTargets(ctx TargetsContext) (TargetsContext, []TargetPath) {
// TODO: Modify context to represent new index.
return f.target.QueryTargets(ctx)
}
//------------------------------------------------------------------------------
var _ = registerMethod(
NewMethodSpec(
"from_all",
"Modifies a target query such that certain functions are executed from the perspective of each message in the batch, and returns the set of results as an array. Functions that support this behaviour are `content`, `json` and `meta`.",
NewExampleSpec("",
`root = this
root.foo_summed = json("foo").from_all().sum()`,
),
),
fromAllMethod,
)
func fromAllMethod(target Function, _ *ParsedParams) (Function, error) {
return ClosureFunction("method from_all", func(ctx FunctionContext) (any, error) {
values := make([]any, ctx.MsgBatch.Len())
for i := 0; i < ctx.MsgBatch.Len(); i++ {
subCtx := ctx
subCtx.Index = i
var err error
if values[i], err = target.Exec(subCtx); err != nil {
return nil, err
}
}
return values, nil
}, target.QueryTargets), nil
}
//------------------------------------------------------------------------------
var _ = registerMethod(
NewMethodSpec(
"get",
"Extract a field value, identified via a [dot path][field_paths], from an object.",
).InCategory(
MethodCategoryObjectAndArray, "",
NewExampleSpec("",
`root.result = this.foo.get(this.target)`,
`{"foo":{"bar":"from bar","baz":"from baz"},"target":"bar"}`,
`{"result":"from bar"}`,
`{"foo":{"bar":"from bar","baz":"from baz"},"target":"baz"}`,
`{"result":"from baz"}`,
),
).Param(ParamString("path", "A [dot path][field_paths] identifying a field to obtain.")),
getMethodCtor,
)
type getMethod struct {
fn Function
path []string
}
func (g *getMethod) Annotation() string {
return "path `" + SliceToDotPath(g.path...) + "`"
}
func (g *getMethod) Exec(ctx FunctionContext) (any, error) {
v, err := g.fn.Exec(ctx)
if err != nil {
return nil, err
}
return gabs.Wrap(v).S(g.path...).Data(), nil
}
func (g *getMethod) QueryTargets(ctx TargetsContext) (TargetsContext, []TargetPath) {
ctx, fnPaths := g.fn.QueryTargets(ctx)
basePaths := ctx.Value()
paths := make([]TargetPath, len(basePaths))
for i, p := range basePaths {
paths[i] = p
paths[i].Path = append(paths[i].Path, g.path...)
}
ctx = ctx.WithValues(paths)
return ctx, append(fnPaths, paths...)
}
// NewGetMethod creates a new get method.
func NewGetMethod(target Function, pathStr string) (Function, error) {
path := gabs.DotPathToSlice(pathStr)
switch t := target.(type) {
case *getMethod:
newPath := append([]string{}, t.path...)
newPath = append(newPath, path...)
return &getMethod{
fn: t.fn,
path: newPath,
}, nil
case *fieldFunction:
return t.expand(path...), nil
}
return &getMethod{
fn: target,
path: path,
}, nil
}
func getMethodCtor(target Function, args *ParsedParams) (Function, error) {
pathStr, err := args.FieldString("path")
if err != nil {
return nil, err
}
return NewGetMethod(target, pathStr)
}
//------------------------------------------------------------------------------
var _ = registerMethod(
NewHiddenMethodSpec("map").
Param(ParamQuery("query", "A query to execute on the target.", false)),
mapMethod,
)
// NewMapMethod attempts to create a map method.
func NewMapMethod(target, mapFn Function) (Function, error) {
return ClosureFunction(mapFn.Annotation(), func(ctx FunctionContext) (any, error) {
res, err := target.Exec(ctx)
if err != nil {
return nil, err
}
return mapFn.Exec(ctx.WithValue(res))
}, func(ctx TargetsContext) (TargetsContext, []TargetPath) {
mapCtx, targets := target.QueryTargets(ctx)
mapCtx = mapCtx.WithValues(targets).WithValuesAsContext()
returnCtx, mapTargets := mapFn.QueryTargets(mapCtx)
return returnCtx, append(targets, mapTargets...)
}), nil
}
func mapMethod(target Function, args *ParsedParams) (Function, error) {
mapFn, err := args.FieldQuery("query")
if err != nil {
return nil, err
}
return NewMapMethod(target, mapFn)
}
//------------------------------------------------------------------------------
var _ = registerMethod(NewHiddenMethodSpec("not"), notMethodCtor)
type notMethod struct {
fn Function
}
// Not returns a logical NOT of a child function.
func Not(fn Function) Function {
return ¬Method{
fn: fn,
}
}
func (n *notMethod) Annotation() string {
return "not " + n.fn.Annotation()
}
func (n *notMethod) Exec(ctx FunctionContext) (any, error) {
v, err := n.fn.Exec(ctx)
if err != nil {
return nil, err
}
b, ok := v.(bool)
if !ok {
return nil, NewTypeErrorFrom(n.fn.Annotation(), v, ValueBool)
}
return !b, nil
}
func (n *notMethod) QueryTargets(ctx TargetsContext) (TargetsContext, []TargetPath) {
return n.fn.QueryTargets(ctx)
}
func notMethodCtor(target Function, _ *ParsedParams) (Function, error) {
return ¬Method{fn: target}, nil
}
//------------------------------------------------------------------------------
var _ = registerSimpleMethod(
NewMethodSpec(
"not_null", "",
).InCategory(
MethodCategoryCoercion,
"Ensures that the given value is not `null`, and if so returns it, otherwise an error is returned.",
NewExampleSpec("",
`root.a = this.a.not_null()`,
`{"a":"foobar","b":"barbaz"}`,
`{"a":"foobar"}`,
`{"b":"barbaz"}`,
`Error("failed assignment (line 1): field `+"`this.a`"+`: value is null")`,
),
),
func(*ParsedParams) (simpleMethod, error) {
return func(v any, ctx FunctionContext) (any, error) {
if v == nil {
return nil, errors.New("value is null")
}
return v, nil
}, nil
},
)
//------------------------------------------------------------------------------
var _ = registerMethod(
NewMethodSpec(
"number", "",
).InCategory(
MethodCategoryCoercion,
"Attempt to parse a value into a number. An optional argument can be provided, in which case if the value cannot be parsed into a number the argument will be returned instead.",
NewExampleSpec("",
`root.foo = this.thing.number() + 10
root.bar = this.thing.number(5) * 10`,
),
).Param(ParamFloat("default", "An optional value to yield if the target cannot be parsed as a number.").Optional()),
numberCoerceMethod,
)
func numberCoerceMethod(target Function, args *ParsedParams) (Function, error) {
defaultNum, err := args.FieldOptionalFloat("default")
if err != nil {
return nil, err
}
return ClosureFunction("method number", func(ctx FunctionContext) (any, error) {
v, err := target.Exec(ctx)
if err != nil {
if defaultNum != nil {
return *defaultNum, nil
}
return nil, err
}
f, err := IToNumber(v)
if err != nil {
if defaultNum != nil {
return *defaultNum, nil
}
return nil, ErrFrom(err, target)
}
return f, nil
}, target.QueryTargets), nil
}
//------------------------------------------------------------------------------
var _ = registerMethod(
NewMethodSpec(
"or", "If the result of the target query fails or resolves to `null`, returns the argument instead. This is an explicit method alternative to the coalesce pipe operator `|`.",
NewExampleSpec("", `root.doc.id = this.thing.id.or(uuid_v4())`),
).Param(ParamQuery("fallback", "A value to yield, or query to execute, if the target query fails or resolves to `null`.", true)),
orMethod,
)
func orMethod(fn Function, args *ParsedParams) (Function, error) {
orFn, err := args.FieldQuery("fallback")
if err != nil {
return nil, err
}
return ClosureFunction("method or", func(ctx FunctionContext) (any, error) {
res, err := fn.Exec(ctx)
if err != nil || IIsNull(res) {
return orFn.Exec(ctx)
}
return res, err
}, aggregateTargetPaths(fn, orFn)), nil
}
//------------------------------------------------------------------------------
var _ = registerSimpleMethod(
NewMethodSpec(
"type", "",
).InCategory(
MethodCategoryCoercion,
"Returns the type of a value as a string, providing one of the following values: `string`, `bytes`, `number`, `bool`, `timestamp`, `array`, `object` or `null`.",
NewExampleSpec("",
`root.bar_type = this.bar.type()
root.foo_type = this.foo.type()`,
`{"bar":10,"foo":"is a string"}`,
`{"bar_type":"number","foo_type":"string"}`,
),
NewExampleSpec("",
`root.type = this.type()`,
`"foobar"`,
`{"type":"string"}`,
`666`,
`{"type":"number"}`,
`false`,
`{"type":"bool"}`,
`["foo", "bar"]`,
`{"type":"array"}`,
`{"foo": "bar"}`,
`{"type":"object"}`,
`null`,
`{"type":"null"}`,
),
NewExampleSpec("",
`root.type = content().type()`,
`foobar`,
`{"type":"bytes"}`,
),
NewExampleSpec("",
`root.type = this.ts_parse("2006-01-02").type()`,
`"2022-06-06"`,
`{"type":"timestamp"}`,
),
),
func(*ParsedParams) (simpleMethod, error) {
return func(v any, ctx FunctionContext) (any, error) {
return string(ITypeOf(v)), nil
}, nil
},
)