1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-05 15:37:19 +00:00
kyverno/pkg/utils/json/utils.go
shuting fb9c66f455
feat(perf): add new linter prealloc to enforce slice declarations best practice (#10250)
* feat(perf): add new linter prealloc to enforce slice declarations best practice

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* fix(linter): prealloac slices

Signed-off-by: ShutingZhao <shuting@nirmata.com>

---------

Signed-off-by: ShutingZhao <shuting@nirmata.com>
2024-05-20 14:46:35 +05:30

37 lines
818 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
}
patchOperations := make([]string, 0, len(patches))
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)
}