-
Notifications
You must be signed in to change notification settings - Fork 34
/
patch.go
84 lines (78 loc) · 2.2 KB
/
patch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package core
import (
"encoding/json"
"github.com/mattbaird/jsonpatch"
"github.com/viant/toolbox"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
//ResourcePatch represents resource patch
type ResourcePatch struct {
metav1.TypeMeta `json:",inline"`
Name string
Pt types.PatchType
Data []byte
HasChanged bool
}
var patchableKeys = []string{"spec", "metadata"}
//NewResourcePatch returns a new resource patch
func NewResourcePatch(meta *ResourceMeta, original, target interface{}) (*ResourcePatch, error) {
patch := &ResourcePatch{
TypeMeta: meta.TypeMeta,
Name: meta.Metadata.Name,
}
var err error
if original, err = filterFields(original, patchableKeys); err != nil {
return nil, err
}
if target, err = filterFields(target, patchableKeys); err != nil {
return nil, err
}
originalSON, err := json.Marshal(original)
if err != nil {
return nil, err
}
targetJSON, err := json.Marshal(target)
if err != nil {
return nil, err
}
patchOperations, err := jsonpatch.CreatePatch(originalSON, targetJSON)
if err != nil {
return nil, err
}
patchOperations = deleteRemoval(patchOperations)
patch.HasChanged = len(patchOperations) > 0
if patch.Data, err = json.Marshal(patchOperations); err != nil {
return nil, err
}
patch.Pt = types.JSONPatchType
return patch, nil
}
func filterFields(resource interface{}, keys []string) (interface{}, error) {
var original = make(map[string]interface{})
err := converter.AssignConverted(&original, resource)
if err != nil {
return nil, err
}
original = toolbox.DeleteEmptyKeys(original)
var targetMap = make(map[string]interface{})
for _, key := range keys {
targetMap[key] = original[key]
}
return targetMap, err
}
func deleteRemoval(operations []jsonpatch.JsonPatchOperation) []jsonpatch.JsonPatchOperation {
var result = make([]jsonpatch.JsonPatchOperation, 0)
for _, operation := range operations {
if operation.Operation == "remove" {
continue
}
if operation.Operation == "replace" {
if operation.Value == nil || (toolbox.IsNumber(operation.Value) && toolbox.AsInt(operation.Value) == 0) {
continue
}
}
result = append(result, operation)
}
return result
}