forked from mattbaird/jsonpatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays.go
94 lines (86 loc) · 2.11 KB
/
arrays.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
85
86
87
88
89
90
91
92
93
94
package jsonpatch
import (
"fmt"
"reflect"
)
type tmpEl struct {
val interface{}
isFixed bool
}
func diffArrays(a, b []interface{}, p string, forceFullPatch bool) ([]JSONPatchOperation, error) {
fullReplace := []JSONPatchOperation{NewPatch("replace", p, b)}
patch := []JSONPatchOperation{}
tmp := make([]tmpEl, len(a))
for i, ae := range a {
newEl := tmpEl{val: ae}
for j := i; j < len(b); j++ {
if len(b) <= j { //b is out of bounds
break
}
be := b[j]
if reflect.DeepEqual(ae, be) {
newEl.isFixed = true // this element should remain in place
}
}
tmp[i] = newEl
}
// Now we have an array of elements in which we know the original, unmoved elements
fmt.Println("a>>>", a)
fmt.Println("TMP>>>", tmp)
aIndex := 0
bIndex := 0
addedDelta := 0
maxLen := len(a)
if len(b) > maxLen {
maxLen = len(b)
}
for aIndex+addedDelta < maxLen {
tmpIndex := aIndex + addedDelta
newPath := makePath(p, tmpIndex)
if aIndex >= len(a) && bIndex >= len(b) {
break
}
if aIndex >= len(a) { // a is out of bounds, all new items in b must be adds
patch = append(patch, NewPatch("add", newPath, b[tmpIndex]))
addedDelta++
continue
}
if bIndex >= len(b) { // b is out of bounds, all new items in a must be removed
patch = append(patch, NewPatch("remove", newPath, nil))
addedDelta--
aIndex++
continue
}
// can compare elements, so let's compare them
te := tmp[aIndex]
for j := bIndex; j < maxLen; j++ {
be := b[j]
fmt.Printf("Comparing i=%d j=%d ae=%v be=%v\n", aIndex, j, te.val, be)
if reflect.DeepEqual(te.val, be) {
// element is already in b, move on
bIndex++
aIndex++
break
} else {
if te.isFixed {
fmt.Println("add", newPath, be)
patch = append(patch, NewPatch("add", newPath, be))
addedDelta++
bIndex++
break
} else {
fmt.Println("remove", newPath, be)
patch = append(patch, NewPatch("remove", newPath, nil))
addedDelta--
aIndex++
break
}
}
}
}
fmt.Println("patch>>>", patch)
if forceFullPatch {
return patch, nil
}
return getSmallestPatch(fullReplace, patch), nil
}