-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathobjects.go
39 lines (37 loc) · 992 Bytes
/
objects.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
package patchwerk
import (
"reflect"
)
// diff returns the (recursive) difference between a and b as an array of JsonPatchOperations.
func diffObjects(a, b map[string]interface{}, path string) ([]*JSONPatchOperation, error) {
patch := []*JSONPatchOperation{}
for key, bv := range b {
p := makePath(path, key)
av, ok := a[key]
// Key doesn't exist in original document, value was added
if !ok {
patch = append(patch, NewPatch("add", p, bv))
continue
}
// If types have changed, replace completely
if reflect.TypeOf(av) != reflect.TypeOf(bv) {
patch = append(patch, NewPatch("replace", p, bv))
continue
}
// Types are the same, compare values
tempPatch, err := diff(av, bv, p)
if err != nil {
return nil, err
}
patch = append(patch, tempPatch...)
}
// Now add all deleted values as nil
for key := range a {
_, ok := b[key]
if !ok {
p := makePath(path, key)
patch = append(patch, NewPatch("remove", p, nil))
}
}
return patch, nil
}