-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathexpr_string.go
139 lines (118 loc) · 3.73 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
package helper
import (
"fmt"
"os"
"strings"
"sync"
"github.com/antonmedv/expr"
"github.com/antonmedv/expr/vm"
"github.com/observiq/stanza/entry"
"github.com/observiq/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())
if err != nil {
return nil, errors.Wrap(err, "compile embedded expression")
}
subExprs = append(subExprs, program)
}
return &ExprString{
SubStrings: subStrings,
SubExprs: subExprs,
}, nil
}
// 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]interface{}) (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
}
var envPool = sync.Pool{
New: func() interface{} {
return map[string]interface{}{
"env": 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]interface{} {
env := envPool.Get().(map[string]interface{})
env["$"] = e.Record
env["$record"] = e.Record
env["$labels"] = e.Labels
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]interface{}) {
envPool.Put(e)
}