1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/utils/json/patch.go
Charles-Edouard Brétéché e233f56b73
chore: use github.com/evanphx/json-patch/v5 (#7015)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-04-27 11:14:20 +00:00

47 lines
1 KiB
Go

package json
import (
"encoding/json"
jsonpatch "github.com/evanphx/json-patch/v5"
)
type PatchOperation struct {
Path string `json:"path"`
Op string `json:"op"`
Value interface{} `json:"value,omitempty"`
}
func NewPatchOperation(path, op string, value interface{}) PatchOperation {
return PatchOperation{path, op, value}
}
func (p *PatchOperation) Marshal() ([]byte, error) {
return json.Marshal(p)
}
func (p *PatchOperation) ToPatchBytes() ([]byte, error) {
if patch, err := json.Marshal(p); err != nil {
return nil, err
} else {
return JoinPatches(patch), nil
}
}
func MarshalPatchOperation(path, op string, value interface{}) ([]byte, error) {
p := NewPatchOperation(path, op, value)
return p.Marshal()
}
func CheckPatch(patch []byte) error {
_, err := jsonpatch.DecodePatch([]byte("[" + string(patch) + "]"))
return err
}
func UnmarshalPatchOperation(patch []byte) (*PatchOperation, error) {
var p PatchOperation
if err := json.Unmarshal(patch, &p); err != nil {
return nil, err
}
return &p, nil
}