2021-12-07 15:44:46 +00:00
|
|
|
package jmespath
|
|
|
|
|
|
|
|
import (
|
2022-12-05 11:15:32 +00:00
|
|
|
"errors"
|
2021-12-07 15:44:46 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/inf.v0"
|
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Operand interface {
|
|
|
|
Add(interface{}) (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
|
|
|
|
}
|
|
|
|
|
2022-12-05 11:15:32 +00:00
|
|
|
var errTypeMismatch = errors.New("types mismatch")
|
|
|
|
|
2021-12-07 15:44:46 +00:00
|
|
|
func ParseArithemticOperands(arguments []interface{}, operator string) (Operand, Operand, error) {
|
|
|
|
op := [2]Operand{nil, nil}
|
|
|
|
t := [2]int{0, 0}
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
tmp, err := validateArg(divide, arguments, i, reflect.Float64)
|
|
|
|
if err == nil {
|
|
|
|
var sc Scalar
|
|
|
|
sc.float64 = tmp.Float()
|
|
|
|
op[i] = sc
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp, err = validateArg(divide, arguments, i, reflect.String)
|
|
|
|
if err == nil {
|
|
|
|
var q Quantity
|
|
|
|
q.Quantity, err = resource.ParseQuantity(tmp.String())
|
|
|
|
if err == nil {
|
|
|
|
op[i] = q
|
|
|
|
t[i] = 1
|
|
|
|
} else {
|
|
|
|
var d Duration
|
|
|
|
d.Duration, err = time.ParseDuration(tmp.String())
|
|
|
|
if err == nil {
|
|
|
|
op[i] = d
|
|
|
|
t[i] = 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if op[0] == nil || op[1] == nil || t[0]|t[1] == 3 {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, nil, formatError(genericError, operator, "invalid operands")
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return op[0], op[1], nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 11:15:32 +00:00
|
|
|
// Quantity +|- Quantity -> Quantity
|
|
|
|
// Quantity +|- Duration|Scalar -> error
|
|
|
|
// Duration +|- Duration -> Duration
|
|
|
|
// Duration +|- Quantity|Scalar -> error
|
|
|
|
// Scalar +|- Scalar -> Scalar
|
|
|
|
// Scalar +|- Quantity|Duration -> error
|
|
|
|
|
2021-12-07 15:44:46 +00:00
|
|
|
func (op1 Quantity) Add(op2 interface{}) (interface{}, error) {
|
|
|
|
switch v := op2.(type) {
|
|
|
|
case Quantity:
|
|
|
|
op1.Quantity.Add(v.Quantity)
|
|
|
|
return op1.String(), nil
|
2022-12-05 11:15:32 +00:00
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op1 Duration) Add(op2 interface{}) (interface{}, error) {
|
|
|
|
switch v := op2.(type) {
|
|
|
|
case Duration:
|
2022-12-05 11:15:32 +00:00
|
|
|
return (op1.Duration + v.Duration).String(), nil
|
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op1 Scalar) Add(op2 interface{}) (interface{}, error) {
|
|
|
|
switch v := op2.(type) {
|
|
|
|
case Scalar:
|
|
|
|
return op1.float64 + v.float64, nil
|
2022-12-05 11:15:32 +00:00
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op1 Quantity) Subtract(op2 interface{}) (interface{}, error) {
|
|
|
|
switch v := op2.(type) {
|
|
|
|
case Quantity:
|
|
|
|
op1.Quantity.Sub(v.Quantity)
|
|
|
|
return op1.String(), nil
|
2022-12-05 11:15:32 +00:00
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op1 Duration) Subtract(op2 interface{}) (interface{}, error) {
|
|
|
|
switch v := op2.(type) {
|
|
|
|
case Duration:
|
2022-12-05 11:15:32 +00:00
|
|
|
return (op1.Duration - v.Duration).String(), nil
|
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op1 Scalar) Subtract(op2 interface{}) (interface{}, error) {
|
|
|
|
switch v := op2.(type) {
|
|
|
|
case Scalar:
|
|
|
|
return op1.float64 - v.float64, nil
|
2022-12-05 11:15:32 +00:00
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 11:15:32 +00:00
|
|
|
// Quantity * Quantity|Duration -> error
|
|
|
|
// Quantity * Scalar -> Quantity
|
|
|
|
|
|
|
|
// Duration * Quantity|Duration -> error
|
|
|
|
// Duration * Scalar -> Duration
|
|
|
|
|
|
|
|
// Scalar * Scalar -> Scalar
|
|
|
|
// Scalar * Quantity -> Quantity
|
|
|
|
// Scalar * Duration -> Duration
|
|
|
|
|
2021-12-07 15:44:46 +00:00
|
|
|
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
|
2022-12-05 11:15:32 +00:00
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op1 Duration) Multiply(op2 interface{}) (interface{}, error) {
|
|
|
|
switch v := op2.(type) {
|
|
|
|
case Scalar:
|
2022-12-05 11:15:32 +00:00
|
|
|
seconds := op1.Seconds() * v.float64
|
|
|
|
return time.Duration(seconds * float64(time.Second)).String(), nil
|
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 11:15:32 +00:00
|
|
|
// 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
|
|
|
|
|
2021-12-07 15:44:46 +00:00
|
|
|
func (op1 Quantity) Divide(op2 interface{}) (interface{}, error) {
|
|
|
|
switch v := op2.(type) {
|
|
|
|
case Quantity:
|
2023-02-06 10:21:30 +00:00
|
|
|
divisor := v.AsApproximateFloat64()
|
|
|
|
if divisor == 0 {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, formatError(zeroDivisionError, divide)
|
2022-01-31 17:45:20 +00:00
|
|
|
}
|
2023-02-06 10:21:30 +00:00
|
|
|
dividend := op1.AsApproximateFloat64()
|
|
|
|
return dividend / divisor, nil
|
2021-12-07 15:44:46 +00:00
|
|
|
case Scalar:
|
2022-01-31 17:45:20 +00:00
|
|
|
if v.float64 == 0 {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, formatError(zeroDivisionError, divide)
|
2022-01-31 17:45:20 +00:00
|
|
|
}
|
2021-12-07 15:44:46 +00:00
|
|
|
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
|
2022-12-05 11:15:32 +00:00
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op1 Duration) Divide(op2 interface{}) (interface{}, error) {
|
|
|
|
switch v := op2.(type) {
|
|
|
|
case Duration:
|
|
|
|
if v.Seconds() == 0 {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, formatError(zeroDivisionError, divide)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
2022-12-05 11:15:32 +00:00
|
|
|
return op1.Seconds() / v.Seconds(), nil
|
2021-12-07 15:44:46 +00:00
|
|
|
case Scalar:
|
|
|
|
if v.float64 == 0 {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, formatError(zeroDivisionError, divide)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
2022-12-05 11:15:32 +00:00
|
|
|
seconds := op1.Seconds() / v.float64
|
|
|
|
return time.Duration(seconds * float64(time.Second)).String(), nil
|
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op1 Scalar) Divide(op2 interface{}) (interface{}, error) {
|
|
|
|
switch v := op2.(type) {
|
|
|
|
case Scalar:
|
|
|
|
if v.float64 == 0 {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, formatError(zeroDivisionError, divide)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
return op1.float64 / v.float64, nil
|
2022-12-05 11:15:32 +00:00
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 11:15:32 +00:00
|
|
|
// Quantity % Duration|Scalar -> error
|
|
|
|
// Quantity % Quantity -> Quantity
|
2021-12-07 15:44:46 +00:00
|
|
|
|
2022-12-05 11:15:32 +00:00
|
|
|
// Duration % Quantity|Scalar -> error
|
|
|
|
// Duration % Duration -> Duration
|
2021-12-07 15:44:46 +00:00
|
|
|
|
2022-12-05 11:15:32 +00:00
|
|
|
// Scalar % Quantity|Duration -> error
|
|
|
|
// Scalar % Scalar -> Scalar
|
2021-12-07 15:44:46 +00:00
|
|
|
|
2022-12-05 11:15:32 +00:00
|
|
|
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) {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, formatError(nonIntModuloError, modulo)
|
2022-12-05 11:15:32 +00:00
|
|
|
}
|
|
|
|
if f2 != float64(i2) {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, formatError(nonIntModuloError, modulo)
|
2022-12-05 11:15:32 +00:00
|
|
|
}
|
|
|
|
if i2 == 0 {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, formatError(zeroDivisionError, modulo)
|
2022-12-05 11:15:32 +00:00
|
|
|
}
|
|
|
|
return resource.NewQuantity(i1%i2, op1.Quantity.Format).String(), nil
|
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op1 Duration) Modulo(op2 interface{}) (interface{}, error) {
|
2022-12-05 11:15:32 +00:00
|
|
|
switch v := op2.(type) {
|
|
|
|
case Duration:
|
|
|
|
if v.Duration == 0 {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, formatError(zeroDivisionError, modulo)
|
2022-12-05 11:15:32 +00:00
|
|
|
}
|
|
|
|
return (op1.Duration % v.Duration).String(), nil
|
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, formatError(nonIntModuloError, modulo)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
if v.float64 != float64(val2) {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, formatError(nonIntModuloError, modulo)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
if val2 == 0 {
|
2023-02-06 14:13:44 +00:00
|
|
|
return nil, formatError(zeroDivisionError, modulo)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
return float64(val1 % val2), nil
|
2022-12-05 11:15:32 +00:00
|
|
|
default:
|
|
|
|
return nil, errTypeMismatch
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|