mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-05 15:37:19 +00:00
* 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>
37 lines
818 B
Go
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)
|
|
}
|