/
flatten.go
86 lines (70 loc) · 2.26 KB
/
flatten.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
package process
import (
"context"
"fmt"
"github.com/brexhq/substation/condition"
"github.com/brexhq/substation/config"
"github.com/brexhq/substation/internal/errors"
)
// flatten processes data by flattening object arrays.
//
// This processor supports the object handling pattern.
type procFlatten struct {
process
Options procFlattenOptions `json:"options"`
}
type procFlattenOptions struct {
// Deep determines if arrays should be deeply flattened.
//
// This is optional and defaults to false.
Deep bool `json:"deep"`
}
// Create a new flatten processor.
func newProcFlatten(ctx context.Context, cfg config.Config) (p procFlatten, err error) {
if err = config.Decode(cfg.Settings, &p); err != nil {
return procFlatten{}, err
}
p.operator, err = condition.NewOperator(ctx, p.Condition)
if err != nil {
return procFlatten{}, err
}
// only supports JSON arrays, fail if there are no keys
if p.Key == "" && p.SetKey == "" {
return procFlatten{}, fmt.Errorf("process: flatten: options %+v: %v", p.Options, errors.ErrMissingRequiredOption)
}
return p, nil
}
// String returns the processor settings as an object.
func (p procFlatten) String() string {
return toString(p)
}
// Closes resources opened by the processor.
func (p procFlatten) Close(context.Context) error {
return nil
}
// Stream processes a pipeline of capsules with the processor.
func (p procFlatten) Stream(ctx context.Context, in, out *config.Channel) error {
return streamApply(ctx, in, out, p)
}
// Batch processes one or more capsules with the processor.
func (p procFlatten) Batch(ctx context.Context, capsules ...config.Capsule) ([]config.Capsule, error) {
return batchApply(ctx, capsules, p)
}
// Apply processes a capsule with the processor.
func (p procFlatten) Apply(ctx context.Context, capsule config.Capsule) (config.Capsule, error) {
if ok, err := p.operator.Operate(ctx, capsule); err != nil {
return capsule, fmt.Errorf("process: flatten: %v", err)
} else if !ok {
return capsule, nil
}
var value interface{}
if p.Options.Deep {
value = capsule.Get(p.Key + `|@flatten:{"deep":true}`)
} else {
value = capsule.Get(p.Key + `|@flatten`)
}
if err := capsule.Set(p.SetKey, value); err != nil {
return capsule, fmt.Errorf("process: flatten: %v", err)
}
return capsule, nil
}