/
genpatch.go
83 lines (78 loc) · 2.59 KB
/
genpatch.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
package jsonpatch
import (
"encoding/json"
"reflect"
"github.com/VictorLowther/jsonpatch/utils"
)
// This generator does not create copy or move patch ops, and I don't
// care enough to optimize it to do so. Ditto for slice handling.
// There is a lot of optimization that could be done here, but it can get complex real quick.
func basicGen(base, target interface{}, paranoid bool, ptr pointer) patch {
res := make(patch, 0)
if reflect.TypeOf(base) != reflect.TypeOf(target) {
if paranoid {
res = append(res, operation{"test", ptr, nil, utils.Clone(base)})
}
res = append(res, operation{"replace", ptr, nil, utils.Clone(target)})
return res
}
switch baseVal := base.(type) {
case map[string]interface{}:
targetVal := target.(map[string]interface{})
handled := make(map[string]struct{})
// Handle removed and changed first.
for k, oldVal := range baseVal {
newPtr := ptr.Append(k)
newVal, ok := targetVal[k]
if !ok {
// Generate a remove op
if paranoid {
res = append(res, operation{"test", newPtr, nil, utils.Clone(oldVal)})
}
res = append(res, operation{"remove", newPtr, nil, nil})
} else {
subPatch := basicGen(oldVal, newVal, paranoid, newPtr)
res = append(res, subPatch...)
}
handled[k] = struct{}{}
}
// Now, handle additions
for k, newVal := range targetVal {
if _, ok := handled[k]; ok {
continue
}
res = append(res, operation{"add", ptr.Append(k), nil, utils.Clone(newVal)})
}
// case []interface{}:
// Eventually, add code to handle slices more
// efficiently. For now, through, be dumb.
default:
if !reflect.DeepEqual(base, target) {
if paranoid {
res = append(res, operation{"test", ptr, nil, utils.Clone(base)})
}
res = append(res, operation{"replace", ptr, nil, utils.Clone(target)})
}
}
return res
}
// Generate generates a JSON Patch that will modify base into target.
// If paranoid is true, then the generated patch will have test checks.
//
// base and target must be the result of unmarshalling JSON into an interface{}
func Generate(base, target interface{}, paranoid bool) ([]byte, error) {
p := basicGen(base, target, paranoid, make(pointer, 0))
return json.Marshal(p)
}
// GenerateJSON does the same thing as Generate, except base and
// target should be byte arrays containing raw JSON
func GenerateJSON(base, target []byte, paranoid bool) ([]byte, error) {
var rawBase, rawTarget interface{}
if err := json.Unmarshal(base, &rawBase); err != nil {
return nil, err
}
if err := json.Unmarshal(target, &rawTarget); err != nil {
return nil, err
}
return Generate(rawBase, rawTarget, paranoid)
}