mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-30 19:35:06 +00:00
Added JMESPath filter to_boolean() (#6292)
* added to_boolean in JMESPath Filter Signed-off-by: Abhishek Sawan <sawanabhi157@gmail.com> * updated to_boolean JMESPath filter Signed-off-by: Abhishek Sawan <sawanabhi157@gmail.com> * updated to_boolean JMESPath filter with function registration Signed-off-by: Abhishek Sawan <sawanabhi157@gmail.com> * fixed to_boolean JMESPath filter with function registration Signed-off-by: Abhishek Sawan <sawanabhi157@gmail.com> * Update pkg/engine/jmespath/functions.go Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * fix Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> --------- Signed-off-by: Abhishek Sawan <sawanabhi157@gmail.com> Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com> Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
c275740279
commit
8be4460668
3 changed files with 58 additions and 0 deletions
|
@ -49,6 +49,7 @@ var (
|
|||
regexMatch = "regex_match"
|
||||
patternMatch = "pattern_match"
|
||||
labelMatch = "label_match"
|
||||
toBoolean = "to_boolean"
|
||||
add = "add"
|
||||
subtract = "subtract"
|
||||
multiply = "multiply"
|
||||
|
@ -226,6 +227,16 @@ func GetFunctions() []FunctionEntry {
|
|||
},
|
||||
ReturnType: []jpType{jpBool},
|
||||
Note: "object arguments must be enclosed in backticks; ex. `{{request.object.spec.template.metadata.labels}}`",
|
||||
}, {
|
||||
FunctionEntry: gojmespath.FunctionEntry{
|
||||
Name: toBoolean,
|
||||
Arguments: []argSpec{
|
||||
{Types: []jpType{jpString}},
|
||||
},
|
||||
Handler: jpToBoolean,
|
||||
},
|
||||
ReturnType: []jpType{jpBool},
|
||||
Note: "It returns true or false for any string, such as 'True', 'TruE', 'False', 'FAlse', 'faLSE', etc.",
|
||||
}, {
|
||||
FunctionEntry: gojmespath.FunctionEntry{
|
||||
Name: add,
|
||||
|
@ -736,6 +747,21 @@ func jpLabelMatch(arguments []interface{}) (interface{}, error) {
|
|||
return true, nil
|
||||
}
|
||||
|
||||
func jpToBoolean(arguments []interface{}) (interface{}, error) {
|
||||
if input, err := validateArg(toBoolean, arguments, 0, reflect.String); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
switch strings.ToLower(input.String()) {
|
||||
case "true":
|
||||
return true, nil
|
||||
case "false":
|
||||
return false, nil
|
||||
default:
|
||||
return nil, formatError(genericError, toBoolean, fmt.Sprintf("lowercase argument must be 'true' or 'false' (provided: '%s')", input.String()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func jpAdd(arguments []interface{}) (interface{}, error) {
|
||||
op1, op2, err := ParseArithemticOperands(arguments, add)
|
||||
if err != nil {
|
||||
|
|
|
@ -597,6 +597,35 @@ func Test_LabelMatch(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func Test_JpToBoolean(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input interface{}
|
||||
expected interface{}
|
||||
err bool
|
||||
}{
|
||||
{"true", true, false},
|
||||
{"TRue", true, false},
|
||||
{"FaLse", false, false},
|
||||
{"FaLsee", nil, true},
|
||||
{"false", false, false},
|
||||
{"foo", nil, true},
|
||||
{1, nil, true},
|
||||
{nil, nil, true},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
res, err := jpToBoolean([]interface{}{tc.input})
|
||||
if tc.err && err == nil {
|
||||
t.Errorf("Expected an error but received nil")
|
||||
}
|
||||
if !tc.err && err != nil {
|
||||
t.Errorf("Expected nil error but received: %s", err)
|
||||
}
|
||||
if res != tc.expected {
|
||||
t.Errorf("Expected %v but received %v", tc.expected, res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Base64Decode(t *testing.T) {
|
||||
jp, err := New("base64_decode('SGVsbG8sIHdvcmxkIQ==')")
|
||||
assert.NilError(t, err)
|
||||
|
|
|
@ -8,6 +8,9 @@ func validateArg(f string, arguments []interface{}, index int, expectedType refl
|
|||
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())
|
||||
|
|
Loading…
Add table
Reference in a new issue