2023-02-06 07:27:27 +01:00
|
|
|
package jmespath
|
|
|
|
|
|
|
|
import (
|
2023-06-19 15:45:13 +02:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2023-02-06 07:27:27 +01:00
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
func validateArg(f string, arguments []interface{}, index int, expectedType reflect.Kind) (reflect.Value, error) {
|
2023-02-06 12:39:53 +01:00
|
|
|
if index >= len(arguments) {
|
2023-02-06 15:13:44 +01:00
|
|
|
return reflect.Value{}, formatError(argOutOfBoundsError, f, index+1, len(arguments))
|
2023-02-06 12:39:53 +01:00
|
|
|
}
|
2023-03-16 18:22:26 +05:30
|
|
|
if arguments[index] == nil {
|
|
|
|
return reflect.Value{}, formatError(invalidArgumentTypeError, f, index+1, expectedType.String())
|
|
|
|
}
|
2023-02-06 07:27:27 +01:00
|
|
|
arg := reflect.ValueOf(arguments[index])
|
|
|
|
if arg.Type().Kind() != expectedType {
|
2023-02-06 15:13:44 +01:00
|
|
|
return reflect.Value{}, formatError(invalidArgumentTypeError, f, index+1, expectedType.String())
|
2023-02-06 07:27:27 +01:00
|
|
|
}
|
|
|
|
return arg, nil
|
|
|
|
}
|
2023-06-19 15:45:13 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|