-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
math.go
179 lines (167 loc) · 4.47 KB
/
math.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
// 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"
"time"
)
func (p *Parser[K]) evaluateMathExpression(expr *mathExpression) (Getter[K], error) {
mainGetter, err := p.evaluateAddSubTerm(expr.Left)
if err != nil {
return nil, err
}
for _, rhs := range expr.Right {
getter, err := p.evaluateAddSubTerm(rhs.Term)
if err != nil {
return nil, err
}
mainGetter = attemptMathOperation(mainGetter, rhs.Operator, getter)
}
return mainGetter, nil
}
func (p *Parser[K]) evaluateAddSubTerm(term *addSubTerm) (Getter[K], error) {
mainGetter, err := p.evaluateMathValue(term.Left)
if err != nil {
return nil, err
}
for _, rhs := range term.Right {
getter, err := p.evaluateMathValue(rhs.Value)
if err != nil {
return nil, err
}
mainGetter = attemptMathOperation(mainGetter, rhs.Operator, getter)
}
return mainGetter, nil
}
func (p *Parser[K]) evaluateMathValue(val *mathValue) (Getter[K], error) {
switch {
case val.Literal != nil:
return p.newGetter(value{Literal: val.Literal})
case val.SubExpression != nil:
return p.evaluateMathExpression(val.SubExpression)
}
return nil, fmt.Errorf("unsupported mathematical value %v", val)
}
func attemptMathOperation[K any](lhs Getter[K], op mathOp, rhs Getter[K]) Getter[K] {
return exprGetter[K]{
expr: Expr[K]{
exprFunc: func(ctx context.Context, tCtx K) (any, error) {
x, err := lhs.Get(ctx, tCtx)
if err != nil {
return nil, err
}
y, err := rhs.Get(ctx, tCtx)
if err != nil {
return nil, err
}
switch newX := x.(type) {
case int64:
switch newY := y.(type) {
case int64:
result, err := performOp[int64](newX, newY, op)
if err != nil {
return nil, err
}
return result, nil
case float64:
result, err := performOp[float64](float64(newX), newY, op)
if err != nil {
return nil, err
}
return result, nil
default:
return nil, fmt.Errorf("%v must be int64 or float64", y)
}
case float64:
switch newY := y.(type) {
case int64:
result, err := performOp[float64](newX, float64(newY), op)
if err != nil {
return nil, err
}
return result, nil
case float64:
result, err := performOp[float64](newX, newY, op)
if err != nil {
return nil, err
}
return result, nil
default:
return nil, fmt.Errorf("%v must be int64 or float64", y)
}
case time.Time:
return performOpTime(newX, y, op)
case time.Duration:
return performOpDuration(newX, y, op)
default:
return nil, fmt.Errorf("%v must be int64, float64, time.Time or time.Duration", x)
}
},
},
}
}
func performOpTime(x time.Time, y any, op mathOp) (any, error) {
switch op {
case add:
switch newY := y.(type) {
case time.Duration:
result := x.Add(newY)
return result, nil
default:
return nil, fmt.Errorf("time.Time must be added to time.Duration; found %v instead", y)
}
case sub:
switch newY := y.(type) {
case time.Time:
result := x.Sub(newY)
return result, nil
case time.Duration:
result := x.Add(-1 * newY)
return result, nil
default:
return nil, fmt.Errorf("time.Time or time.Duration must be subtracted from time.Time; found %v instead", y)
}
}
return nil, fmt.Errorf("only addition and subtraction supported for time.Time and time.Duration")
}
func performOpDuration(x time.Duration, y any, op mathOp) (any, error) {
switch op {
case add:
switch newY := y.(type) {
case time.Duration:
result := x + newY
return result, nil
case time.Time:
result := newY.Add(x)
return result, nil
default:
return nil, fmt.Errorf("time.Duration must be added to time.Duration or time.Time; found %v instead", y)
}
case sub:
switch newY := y.(type) {
case time.Duration:
result := x - newY
return result, nil
default:
return nil, fmt.Errorf("time.Duration must be subtracted from time.Duration; found %v instead", y)
}
}
return nil, fmt.Errorf("only addition and subtraction supported for time.Time and time.Duration")
}
func performOp[N int64 | float64](x N, y N, op mathOp) (N, error) {
switch op {
case add:
return x + y, nil
case sub:
return x - y, nil
case mult:
return x * y, nil
case div:
if y == 0 {
return 0, fmt.Errorf("attempted to divide by 0")
}
return x / y, nil
}
return 0, fmt.Errorf("invalid operation %v", op)
}