2021-12-07 15:44:46 +00:00
|
|
|
package jmespath
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/inf.v0"
|
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
|
|
)
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
type operand interface {
|
2023-03-23 12:11:56 +00:00
|
|
|
Add(interface{}, string) (interface{}, error)
|
2021-12-07 15:44:46 +00:00
|
|
|
Subtract(interface{}) (interface{}, error)
|
|
|
|
Multiply(interface{}) (interface{}, error)
|
|
|
|
Divide(interface{}) (interface{}, error)
|
|
|
|
Modulo(interface{}) (interface{}, error)
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
type quantity struct {
|
2021-12-07 15:44:46 +00:00
|
|
|
resource.Quantity
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
type duration struct {
|
2021-12-07 15:44:46 +00:00
|
|
|
time.Duration
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
type scalar struct {
|
2021-12-07 15:44:46 +00:00
|
|
|
float64
|
|
|
|
}
|
|
|
|
|
2023-07-03 19:31:54 +00:00
|
|
|
func parseArithemticOperand(arguments []interface{}, index int, operator string) (operand, error) {
|
|
|
|
if tmp, err := validateArg(operator, arguments, index, reflect.Float64); err == nil {
|
|
|
|
return scalar{float64: tmp.Float()}, nil
|
|
|
|
} else if tmp, err = validateArg(operator, arguments, index, reflect.String); err == nil {
|
|
|
|
if q, err := resource.ParseQuantity(tmp.String()); err == nil {
|
|
|
|
return quantity{Quantity: q}, nil
|
|
|
|
} else if d, err := time.ParseDuration(tmp.String()); err == nil {
|
|
|
|
return duration{Duration: d}, nil
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-03 19:31:54 +00:00
|
|
|
return nil, formatError(genericError, operator, "invalid operand")
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseArithemticOperands(arguments []interface{}, operator string) (operand, operand, error) {
|
|
|
|
left, err := parseArithemticOperand(arguments, 0, operator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
right, err := parseArithemticOperand(arguments, 1, operator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
2023-07-03 19:31:54 +00:00
|
|
|
return left, right, nil
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 quantity) Add(op2 interface{}, operator string) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case quantity:
|
2021-12-07 15:44:46 +00:00
|
|
|
op1.Quantity.Add(v.Quantity)
|
|
|
|
return op1.String(), nil
|
2022-12-05 11:15:32 +00:00
|
|
|
default:
|
2023-03-23 12:11:56 +00:00
|
|
|
return nil, formatError(typeMismatchError, operator)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 duration) Add(op2 interface{}, operator string) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case duration:
|
2022-12-05 11:15:32 +00:00
|
|
|
return (op1.Duration + v.Duration).String(), nil
|
|
|
|
default:
|
2023-03-23 12:11:56 +00:00
|
|
|
return nil, formatError(typeMismatchError, operator)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 scalar) Add(op2 interface{}, operator string) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case scalar:
|
2021-12-07 15:44:46 +00:00
|
|
|
return op1.float64 + v.float64, nil
|
2022-12-05 11:15:32 +00:00
|
|
|
default:
|
2023-03-23 12:11:56 +00:00
|
|
|
return nil, formatError(typeMismatchError, operator)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 quantity) Subtract(op2 interface{}) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case quantity:
|
2021-12-07 15:44:46 +00:00
|
|
|
op1.Quantity.Sub(v.Quantity)
|
|
|
|
return op1.String(), nil
|
2022-12-05 11:15:32 +00:00
|
|
|
default:
|
2023-02-06 15:01:39 +00:00
|
|
|
return nil, formatError(typeMismatchError, subtract)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 duration) Subtract(op2 interface{}) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case duration:
|
2022-12-05 11:15:32 +00:00
|
|
|
return (op1.Duration - v.Duration).String(), nil
|
|
|
|
default:
|
2023-02-06 15:01:39 +00:00
|
|
|
return nil, formatError(typeMismatchError, subtract)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 scalar) Subtract(op2 interface{}) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case scalar:
|
2021-12-07 15:44:46 +00:00
|
|
|
return op1.float64 - v.float64, nil
|
2022-12-05 11:15:32 +00:00
|
|
|
default:
|
2023-02-06 15:01:39 +00:00
|
|
|
return nil, formatError(typeMismatchError, subtract)
|
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
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 quantity) Multiply(op2 interface{}) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case scalar:
|
2021-12-07 15:44:46 +00:00
|
|
|
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:
|
2023-02-06 15:01:39 +00:00
|
|
|
return nil, formatError(typeMismatchError, multiply)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 duration) Multiply(op2 interface{}) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
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:
|
2023-02-06 15:01:39 +00:00
|
|
|
return nil, formatError(typeMismatchError, multiply)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 scalar) Multiply(op2 interface{}) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case scalar:
|
2021-12-07 15:44:46 +00:00
|
|
|
return op1.float64 * v.float64, nil
|
2023-04-13 11:29:40 +00:00
|
|
|
case quantity:
|
2021-12-07 15:44:46 +00:00
|
|
|
return v.Multiply(op1)
|
2023-04-13 11:29:40 +00:00
|
|
|
case duration:
|
2021-12-07 15:44:46 +00:00
|
|
|
return v.Multiply(op1)
|
2023-02-06 15:01:39 +00:00
|
|
|
default:
|
|
|
|
return nil, formatError(typeMismatchError, multiply)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 quantity) Divide(op2 interface{}) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
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
|
2023-04-13 11:29:40 +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:
|
2023-02-06 15:01:39 +00:00
|
|
|
return nil, formatError(typeMismatchError, divide)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 duration) Divide(op2 interface{}) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case duration:
|
2021-12-07 15:44:46 +00:00
|
|
|
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
|
2023-04-13 11:29:40 +00:00
|
|
|
case scalar:
|
2021-12-07 15:44:46 +00:00
|
|
|
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:
|
2023-02-06 15:01:39 +00:00
|
|
|
return nil, formatError(typeMismatchError, divide)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 scalar) Divide(op2 interface{}) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case scalar:
|
2021-12-07 15:44:46 +00:00
|
|
|
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:
|
2023-02-06 15:01:39 +00:00
|
|
|
return nil, formatError(typeMismatchError, divide)
|
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
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 quantity) Modulo(op2 interface{}) (interface{}, error) {
|
2022-12-05 11:15:32 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case quantity:
|
2022-12-05 11:15:32 +00:00
|
|
|
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:
|
2023-02-06 15:01:39 +00:00
|
|
|
return nil, formatError(typeMismatchError, modulo)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 duration) Modulo(op2 interface{}) (interface{}, error) {
|
2022-12-05 11:15:32 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case duration:
|
2022-12-05 11:15:32 +00:00
|
|
|
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:
|
2023-02-06 15:01:39 +00:00
|
|
|
return nil, formatError(typeMismatchError, modulo)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
func (op1 scalar) Modulo(op2 interface{}) (interface{}, error) {
|
2021-12-07 15:44:46 +00:00
|
|
|
switch v := op2.(type) {
|
2023-04-13 11:29:40 +00:00
|
|
|
case scalar:
|
2021-12-07 15:44:46 +00:00
|
|
|
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:
|
2023-02-06 15:01:39 +00:00
|
|
|
return nil, formatError(typeMismatchError, modulo)
|
2021-12-07 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|