forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.go
740 lines (672 loc) · 20.7 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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// 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"
"reflect"
"strconv"
"time"
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) (any, error)
type Expr[K any] struct {
exprFunc ExprFunc[K]
}
func (e Expr[K]) Eval(ctx context.Context, tCtx K) (any, error) {
return e.exprFunc(ctx, tCtx)
}
// Getter resolves a value at runtime without performing any type checking on the value that is returned.
type Getter[K any] interface {
// Get retrieves a value of type 'Any' and returns an error if there are any issues during retrieval.
Get(ctx context.Context, tCtx K) (any, error)
}
// Setter allows setting an untyped value on a predefined field within some data at runtime.
type Setter[K any] interface {
// Set sets a value of type 'Any' and returns an error if there are any issues during the setting process.
Set(ctx context.Context, tCtx K, val any) error
}
// GetSetter is an interface that combines the Getter and Setter interfaces.
// It should be used to represent the ability to both get and set a value.
type GetSetter[K any] interface {
Getter[K]
Setter[K]
}
type StandardGetSetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (any, error)
Setter func(ctx context.Context, tCtx K, val any) error
}
func (path StandardGetSetter[K]) Get(ctx context.Context, tCtx K) (any, error) {
return path.Getter(ctx, tCtx)
}
func (path StandardGetSetter[K]) Set(ctx context.Context, tCtx K, val any) error {
return path.Setter(ctx, tCtx, val)
}
type literal[K any] struct {
value any
}
func (l literal[K]) Get(context.Context, K) (any, 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) (any, 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]any:
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 []any:
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) (any, 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
}
// TypeError represents that a value was not an expected type.
type TypeError string
func (t TypeError) Error() string {
return string(t)
}
// StringGetter is a Getter that must return a string.
type StringGetter[K any] interface {
// Get retrieves a string value.
Get(ctx context.Context, tCtx K) (string, error)
}
// StandardStringGetter is a basic implementation of StringGetter
type StandardStringGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (any, error)
}
// Get retrieves a string value.
// If the value is not a string a new TypeError is returned.
// If there is an error getting the value it will be returned.
func (g StandardStringGetter[K]) Get(ctx context.Context, tCtx K) (string, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return "", fmt.Errorf("error getting value in %T: %w", g, err)
}
if val == nil {
return "", TypeError("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 "", TypeError(fmt.Sprintf("expected string but got %v", v.Type()))
default:
return "", TypeError(fmt.Sprintf("expected string but got %T", val))
}
}
// IntGetter is a Getter that must return an int64.
type IntGetter[K any] interface {
// Get retrieves an int64 value.
Get(ctx context.Context, tCtx K) (int64, error)
}
// StandardIntGetter is a basic implementation of IntGetter
type StandardIntGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (any, error)
}
// Get retrieves an int64 value.
// If the value is not an int64 a new TypeError is returned.
// If there is an error getting the value it will be returned.
func (g StandardIntGetter[K]) Get(ctx context.Context, tCtx K) (int64, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return 0, fmt.Errorf("error getting value in %T: %w", g, err)
}
if val == nil {
return 0, TypeError("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, TypeError(fmt.Sprintf("expected int64 but got %v", v.Type()))
default:
return 0, TypeError(fmt.Sprintf("expected int64 but got %T", val))
}
}
// FloatGetter is a Getter that must return a float64.
type FloatGetter[K any] interface {
// Get retrieves a float64 value.
Get(ctx context.Context, tCtx K) (float64, error)
}
// StandardFloatGetter is a basic implementation of FloatGetter
type StandardFloatGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (any, error)
}
// Get retrieves a float64 value.
// If the value is not a float64 a new TypeError is returned.
// If there is an error getting the value it will be returned.
func (g StandardFloatGetter[K]) Get(ctx context.Context, tCtx K) (float64, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return 0, fmt.Errorf("error getting value in %T: %w", g, err)
}
if val == nil {
return 0, TypeError("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, TypeError(fmt.Sprintf("expected float64 but got %v", v.Type()))
default:
return 0, TypeError(fmt.Sprintf("expected float64 but got %T", val))
}
}
// BoolGetter is a Getter that must return a bool.
type BoolGetter[K any] interface {
// Get retrieves a bool value.
Get(ctx context.Context, tCtx K) (bool, error)
}
// StandardBoolGetter is a basic implementation of BoolGetter
type StandardBoolGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (any, error)
}
// Get retrieves a bool value.
// If the value is not a bool a new TypeError is returned.
// If there is an error getting the value it will be returned.
func (g StandardBoolGetter[K]) Get(ctx context.Context, tCtx K) (bool, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return false, fmt.Errorf("error getting value in %T: %w", g, err)
}
if val == nil {
return false, TypeError("expected bool but got nil")
}
switch v := val.(type) {
case bool:
return v, nil
case pcommon.Value:
if v.Type() == pcommon.ValueTypeBool {
return v.Bool(), nil
}
return false, TypeError(fmt.Sprintf("expected bool but got %v", v.Type()))
default:
return false, TypeError(fmt.Sprintf("expected bool but got %T", val))
}
}
// FunctionGetter uses a function factory to return an instantiated function as an Expr.
type FunctionGetter[K any] interface {
// Get returns a function as an Expr[K] built with the provided Arguments
Get(args Arguments) (Expr[K], error)
}
// StandardFunctionGetter is a basic implementation of FunctionGetter.
type StandardFunctionGetter[K any] struct {
FCtx FunctionContext
Fact Factory[K]
}
// Get takes an Arguments struct containing arguments the caller wants passed to the
// function and instantiates the function with those arguments.
// If there is a mismatch between the function's signature and the arguments the caller
// wants to pass to the function, an error is returned.
func (g StandardFunctionGetter[K]) Get(args Arguments) (Expr[K], error) {
if g.Fact == nil {
return Expr[K]{}, fmt.Errorf("undefined function")
}
fArgs := g.Fact.CreateDefaultArguments()
if reflect.TypeOf(fArgs).Kind() != reflect.Pointer {
return Expr[K]{}, fmt.Errorf("factory for %q must return a pointer to an Arguments value in its CreateDefaultArguments method", g.Fact.Name())
}
if reflect.TypeOf(args).Kind() != reflect.Pointer {
return Expr[K]{}, fmt.Errorf("%q must be pointer to an Arguments value", reflect.TypeOf(args).Kind())
}
fArgsVal := reflect.ValueOf(fArgs).Elem()
argsVal := reflect.ValueOf(args).Elem()
if fArgsVal.NumField() != argsVal.NumField() {
return Expr[K]{}, fmt.Errorf("incorrect number of arguments. Expected: %d Received: %d", fArgsVal.NumField(), argsVal.NumField())
}
for i := 0; i < fArgsVal.NumField(); i++ {
field := argsVal.Field(i)
fArgsVal.Field(i).Set(field)
}
fn, err := g.Fact.CreateFunction(g.FCtx, fArgs)
if err != nil {
return Expr[K]{}, fmt.Errorf("couldn't create function: %w", err)
}
return Expr[K]{exprFunc: fn}, nil
}
// PMapGetter is a Getter that must return a pcommon.Map.
type PMapGetter[K any] interface {
// Get retrieves a pcommon.Map value.
Get(ctx context.Context, tCtx K) (pcommon.Map, error)
}
// StandardPMapGetter is a basic implementation of PMapGetter
type StandardPMapGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (any, error)
}
// Get retrieves a pcommon.Map value.
// If the value is not a pcommon.Map a new TypeError is returned.
// If there is an error getting the value it will be returned.
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{}, fmt.Errorf("error getting value in %T: %w", g, err)
}
if val == nil {
return pcommon.Map{}, TypeError("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{}, TypeError(fmt.Sprintf("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{}, TypeError(fmt.Sprintf("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) (any, error)
}
func (g StandardStringLikeGetter[K]) Get(ctx context.Context, tCtx K) (*string, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return nil, fmt.Errorf("error getting value in %T: %w", g, 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, TypeError(fmt.Sprintf("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) (any, error)
}
func (g StandardFloatLikeGetter[K]) Get(ctx context.Context, tCtx K) (*float64, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return nil, fmt.Errorf("error getting value in %T: %w", g, 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, TypeError(fmt.Sprintf("unsupported value type: %v", v.Type()))
}
default:
return nil, TypeError(fmt.Sprintf("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) (any, error)
}
func (g StandardIntLikeGetter[K]) Get(ctx context.Context, tCtx K) (*int64, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return nil, fmt.Errorf("error getting value in %T: %w", g, 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, TypeError(fmt.Sprintf("unsupported value type: %v", v.Type()))
}
default:
return nil, TypeError(fmt.Sprintf("unsupported type: %T", v))
}
return &result, nil
}
// BoolLikeGetter is a Getter that returns a bool by converting the underlying value to a bool if necessary.
type BoolLikeGetter[K any] interface {
// Get retrieves a bool value.
// Unlike `BoolGetter`, the expectation is that the underlying value is converted to a bool if possible.
// If the value cannot be converted to a bool, nil and an error are returned.
// If the value is nil, nil is returned without an error.
Get(ctx context.Context, tCtx K) (*bool, error)
}
type StandardBoolLikeGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (any, error)
}
func (g StandardBoolLikeGetter[K]) Get(ctx context.Context, tCtx K) (*bool, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return nil, fmt.Errorf("error getting value in %T: %w", g, err)
}
if val == nil {
return nil, nil
}
var result bool
switch v := val.(type) {
case bool:
result = v
case int:
result = v != 0
case int64:
result = v != 0
case string:
result, err = strconv.ParseBool(v)
if err != nil {
return nil, err
}
case float64:
result = v != 0.0
case pcommon.Value:
switch v.Type() {
case pcommon.ValueTypeBool:
result = v.Bool()
case pcommon.ValueTypeInt:
result = v.Int() != 0
case pcommon.ValueTypeStr:
result, err = strconv.ParseBool(v.Str())
if err != nil {
return nil, err
}
case pcommon.ValueTypeDouble:
result = v.Double() != 0.0
default:
return nil, TypeError(fmt.Sprintf("unsupported value type: %v", v.Type()))
}
default:
return nil, TypeError(fmt.Sprintf("unsupported type: %T", val))
}
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((*EnumSymbol)(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 {
np, err := newPath[K](eL.Path.Fields)
if err != nil {
return nil, err
}
return p.parsePath(np)
}
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
}
// TimeGetter is a Getter that must return a time.Time.
type TimeGetter[K any] interface {
// Get retrieves a time.Time value.
Get(ctx context.Context, tCtx K) (time.Time, error)
}
// StandardTimeGetter is a basic implementation of TimeGetter
type StandardTimeGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (any, error)
}
// Get retrieves a time.Time value.
// If the value is not a time.Time, a new TypeError is returned.
// If there is an error getting the value it will be returned.
func (g StandardTimeGetter[K]) Get(ctx context.Context, tCtx K) (time.Time, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return time.Time{}, fmt.Errorf("error getting value in %T: %w", g, err)
}
if val == nil {
return time.Time{}, TypeError("expected time but got nil")
}
switch v := val.(type) {
case time.Time:
return v, nil
default:
return time.Time{}, TypeError(fmt.Sprintf("expected time but got %T", val))
}
}
// DurationGetter is a Getter that must return a time.Duration.
type DurationGetter[K any] interface {
// Get retrieves a time.Duration value.
Get(ctx context.Context, tCtx K) (time.Duration, error)
}
// StandardDurationGetter is a basic implementation of DurationGetter
type StandardDurationGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (any, error)
}
// Get retrieves an time.Duration value.
// If the value is not an time.Duration a new TypeError is returned.
// If there is an error getting the value it will be returned.
func (g StandardDurationGetter[K]) Get(ctx context.Context, tCtx K) (time.Duration, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return 0, fmt.Errorf("error getting value in %T: %w", g, err)
}
if val == nil {
return 0, TypeError("expected duration but got nil")
}
switch v := val.(type) {
case time.Duration:
return v, nil
default:
return 0, TypeError(fmt.Sprintf("expected duration but got %T", val))
}
}