/
expr_string.go
167 lines (141 loc) · 4.53 KB
/
expr_string.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package helper // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
import (
"fmt"
"os"
"strings"
"sync"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/ast"
"github.com/expr-lang/expr/vm"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/errors"
)
// ExprStringConfig is a string that represents an expression
type ExprStringConfig string
const (
exprStartToken = "EXPR("
exprEndToken = ")"
)
// Build creates an ExprStr string from the specified config
func (e ExprStringConfig) Build() (*ExprString, error) {
s := string(e)
rangeStart := 0
subStrings := make([]string, 0, 4)
subExprStrings := make([]string, 0, 4)
for {
rangeEnd := len(s)
// Find the first instance of the start token
indexStart := strings.Index(s[rangeStart:rangeEnd], exprStartToken)
if indexStart == -1 {
// Start token does not exist in the remainder of the string,
// so treat the rest as a string literal
subStrings = append(subStrings, s[rangeStart:])
break
}
indexStart = rangeStart + indexStart
// Restrict our end token search range to the next instance of the start token
nextIndexStart := strings.Index(s[indexStart+len(exprStartToken):], exprStartToken)
if nextIndexStart == -1 {
rangeEnd = len(s)
} else {
rangeEnd = indexStart + len(exprStartToken) + nextIndexStart
}
// Greedily search for the last end token in the search range
indexEnd := strings.LastIndex(s[indexStart:rangeEnd], exprEndToken)
if indexEnd == -1 {
// End token does not exist before the next start token
// or end of expression string, so treat the remainder of the string
// as a string literal
subStrings = append(subStrings, s[rangeStart:])
break
}
indexEnd = indexStart + indexEnd
// Unscope the indexes and add the partitioned strings
subStrings = append(subStrings, s[rangeStart:indexStart])
subExprStrings = append(subExprStrings, s[indexStart+len(exprStartToken):indexEnd])
// Reset the starting range and finish if it reaches the end of the string
rangeStart = indexEnd + len(exprEndToken)
if rangeStart > len(s) {
break
}
}
subExprs := make([]*vm.Program, 0, len(subExprStrings))
for _, subExprString := range subExprStrings {
program, err := expr.Compile(subExprString, expr.AllowUndefinedVariables(), expr.Patch(&patcher{}))
if err != nil {
return nil, errors.Wrap(err, "compile embedded expression")
}
subExprs = append(subExprs, program)
}
return &ExprString{
SubStrings: subStrings,
SubExprs: subExprs,
}, nil
}
func ExprCompile(input string) (*vm.Program, error) {
return expr.Compile(input, expr.AllowUndefinedVariables(), expr.Patch(&patcher{}))
}
func ExprCompileBool(input string) (*vm.Program, error) {
return expr.Compile(input, expr.AllowUndefinedVariables(), expr.Patch(&patcher{}), expr.AsBool())
}
// An ExprString is made up of a list of string literals
// interleaved with expressions. len(SubStrings) == len(SubExprs) + 1
type ExprString struct {
SubStrings []string
SubExprs []*vm.Program
}
// Render will render an ExprString as a string
func (e *ExprString) Render(env map[string]any) (string, error) {
var b strings.Builder
for i := 0; i < len(e.SubExprs); i++ {
b.WriteString(e.SubStrings[i])
out, err := vm.Run(e.SubExprs[i], env)
if err != nil {
return "", errors.Wrap(err, "render embedded expression")
}
outString, ok := out.(string)
if !ok {
return "", fmt.Errorf("embedded expression returned non-string %v", out)
}
b.WriteString(outString)
}
b.WriteString(e.SubStrings[len(e.SubStrings)-1])
return b.String(), nil
}
type patcher struct{}
func (p *patcher) Visit(node *ast.Node) {
n, ok := (*node).(*ast.CallNode)
if !ok {
return
}
c, ok := (n.Callee).(*ast.IdentifierNode)
if !ok {
return
}
if c.Value == "env" {
c.Value = "os_env_func"
}
}
var envPool = sync.Pool{
New: func() any {
return map[string]any{
"os_env_func": os.Getenv,
}
},
}
// GetExprEnv returns a map of key/value pairs that can be be used to evaluate an expression
func GetExprEnv(e *entry.Entry) map[string]any {
env := envPool.Get().(map[string]any)
env["$"] = e.Body
env["body"] = e.Body
env["attributes"] = e.Attributes
env["resource"] = e.Resource
env["timestamp"] = e.Timestamp
return env
}
// PutExprEnv adds a key/value pair that will can be used to evaluate an expression
func PutExprEnv(e map[string]any) {
envPool.Put(e)
}