-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathsplit.go
286 lines (245 loc) · 6.45 KB
/
split.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package v2
import (
"errors"
"fmt"
"strings"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/logp"
)
var (
errEmptyField = errors.New("the requested field is empty")
errEmptyRootField = errors.New("the requested root field is empty")
errExpectedSplitArr = errors.New("split was expecting field to be an array")
errExpectedSplitObj = errors.New("split was expecting field to be an object")
errExpectedSplitString = errors.New("split was expecting field to be a string")
)
// split is a split processor chain element. Split processing is executed
// by applying elements of the chain's linked list to an input until completed
// or an error state is encountered.
type split struct {
log *logp.Logger
targetInfo targetInfo
kind string
transforms []basicTransform
child *split
keepParent bool
ignoreEmptyValue bool
keyField string
isRoot bool
delimiter string
}
// newSplitResponse returns a new split based on the provided config and
// logging to the provided logger, tagging the split as the root of the chain.
func newSplitResponse(cfg *splitConfig, log *logp.Logger) (*split, error) {
if cfg == nil {
return nil, nil
}
split, err := newSplit(cfg, log)
if err != nil {
return nil, err
}
// We want to be able to identify which split is the root of the chain.
split.isRoot = true
return split, nil
}
// newSplit returns a new split based on the provided config and
// logging to the provided logger.
func newSplit(c *splitConfig, log *logp.Logger) (*split, error) {
ti, err := getTargetInfo(c.Target)
if err != nil {
return nil, err
}
if ti.Type != targetBody {
return nil, fmt.Errorf("invalid target type: %s", ti.Type)
}
ts, err := newBasicTransformsFromConfig(c.Transforms, responseNamespace, log)
if err != nil {
return nil, err
}
var s *split
if c.Split != nil {
s, err = newSplit(c.Split, log)
if err != nil {
return nil, err
}
}
return &split{
log: log,
targetInfo: ti,
kind: c.Type,
keepParent: c.KeepParent,
ignoreEmptyValue: c.IgnoreEmptyValue,
keyField: c.KeyField,
delimiter: c.DelimiterString,
transforms: ts,
child: s,
}, nil
}
// run runs the split operation on the contents of resp, sending successive
// split results on ch. ctx is passed to transforms that are called during
// the split.
func (s *split) run(ctx *transformContext, resp transformable, ch chan<- maybeMsg) error {
root := resp.body()
return s.split(ctx, root, ch)
}
// split recursively executes the split processor chain.
func (s *split) split(ctx *transformContext, root common.MapStr, ch chan<- maybeMsg) error {
v, err := root.GetValue(s.targetInfo.Name)
if err != nil && err != common.ErrKeyNotFound {
return err
}
if v == nil {
if s.ignoreEmptyValue {
if s.child != nil {
return s.child.split(ctx, root, ch)
}
return nil
}
if s.isRoot {
return errEmptyRootField
}
ch <- maybeMsg{msg: root}
return errEmptyField
}
switch s.kind {
case "", splitTypeArr:
varr, ok := v.([]interface{})
if !ok {
return errExpectedSplitArr
}
if len(varr) == 0 {
if s.ignoreEmptyValue {
if s.child != nil {
return s.child.split(ctx, root, ch)
}
return nil
}
if s.isRoot {
return errEmptyRootField
}
ch <- maybeMsg{msg: root}
return errEmptyField
}
for _, e := range varr {
if err := s.sendMessage(ctx, root, "", e, ch); err != nil {
s.log.Debug(err)
}
}
return nil
case splitTypeMap:
vmap, ok := toMapStr(v)
if !ok {
return errExpectedSplitObj
}
if len(vmap) == 0 {
if s.ignoreEmptyValue {
if s.child != nil {
return s.child.split(ctx, root, ch)
}
return nil
}
if s.isRoot {
return errEmptyRootField
}
ch <- maybeMsg{msg: root}
return errEmptyField
}
for k, e := range vmap {
if err := s.sendMessage(ctx, root, k, e, ch); err != nil {
s.log.Debug(err)
}
}
return nil
case splitTypeString:
vstr, ok := v.(string)
if !ok {
return errExpectedSplitString
}
if len(vstr) == 0 {
if s.ignoreEmptyValue {
if s.child != nil {
return s.child.split(ctx, root, ch)
}
return nil
}
if s.isRoot {
return errEmptyRootField
}
ch <- maybeMsg{msg: root}
return errEmptyField
}
for _, substr := range strings.Split(vstr, s.delimiter) {
if err := s.sendMessageSplitString(ctx, root, substr, ch); err != nil {
s.log.Debug(err)
}
}
return nil
}
return errors.New("unknown split type")
}
// sendMessage sends an array or map split result value, v, on ch after performing
// any necessary transformations. If key is "", the value is an element of an array.
func (s *split) sendMessage(ctx *transformContext, root common.MapStr, key string, v interface{}, ch chan<- maybeMsg) error {
obj, ok := toMapStr(v)
if !ok {
return errExpectedSplitObj
}
clone := root.Clone()
if s.keyField != "" && key != "" {
_, _ = obj.Put(s.keyField, key)
}
if s.keepParent {
_, _ = clone.Put(s.targetInfo.Name, obj)
} else {
clone = obj
}
tr := transformable{}
tr.setBody(clone)
var err error
for _, t := range s.transforms {
tr, err = t.run(ctx, tr)
if err != nil {
return err
}
}
if s.child != nil {
return s.child.split(ctx, clone, ch)
}
ch <- maybeMsg{msg: clone}
return nil
}
func toMapStr(v interface{}) (common.MapStr, bool) {
if v == nil {
return common.MapStr{}, false
}
switch t := v.(type) {
case common.MapStr:
return t, true
case map[string]interface{}:
return common.MapStr(t), true
}
return common.MapStr{}, false
}
// sendMessage sends a string split result value, v, on ch after performing any
// necessary transformations. If key is "", the value is an element of an array.
func (s *split) sendMessageSplitString(ctx *transformContext, root common.MapStr, v string, ch chan<- maybeMsg) error {
clone := root.Clone()
_, _ = clone.Put(s.targetInfo.Name, v)
tr := transformable{}
tr.setBody(clone)
var err error
for _, t := range s.transforms {
tr, err = t.run(ctx, tr)
if err != nil {
return err
}
}
if s.child != nil {
return s.child.split(ctx, clone, ch)
}
ch <- maybeMsg{msg: clone}
return nil
}