1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/engine/jmespath/utils.go
Andreas Brehmer a1ae86cdbe
Add JMESPath function for dynamic object/array lookup (#7136)
* Fix JMESPath functions error message

JMESPath functions `parse_yaml`, `items` and `object_from_lists` use
wrong format string arguments for an error message and count the
argument from 0 instead of 1.

Fix the format string args and add 1 to the argument index.

Also improve the error message itself.

Signed-off-by: Andreas Brehmer <andreas.brehmer@sap.com>

* Add JMESPath function `lookup`

`lookup` allows for dynamic lookups of objects and arrays, i.e. where
the key/index to look up is determined during the JMESPath query and
thus cannot be injected upfront.

Signed-off-by: Andreas Brehmer <andreas.brehmer@sap.com>

---------

Signed-off-by: Andreas Brehmer <andreas.brehmer@sap.com>
Co-authored-by: shuting <shuting@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-06-19 13:45:13 +00:00

32 lines
1,005 B
Go

package jmespath
import (
"fmt"
"math"
"reflect"
)
func validateArg(f string, arguments []interface{}, index int, expectedType reflect.Kind) (reflect.Value, error) {
if index >= len(arguments) {
return reflect.Value{}, formatError(argOutOfBoundsError, f, index+1, len(arguments))
}
if arguments[index] == nil {
return reflect.Value{}, formatError(invalidArgumentTypeError, f, index+1, expectedType.String())
}
arg := reflect.ValueOf(arguments[index])
if arg.Type().Kind() != expectedType {
return reflect.Value{}, formatError(invalidArgumentTypeError, f, index+1, expectedType.String())
}
return arg, nil
}
func intNumber(number float64) (int, error) {
if math.IsInf(number, 0) || math.IsNaN(number) || math.Trunc(number) != number {
return 0, fmt.Errorf("expected an integer number but got: %g", number)
}
intNumber := int(number)
if float64(intNumber) != number {
return 0, fmt.Errorf("number is outside the range of integer numbers: %g", number)
}
return intNumber, nil
}