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

38 lines
796 B
Go
Raw Normal View History

package json
2019-07-05 11:24:18 -07:00
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)
}