1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-05 07:26:55 +00:00
kyverno/pkg/utils/json/utils.go
Jim Bugwadia 58337716c8
Fix merging JSON patches (#4202)
* fix merge of image verify and mutate patches

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* update json patch merge logic

Signed-off-by: Jim Bugwadia <jim@nirmata.com>
2022-07-11 09:26:31 +05:30

37 lines
796 B
Go

package json
import (
"strings"
)
// JoinPatches joins array of serialized JSON patches to the single JSONPatch array
// It accepts patch operations and patches (arrays of patch operations) and returns
// a single combined patch.
func JoinPatches(patches ...[]byte) []byte {
if len(patches) == 0 {
return nil
}
var patchOperations []string
for _, patch := range patches {
str := strings.TrimSpace(string(patch))
if len(str) == 0 {
continue
}
if strings.HasPrefix(str, "[") {
str = strings.TrimPrefix(str, "[")
str = strings.TrimSuffix(str, "]")
str = strings.TrimSpace(str)
}
patchOperations = append(patchOperations, str)
}
if len(patchOperations) == 0 {
return nil
}
result := "[" + strings.Join(patchOperations, ", ") + "]"
return []byte(result)
}