forked from samsarahq/thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
325 lines (295 loc) · 8.45 KB
/
diff.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
// Package diff computes diffs for updating JSON objects. It uses a lightweight
// field-by-field diffing algorithm suitable for comparing graphql result
// objects, but perhaps not for other JSON objects.
//
// For each field, a diff represents an update as
// - a recursive diff, stored as an object.
// - a complex replacement of the original field's value, stored as the new value in a 1-element array.
// - a scalar replacement, stored as the raw new value.
// - a deletion of the old field, stored as a 0-element array.
//
// For example, consider the following objects old, new, and the resulting
// diff:
//
// old = {
// "name": "bob",
// "address": {"state": "ca", "city": "sf"},
// "age": 30
// }
// new = {
// "name": "alice",
// "address": {"state": "ca", "city": "oakland"},
// "friends": ["bob", "charlie]
// }
// diff = {
// "name": "alice",
// "address": {"city": "oakland"},
// "age": [],
// "friends": [["bob", "charlie]]
// }
//
// The diff updates the name field to "alice", stored as a scalar. The diff
// updates the address recursively, keeping the state, but changing the city
// to "oakland". The diff deletes the age field with a 0-element array, and
// add a new complex friends field, stored in 1-element array.
//
// Array diffs are represented as object diffs with an optional reordering
// field stored in the "$" property. This reordering field holds a compressed
// of indices, indicating for each value in the new array the location of the
// element in the previous array. This makes for small diffs of long arrays
// that contain some permuted objects.
//
// For example, consider the following arrays old, new, and the resulting
// reordering:
//
// old = [0, 1, 2, 3]
// new = [1, 2, 3, 4]
// reordering = [1, 2, 3, -1]
//
// The reordering indicates that the first 3 elements of the new array can
// be found at positions 1, 2, 3 and of the old array. The fourth item in
// the new array was not found in the old array.
//
// To compress this reodering array, we replace runs of adjacent indices
// as a tuple [start, length], so that the compressed reordering becomes
//
// reordering = [1, 2, 3, -1]
// compressed = [[1, 3], -1]
//
// Finally, to identify complex objects in arrays, package diffs uses
// a special "__key" field in objects. This must be a comparable value
// that allows the algorithm to line up values in arrays. For example,
//
// old = [
// {"__key": 10, "name": "bob", "age": 20"},
// {"__key": 13, "name": "alice"}
// ]
// new = [
// {"__key": 13, "name": "alice"}
// {"__key": 10, "name": "bob", "age": 23},
// ]
// diff = {
// "$": [1, 0],
// "1": {"age": 23}
// }
//
// Here, the diff first switches the order of the elements in the array,
// using the __key field to identify the two objects, and then updates
// the "age" field in the second element of the array to 23.
package diff
import (
"bytes"
"fmt"
"reflect"
)
var emptyArray = []interface{}{}
// markRemoved returns a 0-element JSON array to indicate a removed field.
func markRemoved() interface{} {
return emptyArray
}
// StripKey recursively creates a new JSON object with all __key fields
// removed.
func StripKey(i interface{}) interface{} {
switch i := i.(type) {
case map[string]interface{}:
r := make(map[string]interface{})
for k, v := range i {
if k == "__key" {
continue
}
r[k] = StripKey(v)
}
return r
case []interface{}:
r := make([]interface{}, 0, len(i))
for _, v := range i {
r = append(r, StripKey(v))
}
return r
default:
return i
}
}
// markReplaced returns either a scalar value or a 1-element array
// wrapping a complex value to indicate an updated field.
//
// markReplaced strips __key fields from any complex value.
func markReplaced(i interface{}) interface{} {
switch i := i.(type) {
case bool, int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64, string:
// Pass through i that don't look like deltas.
return i
default:
// Wrap all other values.
return []interface{}{StripKey(i)}
}
}
// diffMap computes a diff between two maps by comparing fields key-by-key.
func diffMap(old map[string]interface{}, newAny interface{}) interface{} {
// Verify the type of new.
new, ok := newAny.(map[string]interface{})
if !ok {
return markReplaced(new)
}
// Check if two map are identical by comparing their pointers, and
// short-circuit if so.
if reflect.ValueOf(old).Pointer() == reflect.ValueOf(new).Pointer() {
return nil
}
// Assert that the __key fields, if present, are equal.
if old["__key"] != new["__key"] {
return markReplaced(new)
}
// Build the diff.
d := make(map[string]interface{})
// Handle deleted fields.
for k := range old {
if _, ok := new[k]; !ok {
d[k] = markRemoved()
}
}
// Handle changed fields.
for k, newV := range new {
if oldV, ok := old[k]; ok {
if innerD := Diff(oldV, newV); innerD != nil {
d[k] = innerD
}
} else {
d[k] = newV
}
}
// Check if the diff is empty.
if len(d) == 0 {
return nil
}
return d
}
// reoderKey returns the key to use for a
func reorderKey(i interface{}) interface{} {
if object, ok := i.(map[string]interface{}); ok {
if key, ok := object["__key"]; ok {
return key
}
}
if reflect.TypeOf(i).Comparable() {
return i
}
return nil
}
// computeReorderIndices returns an array containing the index in old of each
// item in new
//
// If an item in new is not present in old, the index is -1. Objects are
// identified using the __key field, if present. Otherwise, the values are used
// as map keys if they are comparable.
func computeReorderIndices(old, new []interface{}) []int {
oldIndices := make(map[interface{}][]int)
for i, item := range old {
key := reorderKey(item)
oldIndices[key] = append(oldIndices[key], i)
}
indices := make([]int, len(new))
for i, item := range new {
key := reorderKey(item)
if index := oldIndices[key]; len(index) > 0 {
indices[i] = index[0]
oldIndices[key] = index[1:]
} else {
indices[i] = -1
}
}
return indices
}
// compressReorderIndices compresses a set of indices
//
// Runs of incrementing non-negative consecutive values are represented by a
// two-element array [first, count].
func compressReorderIndices(indices []int) []interface{} {
compressed := make([]interface{}, 0)
i := 0
for i < len(indices) {
// j represents the end of the current run.
j := i
for j < len(indices) && indices[j] != -1 && indices[j]-indices[i] == j-i {
// Increment j while the run continues.
j++
}
if i == j {
compressed = append(compressed, -1)
i = j + 1
} else if j-i == 1 {
compressed = append(compressed, indices[i])
i = j
} else {
compressed = append(compressed, [2]int{indices[i], j - i})
i = j
}
}
return compressed
}
// diffArray computes a diff between two arrays by first reordering the
// elements and then comparing elements one-by-one.
func diffArray(old []interface{}, newAny interface{}) interface{} {
// Verify the type of new.
new, ok := newAny.([]interface{})
if !ok {
return markReplaced(new)
}
// Check if two arrays are identical by comparing their pointers and length,
// and short-circuit if so.
if len(old) == len(new) && reflect.ValueOf(old).Pointer() == reflect.ValueOf(new).Pointer() {
return nil
}
d := make(map[string]interface{})
// Compute reorder indices.
indices := computeReorderIndices(old, new)
// Check if the reorder indices can be omitted.
orderChanged := len(old) != len(indices)
for i := range indices {
if indices[i] != i {
orderChanged = true
}
}
if orderChanged {
d["$"] = compressReorderIndices(indices)
}
// Compare the array elements.
for i, newI := range new {
var oldI interface{}
if j := indices[i]; j != -1 {
oldI = old[j]
}
if innerD := Diff(oldI, newI); innerD != nil {
d[fmt.Sprint(i)] = innerD
}
}
// Check if the diff is empty.
if len(d) == 0 {
return nil
}
return d
}
// Diff computes a diff between two JSON objects. See the package comment for
// details of the algorithm and the diff format.
//
// A nil diff indicates that the old and new objects are equal.
func Diff(old interface{}, new interface{}) interface{} {
switch old := old.(type) {
case map[string]interface{}:
return diffMap(old, new)
case []interface{}:
return diffArray(old, new)
case []uint8:
if new, ok := new.([]uint8); ok && bytes.Equal(old, new) {
return nil
}
return markReplaced(new)
default:
if old != new {
return markReplaced(new)
}
return nil
}
}