-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
boolean_value.go
199 lines (179 loc) · 4.94 KB
/
boolean_value.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ottl // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
import (
"context"
"fmt"
)
// boolExpressionEvaluator is a function that returns the result.
type boolExpressionEvaluator[K any] func(ctx context.Context, tCtx K) (bool, error)
type BoolExpr[K any] struct {
boolExpressionEvaluator[K]
}
func (e BoolExpr[K]) Eval(ctx context.Context, tCtx K) (bool, error) {
return e.boolExpressionEvaluator(ctx, tCtx)
}
//nolint:unparam
func not[K any](original BoolExpr[K]) (BoolExpr[K], error) {
return BoolExpr[K]{func(ctx context.Context, tCtx K) (bool, error) {
result, err := original.Eval(ctx, tCtx)
return !result, err
}}, nil
}
func alwaysTrue[K any](context.Context, K) (bool, error) {
return true, nil
}
func alwaysFalse[K any](context.Context, K) (bool, error) {
return false, nil
}
// builds a function that returns a short-circuited result of ANDing
// boolExpressionEvaluator funcs
func andFuncs[K any](funcs []BoolExpr[K]) BoolExpr[K] {
return BoolExpr[K]{func(ctx context.Context, tCtx K) (bool, error) {
for _, f := range funcs {
result, err := f.Eval(ctx, tCtx)
if err != nil {
return false, err
}
if !result {
return false, nil
}
}
return true, nil
}}
}
// builds a function that returns a short-circuited result of ORing
// boolExpressionEvaluator funcs
func orFuncs[K any](funcs []BoolExpr[K]) BoolExpr[K] {
return BoolExpr[K]{func(ctx context.Context, tCtx K) (bool, error) {
for _, f := range funcs {
result, err := f.Eval(ctx, tCtx)
if err != nil {
return false, err
}
if result {
return true, nil
}
}
return false, nil
}}
}
func (p *Parser[K]) newComparisonEvaluator(comparison *comparison) (BoolExpr[K], error) {
if comparison == nil {
return BoolExpr[K]{alwaysTrue[K]}, nil
}
left, err := p.newGetter(comparison.Left)
if err != nil {
return BoolExpr[K]{}, err
}
right, err := p.newGetter(comparison.Right)
if err != nil {
return BoolExpr[K]{}, err
}
// The parser ensures that we'll never get an invalid comparison.Op, so we don't have to check that case.
return BoolExpr[K]{func(ctx context.Context, tCtx K) (bool, error) {
a, leftErr := left.Get(ctx, tCtx)
if leftErr != nil {
return false, leftErr
}
b, rightErr := right.Get(ctx, tCtx)
if rightErr != nil {
return false, rightErr
}
return p.compare(a, b, comparison.Op), nil
}}, nil
}
func (p *Parser[K]) newBoolExpr(expr *booleanExpression) (BoolExpr[K], error) {
if expr == nil {
return BoolExpr[K]{alwaysTrue[K]}, nil
}
f, err := p.newBooleanTermEvaluator(expr.Left)
if err != nil {
return BoolExpr[K]{}, err
}
funcs := []BoolExpr[K]{f}
for _, rhs := range expr.Right {
f, err := p.newBooleanTermEvaluator(rhs.Term)
if err != nil {
return BoolExpr[K]{}, err
}
funcs = append(funcs, f)
}
return orFuncs(funcs), nil
}
func (p *Parser[K]) newBooleanTermEvaluator(term *term) (BoolExpr[K], error) {
if term == nil {
return BoolExpr[K]{alwaysTrue[K]}, nil
}
f, err := p.newBooleanValueEvaluator(term.Left)
if err != nil {
return BoolExpr[K]{}, err
}
funcs := []BoolExpr[K]{f}
for _, rhs := range term.Right {
f, err := p.newBooleanValueEvaluator(rhs.Value)
if err != nil {
return BoolExpr[K]{}, err
}
funcs = append(funcs, f)
}
return andFuncs(funcs), nil
}
func (p *Parser[K]) newBooleanValueEvaluator(value *booleanValue) (BoolExpr[K], error) {
if value == nil {
return BoolExpr[K]{alwaysTrue[K]}, nil
}
var boolExpr BoolExpr[K]
var err error
switch {
case value.Comparison != nil:
boolExpr, err = p.newComparisonEvaluator(value.Comparison)
if err != nil {
return BoolExpr[K]{}, err
}
case value.ConstExpr != nil:
switch {
case value.ConstExpr.Boolean != nil:
if *value.ConstExpr.Boolean {
boolExpr = BoolExpr[K]{alwaysTrue[K]}
} else {
boolExpr = BoolExpr[K]{alwaysFalse[K]}
}
case value.ConstExpr.Converter != nil:
boolExpr, err = p.newConverterEvaluator(*value.ConstExpr.Converter)
if err != nil {
return BoolExpr[K]{}, err
}
default:
return BoolExpr[K]{}, fmt.Errorf("unhandled boolean operation %v", value)
}
case value.SubExpr != nil:
boolExpr, err = p.newBoolExpr(value.SubExpr)
if err != nil {
return BoolExpr[K]{}, err
}
default:
return BoolExpr[K]{}, fmt.Errorf("unhandled boolean operation %v", value)
}
if value.Negation != nil {
return not(boolExpr)
}
return boolExpr, nil
}
func (p *Parser[K]) newConverterEvaluator(c converter) (BoolExpr[K], error) {
getter, err := p.newGetterFromConverter(c)
if err != nil {
return BoolExpr[K]{}, err
}
return BoolExpr[K]{func(ctx context.Context, tCtx K) (bool, error) {
result, err := getter.Get(ctx, tCtx)
if err != nil {
return false, err
}
boolResult, ok := result.(bool)
if !ok {
return false, fmt.Errorf("value returned from Converter in constant expression must be bool but got %T", result)
}
return boolResult, nil
}}, nil
}