-
Notifications
You must be signed in to change notification settings - Fork 812
/
arithmetic.go
294 lines (262 loc) · 7.63 KB
/
arithmetic.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package jmespath
import (
"fmt"
"math"
"reflect"
"time"
"gopkg.in/inf.v0"
"k8s.io/apimachinery/pkg/api/resource"
)
type Operand interface {
Add(interface{}, string) (interface{}, error)
Subtract(interface{}) (interface{}, error)
Multiply(interface{}) (interface{}, error)
Divide(interface{}) (interface{}, error)
Modulo(interface{}) (interface{}, error)
}
type Quantity struct {
resource.Quantity
}
type Duration struct {
time.Duration
}
type Scalar struct {
float64
}
func ParseArithemticOperands(arguments []interface{}, operator string) (Operand, Operand, error) {
op := [2]Operand{nil, nil}
for i := 0; i < 2; i++ {
if tmp, err := validateArg(divide, arguments, i, reflect.Float64); err == nil {
var sc Scalar
sc.float64 = tmp.Float()
op[i] = sc
} else if tmp, err = validateArg(divide, arguments, i, reflect.String); err == nil {
if q, err := resource.ParseQuantity(tmp.String()); err == nil {
op[i] = Quantity{Quantity: q}
} else if d, err := time.ParseDuration(tmp.String()); err == nil {
op[i] = Duration{Duration: d}
}
}
}
if op[0] == nil || op[1] == nil {
return nil, nil, formatError(genericError, operator, "invalid operands")
}
return op[0], op[1], nil
}
// Quantity +|- Quantity -> Quantity
// Quantity +|- Duration|Scalar -> error
// Duration +|- Duration -> Duration
// Duration +|- Quantity|Scalar -> error
// Scalar +|- Scalar -> Scalar
// Scalar +|- Quantity|Duration -> error
func (op1 Quantity) Add(op2 interface{}, operator string) (interface{}, error) {
switch v := op2.(type) {
case Quantity:
op1.Quantity.Add(v.Quantity)
return op1.String(), nil
default:
return nil, formatError(typeMismatchError, operator)
}
}
func (op1 Duration) Add(op2 interface{}, operator string) (interface{}, error) {
switch v := op2.(type) {
case Duration:
return (op1.Duration + v.Duration).String(), nil
default:
return nil, formatError(typeMismatchError, operator)
}
}
func (op1 Scalar) Add(op2 interface{}, operator string) (interface{}, error) {
switch v := op2.(type) {
case Scalar:
return op1.float64 + v.float64, nil
default:
return nil, formatError(typeMismatchError, operator)
}
}
func (op1 Quantity) Subtract(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Quantity:
op1.Quantity.Sub(v.Quantity)
return op1.String(), nil
default:
return nil, formatError(typeMismatchError, subtract)
}
}
func (op1 Duration) Subtract(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Duration:
return (op1.Duration - v.Duration).String(), nil
default:
return nil, formatError(typeMismatchError, subtract)
}
}
func (op1 Scalar) Subtract(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Scalar:
return op1.float64 - v.float64, nil
default:
return nil, formatError(typeMismatchError, subtract)
}
}
// Quantity * Quantity|Duration -> error
// Quantity * Scalar -> Quantity
// Duration * Quantity|Duration -> error
// Duration * Scalar -> Duration
// Scalar * Scalar -> Scalar
// Scalar * Quantity -> Quantity
// Scalar * Duration -> Duration
func (op1 Quantity) Multiply(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Scalar:
q, err := resource.ParseQuantity(fmt.Sprintf("%v", v.float64))
if err != nil {
return nil, err
}
var prod inf.Dec
prod.Mul(op1.Quantity.AsDec(), q.AsDec())
return resource.NewDecimalQuantity(prod, op1.Quantity.Format).String(), nil
default:
return nil, formatError(typeMismatchError, multiply)
}
}
func (op1 Duration) Multiply(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Scalar:
seconds := op1.Seconds() * v.float64
return time.Duration(seconds * float64(time.Second)).String(), nil
default:
return nil, formatError(typeMismatchError, multiply)
}
}
func (op1 Scalar) Multiply(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Scalar:
return op1.float64 * v.float64, nil
case Quantity:
return v.Multiply(op1)
case Duration:
return v.Multiply(op1)
default:
return nil, formatError(typeMismatchError, multiply)
}
}
// Quantity / Duration -> error
// Quantity / Quantity -> Scalar
// Quantity / Scalar -> Quantity
// Duration / Quantity -> error
// Duration / Duration -> Scalar
// Duration / Scalar -> Duration
// Scalar / Scalar -> Scalar
// Scalar / Quantity -> error
// Scalar / Duration -> error
func (op1 Quantity) Divide(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Quantity:
divisor := v.AsApproximateFloat64()
if divisor == 0 {
return nil, formatError(zeroDivisionError, divide)
}
dividend := op1.AsApproximateFloat64()
return dividend / divisor, nil
case Scalar:
if v.float64 == 0 {
return nil, formatError(zeroDivisionError, divide)
}
q, err := resource.ParseQuantity(fmt.Sprintf("%v", v.float64))
if err != nil {
return nil, err
}
var quo inf.Dec
scale := inf.Scale(math.Max(float64(op1.AsDec().Scale()), float64(q.AsDec().Scale())))
quo.QuoRound(op1.AsDec(), q.AsDec(), scale, inf.RoundDown)
return resource.NewDecimalQuantity(quo, op1.Quantity.Format).String(), nil
default:
return nil, formatError(typeMismatchError, divide)
}
}
func (op1 Duration) Divide(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Duration:
if v.Seconds() == 0 {
return nil, formatError(zeroDivisionError, divide)
}
return op1.Seconds() / v.Seconds(), nil
case Scalar:
if v.float64 == 0 {
return nil, formatError(zeroDivisionError, divide)
}
seconds := op1.Seconds() / v.float64
return time.Duration(seconds * float64(time.Second)).String(), nil
default:
return nil, formatError(typeMismatchError, divide)
}
}
func (op1 Scalar) Divide(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Scalar:
if v.float64 == 0 {
return nil, formatError(zeroDivisionError, divide)
}
return op1.float64 / v.float64, nil
default:
return nil, formatError(typeMismatchError, divide)
}
}
// Quantity % Duration|Scalar -> error
// Quantity % Quantity -> Quantity
// Duration % Quantity|Scalar -> error
// Duration % Duration -> Duration
// Scalar % Quantity|Duration -> error
// Scalar % Scalar -> Scalar
func (op1 Quantity) Modulo(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Quantity:
f1 := op1.ToDec().AsApproximateFloat64()
f2 := v.ToDec().AsApproximateFloat64()
i1 := int64(f1)
i2 := int64(f2)
if f1 != float64(i1) {
return nil, formatError(nonIntModuloError, modulo)
}
if f2 != float64(i2) {
return nil, formatError(nonIntModuloError, modulo)
}
if i2 == 0 {
return nil, formatError(zeroDivisionError, modulo)
}
return resource.NewQuantity(i1%i2, op1.Quantity.Format).String(), nil
default:
return nil, formatError(typeMismatchError, modulo)
}
}
func (op1 Duration) Modulo(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Duration:
if v.Duration == 0 {
return nil, formatError(zeroDivisionError, modulo)
}
return (op1.Duration % v.Duration).String(), nil
default:
return nil, formatError(typeMismatchError, modulo)
}
}
func (op1 Scalar) Modulo(op2 interface{}) (interface{}, error) {
switch v := op2.(type) {
case Scalar:
val1 := int64(op1.float64)
val2 := int64(v.float64)
if op1.float64 != float64(val1) {
return nil, formatError(nonIntModuloError, modulo)
}
if v.float64 != float64(val2) {
return nil, formatError(nonIntModuloError, modulo)
}
if val2 == 0 {
return nil, formatError(zeroDivisionError, modulo)
}
return float64(val1 % val2), nil
default:
return nil, formatError(typeMismatchError, modulo)
}
}