-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
expression.go
156 lines (131 loc) · 3.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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ottl // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
import (
"context"
"fmt"
)
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, tCx K) (interface{}, error)
Setter func(ctx context.Context, tCx K, val interface{}) error
}
func (path StandardGetSetter[K]) Get(ctx context.Context, tCx K) (interface{}, error) {
return path.Getter(ctx, tCx)
}
func (path StandardGetSetter[K]) Set(ctx context.Context, tCx K, val interface{}) error {
return path.Setter(ctx, tCx, 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]
}
func (g exprGetter[K]) Get(ctx context.Context, tCtx K) (interface{}, error) {
return g.expr.Eval(ctx, tCtx)
}
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
}
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 {
call, err := p.newFunctionCall(invocation{
Function: eL.Converter.Function,
Arguments: eL.Converter.Arguments,
})
if err != nil {
return nil, err
}
return &exprGetter[K]{
expr: call,
}, nil
}
}
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)
}