forked from wI2L/jsondiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffer.go
357 lines (321 loc) · 8.29 KB
/
differ.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package jsondiff
import (
"sort"
"strings"
)
// A Differ is a JSON Patch generator.
// The zero value is an empty generator ready to use.
type Differ struct {
patch Patch
hasher hasher
hashmap map[uint64]jsonNode
targetBytes []byte
opts options
}
type options struct {
factorize bool
rationalize bool
invertible bool
equivalent bool
}
// Patch returns the list of JSON patch operations
// generated by the Differ. The patch is valid for usage
// until the next comparison or reset.
func (d *Differ) Patch() Patch {
return d.patch
}
// WithOpts applies the given options to the Differ
// and returns it to allow chained calls.
func (d *Differ) WithOpts(opts ...Option) *Differ {
for _, o := range opts {
o(d)
}
return d
}
// Reset resets the Differ to be empty, but it retains the
// underlying storage for use by future comparisons.
func (d *Differ) Reset() {
d.patch = d.patch[:0]
// Optimized map clear.
for k := range d.hashmap {
delete(d.hashmap, k)
}
}
// Compare computes the differences between src and tgt as
// a series of JSON Patch operations.
func (d *Differ) Compare(src, tgt interface{}) {
if d.opts.factorize {
d.prepare(emptyPtr, src, tgt)
}
d.diff(emptyPtr, src, tgt)
}
func (d *Differ) diff(ptr pointer, src, tgt interface{}) {
if src == nil && tgt == nil {
return
}
if !areComparable(src, tgt) {
if ptr.isRoot() {
// If incomparable values are located at the root
// of the document, use an add operation to replace
// the entire content of the document.
// https://tools.ietf.org/html/rfc6902#section-4.1
d.patch = d.patch.append(OperationAdd, emptyPtr, ptr, src, tgt)
} else {
// Values are incomparable, generate a replacement.
d.replace(ptr, src, tgt)
}
return
}
if deepValueEqual(src, tgt, typeSwitchKind(src)) {
return
}
size := len(d.patch)
// Values are comparable, but are not
// equivalent.
switch val := src.(type) {
case []interface{}:
d.compareArrays(ptr, val, tgt.([]interface{}))
case map[string]interface{}:
d.compareObjects(ptr, val, tgt.(map[string]interface{}))
default:
// Generate a replace operation for
// scalar types.
if !deepValueEqual(src, tgt, typeSwitchKind(src)) {
d.replace(ptr, src, tgt)
return
}
}
// Rationalize any new operations.
if d.opts.rationalize && len(d.patch) > size {
d.rationalizeLastOps(ptr, src, tgt, size)
}
}
func (d *Differ) prepare(ptr pointer, src, tgt interface{}) {
if src == nil && tgt == nil {
return
}
// When both values are deeply equals, save
// the location indexed by the value hash.
if !areComparable(src, tgt) {
return
} else if deepValueEqual(src, tgt, typeSwitchKind(src)) {
k := d.hasher.digest(tgt)
if d.hashmap == nil {
d.hashmap = make(map[uint64]jsonNode)
}
d.hashmap[k] = jsonNode{ptr: ptr, val: tgt}
return
}
// At this point, the source and target values
// are non-nil and have comparable types.
switch vsrc := src.(type) {
case []interface{}:
oarr := vsrc
narr := tgt.([]interface{})
for i := 0; i < min(len(oarr), len(narr)); i++ {
d.prepare(ptr.appendIndex(i), oarr[i], narr[i])
}
case map[string]interface{}:
oobj := vsrc
nobj := tgt.(map[string]interface{})
for k, v1 := range oobj {
if v2, ok := nobj[k]; ok {
d.prepare(ptr.appendKey(k), v1, v2)
}
}
default:
// Skipped.
}
}
func (d *Differ) rationalizeLastOps(ptr pointer, src, tgt interface{}, lastOpIdx int) {
newOps := make(Patch, 0, 2)
if d.opts.invertible {
newOps = newOps.append(OperationTest, emptyPtr, ptr, nil, src)
}
// replaceOp represents a single operation that
// replace the source document with the target.
replaceOp := Operation{
Type: OperationReplace,
Path: ptr,
Value: tgt,
}
newOps = append(newOps, replaceOp)
curOps := d.patch[lastOpIdx:]
newLen := replaceOp.jsonLength(d.targetBytes)
curLen := curOps.jsonLength(d.targetBytes)
// If one operation is cheaper than many small
// operations that represents the changes between
// the two objects, replace the last operations.
if curLen > newLen {
d.patch = d.patch[:lastOpIdx]
d.patch = append(d.patch, newOps...)
}
}
// compareObjects generates the patch operations that
// represents the differences between two JSON objects.
func (d *Differ) compareObjects(ptr pointer, src, tgt map[string]interface{}) {
cmpSet := map[string]uint8{}
for k := range src {
cmpSet[k] |= 1 << 0
}
for k := range tgt {
cmpSet[k] |= 1 << 1
}
keys := make([]string, 0, len(cmpSet))
for k := range cmpSet {
keys = append(keys, k)
}
sortStrings(keys)
for _, k := range keys {
v := cmpSet[k]
inOld := v&(1<<0) != 0
inNew := v&(1<<1) != 0
switch {
case inOld && inNew:
d.diff(ptr.appendKey(k), src[k], tgt[k])
case inOld && !inNew:
d.remove(ptr.appendKey(k), src[k])
case !inOld && inNew:
d.add(ptr.appendKey(k), tgt[k])
}
}
}
// compareArrays generates the patch operations that
// represents the differences between two JSON arrays.
func (d *Differ) compareArrays(ptr pointer, src, tgt []interface{}) {
size := min(len(src), len(tgt))
// When the source array contains more elements
// than the target, entries are being removed
// from the destination and the removal index
// is always equal to the original array length.
for i := size; i < len(src); i++ {
d.remove(ptr.appendIndex(size), src[i])
}
if d.opts.equivalent && d.unorderedDeepEqualSlice(src, tgt) {
goto next
}
// Compare the elements at each index present in
// both the source and destination arrays.
for i := 0; i < size; i++ {
d.diff(ptr.appendIndex(i), src[i], tgt[i])
}
next:
// When the target array contains more elements
// than the source, entries are appended to the
// destination.
for i := size; i < len(tgt); i++ {
d.add(ptr.appendKey("-"), tgt[i])
}
}
func (d *Differ) unorderedDeepEqualSlice(src, tgt []interface{}) bool {
if len(src) != len(tgt) {
return false
}
diff := make(map[uint64]int, len(src))
for _, v := range src {
k := d.hasher.digest(v)
diff[k]++
}
for _, v := range tgt {
k := d.hasher.digest(v)
// If the digest hash if not in the Compare,
// return early.
if _, ok := diff[k]; !ok {
return false
}
diff[k] -= 1
if diff[k] == 0 {
delete(diff, k)
}
}
return len(diff) == 0
}
func (d *Differ) add(ptr pointer, v interface{}) {
if !d.opts.factorize {
d.patch = d.patch.append(OperationAdd, emptyPtr, ptr, nil, v)
return
}
idx := d.findRemoved(v)
if idx != -1 {
op := d.patch[idx]
// https://tools.ietf.org/html/rfc6902#section-4.4f
// The "from" location MUST NOT be a proper prefix
// of the "path" location; i.e., a location cannot
// be moved into one of its children.
if !strings.HasPrefix(string(ptr), string(op.Path)) {
d.patch = d.patch.remove(idx)
d.patch = d.patch.append(OperationMove, op.Path, ptr, v, v)
}
return
}
uptr := d.findUnchanged(v)
if !uptr.isRoot() && !d.opts.invertible {
d.patch = d.patch.append(OperationCopy, uptr, ptr, nil, v)
} else {
d.patch = d.patch.append(OperationAdd, emptyPtr, ptr, nil, v)
}
}
func (d *Differ) replace(ptr pointer, src, tgt interface{}) {
if d.opts.invertible {
d.patch = d.patch.append(OperationTest, emptyPtr, ptr, nil, src)
}
d.patch = d.patch.append(OperationReplace, emptyPtr, ptr, src, tgt)
}
func (d *Differ) remove(ptr pointer, v interface{}) {
if d.opts.invertible {
d.patch = d.patch.append(OperationTest, emptyPtr, ptr, nil, v)
}
d.patch = d.patch.append(OperationRemove, emptyPtr, ptr, v, nil)
}
func (d *Differ) findUnchanged(v interface{}) pointer {
if d.hashmap != nil {
k := d.hasher.digest(v)
node, ok := d.hashmap[k]
if ok {
return node.ptr
}
}
return emptyPtr
}
func (d *Differ) findRemoved(v interface{}) int {
for i := 0; i < len(d.patch); i++ {
op := d.patch[i]
if op.Type == OperationRemove && deepEqual(op.OldValue, v) {
return i
}
}
return -1
}
func (d *Differ) applyOpts(opts ...Option) {
for _, opt := range opts {
if opt != nil {
opt(d)
}
}
}
func sortStrings(v []string) {
if len(v) < 20 {
insertionSort(v)
} else {
sort.Strings(v)
}
}
func insertionSort(v []string) {
for j := 1; j < len(v); j++ {
// Invariant: v[:j] contains the same elements as
// the original slice v[:j], but in sorted order.
key := v[j]
i := j - 1
for i >= 0 && v[i] > key {
v[i+1] = v[i]
i--
}
v[i+1] = key
}
}
func min(i, j int) int {
if i < j {
return i
}
return j
}