-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathexpression.go
503 lines (452 loc) · 12.3 KB
/
expression.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ottl // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
import (
"context"
"encoding/hex"
"fmt"
"strconv"
jsoniter "github.com/json-iterator/go"
"go.opentelemetry.io/collector/pdata/pcommon"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/internal/ottlcommon"
)
type ExprFunc[K any] func(ctx context.Context, tCtx K) (interface{}, error)
type Expr[K any] struct {
exprFunc ExprFunc[K]
}
func (e Expr[K]) Eval(ctx context.Context, tCtx K) (interface{}, error) {
return e.exprFunc(ctx, tCtx)
}
type Getter[K any] interface {
Get(ctx context.Context, tCtx K) (interface{}, error)
}
type Setter[K any] interface {
Set(ctx context.Context, tCtx K, val interface{}) error
}
type GetSetter[K any] interface {
Getter[K]
Setter[K]
}
type StandardGetSetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (interface{}, error)
Setter func(ctx context.Context, tCtx K, val interface{}) error
}
func (path StandardGetSetter[K]) Get(ctx context.Context, tCtx K) (interface{}, error) {
return path.Getter(ctx, tCtx)
}
func (path StandardGetSetter[K]) Set(ctx context.Context, tCtx K, val interface{}) error {
return path.Setter(ctx, tCtx, val)
}
type literal[K any] struct {
value interface{}
}
func (l literal[K]) Get(context.Context, K) (interface{}, error) {
return l.value, nil
}
type exprGetter[K any] struct {
expr Expr[K]
keys []Key
}
func (g exprGetter[K]) Get(ctx context.Context, tCtx K) (interface{}, error) {
result, err := g.expr.Eval(ctx, tCtx)
if err != nil {
return nil, err
}
if g.keys == nil {
return result, nil
}
for _, k := range g.keys {
switch {
case k.String != nil:
switch r := result.(type) {
case pcommon.Map:
val, ok := r.Get(*k.String)
if !ok {
return nil, fmt.Errorf("key not found in map")
}
result = ottlcommon.GetValue(val)
case map[string]interface{}:
val, ok := r[*k.String]
if !ok {
return nil, fmt.Errorf("key not found in map")
}
result = val
default:
return nil, fmt.Errorf("type, %T, does not support string indexing", result)
}
case k.Int != nil:
switch r := result.(type) {
case pcommon.Slice:
if int(*k.Int) >= r.Len() || int(*k.Int) < 0 {
return nil, fmt.Errorf("index %v out of bounds", *k.Int)
}
result = ottlcommon.GetValue(r.At(int(*k.Int)))
case []interface{}:
if int(*k.Int) >= len(r) || int(*k.Int) < 0 {
return nil, fmt.Errorf("index %v out of bounds", *k.Int)
}
result = r[*k.Int]
default:
return nil, fmt.Errorf("type, %T, does not support int indexing", result)
}
default:
return nil, fmt.Errorf("neither map nor slice index were set; this is an error in OTTL")
}
}
return result, nil
}
type listGetter[K any] struct {
slice []Getter[K]
}
func (l *listGetter[K]) Get(ctx context.Context, tCtx K) (interface{}, error) {
evaluated := make([]any, len(l.slice))
for i, v := range l.slice {
val, err := v.Get(ctx, tCtx)
if err != nil {
return nil, err
}
evaluated[i] = val
}
return evaluated, nil
}
// StringGetter is a Getter that must return a string.
type StringGetter[K any] interface {
// Get retrieves a string value. If the value is not a string, an error is returned.
Get(ctx context.Context, tCtx K) (string, error)
}
type StandardStringGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (interface{}, error)
}
func (g StandardStringGetter[K]) Get(ctx context.Context, tCtx K) (string, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return "", err
}
if val == nil {
return "", fmt.Errorf("expected string but got nil")
}
switch v := val.(type) {
case string:
return v, nil
case pcommon.Value:
if v.Type() == pcommon.ValueTypeStr {
return v.Str(), nil
}
return "", fmt.Errorf("expected string but got %v", v.Type())
default:
return "", fmt.Errorf("expected string but got %T", val)
}
}
type IntGetter[K any] interface {
Get(ctx context.Context, tCtx K) (int64, error)
}
type StandardIntGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (interface{}, error)
}
func (g StandardIntGetter[K]) Get(ctx context.Context, tCtx K) (int64, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return 0, err
}
if val == nil {
return 0, fmt.Errorf("expected int64 but got nil")
}
switch v := val.(type) {
case int64:
return v, nil
case pcommon.Value:
if v.Type() == pcommon.ValueTypeInt {
return v.Int(), nil
}
return 0, fmt.Errorf("expected int64 but got %v", v.Type())
default:
return 0, fmt.Errorf("expected int64 but got %T", val)
}
}
type FloatGetter[K any] interface {
Get(ctx context.Context, tCtx K) (float64, error)
}
type StandardFloatGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (interface{}, error)
}
func (g StandardFloatGetter[K]) Get(ctx context.Context, tCtx K) (float64, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return 0, err
}
if val == nil {
return 0, fmt.Errorf("expected float64 but got nil")
}
switch v := val.(type) {
case float64:
return v, nil
case pcommon.Value:
if v.Type() == pcommon.ValueTypeDouble {
return v.Double(), nil
}
return 0, fmt.Errorf("expected float64 but got %v", v.Type())
default:
return 0, fmt.Errorf("expected float64 but got %T", val)
}
}
type PMapGetter[K any] interface {
Get(ctx context.Context, tCtx K) (pcommon.Map, error)
}
type StandardPMapGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (interface{}, error)
}
func (g StandardPMapGetter[K]) Get(ctx context.Context, tCtx K) (pcommon.Map, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return pcommon.Map{}, err
}
if val == nil {
return pcommon.Map{}, fmt.Errorf("expected pcommon.Map but got nil")
}
switch v := val.(type) {
case pcommon.Map:
return v, nil
case pcommon.Value:
if v.Type() == pcommon.ValueTypeMap {
return v.Map(), nil
}
return pcommon.Map{}, fmt.Errorf("expected pcommon.Map but got %v", v.Type())
case map[string]any:
m := pcommon.NewMap()
err = m.FromRaw(v)
if err != nil {
return pcommon.Map{}, err
}
return m, nil
default:
return pcommon.Map{}, fmt.Errorf("expected pcommon.Map but got %T", val)
}
}
// StringLikeGetter is a Getter that returns a string by converting the underlying value to a string if necessary.
type StringLikeGetter[K any] interface {
// Get retrieves a string value.
// Unlike `StringGetter`, the expectation is that the underlying value is converted to a string if possible.
// If the value cannot be converted to a string, nil and an error are returned.
// If the value is nil, nil is returned without an error.
Get(ctx context.Context, tCtx K) (*string, error)
}
type StandardStringLikeGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (interface{}, error)
}
func (g StandardStringLikeGetter[K]) Get(ctx context.Context, tCtx K) (*string, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return nil, err
}
if val == nil {
return nil, nil
}
var result string
switch v := val.(type) {
case string:
result = v
case []byte:
result = hex.EncodeToString(v)
case pcommon.Map:
result, err = jsoniter.MarshalToString(v.AsRaw())
if err != nil {
return nil, err
}
case pcommon.Slice:
result, err = jsoniter.MarshalToString(v.AsRaw())
if err != nil {
return nil, err
}
case pcommon.Value:
result = v.AsString()
default:
result, err = jsoniter.MarshalToString(v)
if err != nil {
return nil, fmt.Errorf("unsupported type: %T", v)
}
}
return &result, nil
}
// FloatLikeGetter is a Getter that returns a float64 by converting the underlying value to a float64 if necessary.
type FloatLikeGetter[K any] interface {
// Get retrieves a float64 value.
// Unlike `FloatGetter`, the expectation is that the underlying value is converted to a float64 if possible.
// If the value cannot be converted to a float64, nil and an error are returned.
// If the value is nil, nil is returned without an error.
Get(ctx context.Context, tCtx K) (*float64, error)
}
type StandardFloatLikeGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (interface{}, error)
}
func (g StandardFloatLikeGetter[K]) Get(ctx context.Context, tCtx K) (*float64, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return nil, err
}
if val == nil {
return nil, nil
}
var result float64
switch v := val.(type) {
case float64:
result = v
case int64:
result = float64(v)
case string:
result, err = strconv.ParseFloat(v, 64)
if err != nil {
return nil, err
}
case bool:
if v {
result = float64(1)
} else {
result = float64(0)
}
case pcommon.Value:
switch v.Type() {
case pcommon.ValueTypeDouble:
result = v.Double()
case pcommon.ValueTypeInt:
result = float64(v.Int())
case pcommon.ValueTypeStr:
result, err = strconv.ParseFloat(v.Str(), 64)
if err != nil {
return nil, err
}
case pcommon.ValueTypeBool:
if v.Bool() {
result = float64(1)
} else {
result = float64(0)
}
default:
return nil, fmt.Errorf("unsupported value type: %v", v.Type())
}
default:
return nil, fmt.Errorf("unsupported type: %T", v)
}
return &result, nil
}
// IntLikeGetter is a Getter that returns an int by converting the underlying value to an int if necessary.
type IntLikeGetter[K any] interface {
// Get retrieves an int value.
// Unlike `IntGetter`, the expectation is that the underlying value is converted to an int if possible.
// If the value cannot be converted to an int, nil and an error are returned.
// If the value is nil, nil is returned without an error.
Get(ctx context.Context, tCtx K) (*int64, error)
}
type StandardIntLikeGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (interface{}, error)
}
func (g StandardIntLikeGetter[K]) Get(ctx context.Context, tCtx K) (*int64, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return nil, err
}
if val == nil {
return nil, nil
}
var result int64
switch v := val.(type) {
case int64:
result = v
case string:
result, err = strconv.ParseInt(v, 10, 64)
if err != nil {
return nil, nil
}
case float64:
result = int64(v)
case bool:
if v {
result = int64(1)
} else {
result = int64(0)
}
case pcommon.Value:
switch v.Type() {
case pcommon.ValueTypeInt:
result = v.Int()
case pcommon.ValueTypeDouble:
result = int64(v.Double())
case pcommon.ValueTypeStr:
result, err = strconv.ParseInt(v.Str(), 10, 64)
if err != nil {
return nil, nil
}
case pcommon.ValueTypeBool:
if v.Bool() {
result = int64(1)
} else {
result = int64(0)
}
default:
return nil, fmt.Errorf("unsupported value type: %v", v.Type())
}
default:
return nil, fmt.Errorf("unsupported type: %T", v)
}
return &result, nil
}
func (p *Parser[K]) newGetter(val value) (Getter[K], error) {
if val.IsNil != nil && *val.IsNil {
return &literal[K]{value: nil}, nil
}
if s := val.String; s != nil {
return &literal[K]{value: *s}, nil
}
if b := val.Bool; b != nil {
return &literal[K]{value: bool(*b)}, nil
}
if b := val.Bytes; b != nil {
return &literal[K]{value: ([]byte)(*b)}, nil
}
if val.Enum != nil {
enum, err := p.enumParser(val.Enum)
if err != nil {
return nil, err
}
return &literal[K]{value: int64(*enum)}, nil
}
if eL := val.Literal; eL != nil {
if f := eL.Float; f != nil {
return &literal[K]{value: *f}, nil
}
if i := eL.Int; i != nil {
return &literal[K]{value: *i}, nil
}
if eL.Path != nil {
return p.pathParser(eL.Path)
}
if eL.Converter != nil {
return p.newGetterFromConverter(*eL.Converter)
}
}
if val.List != nil {
lg := listGetter[K]{slice: make([]Getter[K], len(val.List.Values))}
for i, v := range val.List.Values {
getter, err := p.newGetter(v)
if err != nil {
return nil, err
}
lg.slice[i] = getter
}
return &lg, nil
}
if val.MathExpression == nil {
// In practice, can't happen since the DSL grammar guarantees one is set
return nil, fmt.Errorf("no value field set. This is a bug in the OpenTelemetry Transformation Language")
}
return p.evaluateMathExpression(val.MathExpression)
}
func (p *Parser[K]) newGetterFromConverter(c converter) (Getter[K], error) {
call, err := p.newFunctionCall(editor(c))
if err != nil {
return nil, err
}
return &exprGetter[K]{
expr: call,
keys: c.Keys,
}, nil
}