1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

jmespath truncate - handle negative input value (#2856)

Signed-off-by: Danny Kulchinsky <dkulchinsky@fastly.com>
This commit is contained in:
Danny Kulchinsky 2021-12-20 01:50:46 -05:00 committed by GitHub
parent 2076f07b9f
commit ff99d92f80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View file

@ -612,6 +612,7 @@ func jpPathCanonicalize(arguments []interface{}) (interface{}, error) {
func jpTruncate(arguments []interface{}) (interface{}, error) {
var err error
var normalizedLength float64
str, err := validateArg(truncate, arguments, 0, reflect.String)
if err != nil {
return nil, err
@ -621,7 +622,13 @@ func jpTruncate(arguments []interface{}) (interface{}, error) {
return nil, err
}
return trunc.Truncator(str.String(), int(length.Float()), trunc.CutStrategy{}), nil
if length.Float() < 0 {
normalizedLength = float64(0)
} else {
normalizedLength = length.Float()
}
return trunc.Truncator(str.String(), int(normalizedLength), trunc.CutStrategy{}), nil
}
// InterfaceToString casts an interface to a string type

View file

@ -995,6 +995,18 @@ func Test_Truncate(t *testing.T) {
jmesPath: "truncate('Lorem ipsum ipsum ipsum dolor sit amet', `40`)",
expectedResult: "Lorem ipsum ipsum ipsum dolor sit amet",
},
{
jmesPath: "truncate('Lorem ipsum', `2.6`)",
expectedResult: "Lo",
},
{
jmesPath: "truncate('Lorem ipsum', `0`)",
expectedResult: "",
},
{
jmesPath: "truncate('Lorem ipsum', `-1`)",
expectedResult: "",
},
}
for _, tc := range testCases {