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:
parent
2076f07b9f
commit
ff99d92f80
2 changed files with 20 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue