-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
expression.go
117 lines (97 loc) · 2.72 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
// 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
//
// http://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/oteltransformationlanguage/ottl"
import (
"fmt"
"go.opentelemetry.io/collector/pdata/pcommon"
)
type TransformContext interface {
GetItem() interface{}
GetInstrumentationScope() pcommon.InstrumentationScope
GetResource() pcommon.Resource
}
type ExprFunc func(ctx TransformContext) interface{}
type Getter interface {
Get(ctx TransformContext) interface{}
}
type Setter interface {
Set(ctx TransformContext, val interface{})
}
type GetSetter interface {
Getter
Setter
}
type StandardGetSetter struct {
Getter func(ctx TransformContext) interface{}
Setter func(ctx TransformContext, val interface{})
}
func (path StandardGetSetter) Get(ctx TransformContext) interface{} {
return path.Getter(ctx)
}
func (path StandardGetSetter) Set(ctx TransformContext, val interface{}) {
path.Setter(ctx, val)
}
type literal struct {
value interface{}
}
func (l literal) Get(ctx TransformContext) interface{} {
return l.value
}
type exprGetter struct {
expr ExprFunc
}
func (g exprGetter) Get(ctx TransformContext) interface{} {
return g.expr(ctx)
}
func (p *Parser) newGetter(val Value) (Getter, error) {
if val.IsNil != nil && *val.IsNil {
return &literal{value: nil}, nil
}
if s := val.String; s != nil {
return &literal{value: *s}, nil
}
if f := val.Float; f != nil {
return &literal{value: *f}, nil
}
if i := val.Int; i != nil {
return &literal{value: *i}, nil
}
if b := val.Bool; b != nil {
return &literal{value: bool(*b)}, nil
}
if b := val.Bytes; b != nil {
return &literal{value: ([]byte)(*b)}, nil
}
if val.Enum != nil {
enum, err := p.enumParser(val.Enum)
if err != nil {
return nil, err
}
return &literal{value: int64(*enum)}, nil
}
if val.Path != nil {
return p.pathParser(val.Path)
}
if val.Invocation == 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")
}
call, err := p.newFunctionCall(*val.Invocation)
if err != nil {
return nil, err
}
return &exprGetter{
expr: call,
}, nil
}