forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_operation.go
282 lines (256 loc) · 7.48 KB
/
json_operation.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
package ravendb
import (
"fmt"
"sort"
)
func jsonOperationEntityChanged(newObj map[string]interface{}, documentInfo *documentInfo, changes map[string][]*DocumentsChanges) bool {
var docChanges []*DocumentsChanges
doc := documentInfo.document
if !documentInfo.newDocument && doc != nil {
id := documentInfo.id
return jsonOperationCompareJson(id, doc, newObj, changes, &docChanges)
}
if changes == nil {
return true
}
jsonOperationNewChange("", nil, nil, &docChanges, DocumentChangeDocumentAdded)
id := documentInfo.id
a := changes[id]
a = append(a, docChanges...)
changes[id] = a
return true
}
func isJSONFloatEqual(oldPropVal float64, newProp interface{}) bool {
switch newPropVal := newProp.(type) {
case nil:
return false
case float64:
return oldPropVal == newPropVal
default:
// TODO: can those happen in real life?
panicIf(true, "unhandled type of newProp, expected 'float64' and is '%T'", newProp)
}
return false
}
func isJSONBoolEqual(oldPropVal bool, newProp interface{}) bool {
switch newPropVal := newProp.(type) {
case nil:
return false
case bool:
return oldPropVal == newPropVal
default:
// TODO: can those happen in real life?
panicIf(true, "unhandled type of newProp, expected 'bool' and is '%T'", newProp)
}
return false
}
func isJSONStringEqual(oldPropVal string, newProp interface{}) bool {
switch newPropVal := newProp.(type) {
case nil:
return false
case string:
return oldPropVal == newPropVal
default:
// TODO: can those happen in real life?
panicIf(true, "unhandled type of newProp, expected 'string' and is '%T'", newProp)
}
return false
}
func jsonOperationCompareJson(id string, originalJson map[string]interface{}, newJson map[string]interface{}, changes map[string][]*DocumentsChanges, docChanges *[]*DocumentsChanges) bool {
newJsonProps := getObjectNodeFieldNames(newJson)
oldJsonProps := getObjectNodeFieldNames(originalJson)
newFields := stringArraySubtract(newJsonProps, oldJsonProps)
removedFields := stringArraySubtract(oldJsonProps, newJsonProps)
for _, field := range removedFields {
if changes == nil {
return true
}
jsonOperationNewChange(field, nil, nil, docChanges, DocumentChangeRemovedField)
}
for _, prop := range newJsonProps {
switch prop {
case MetadataLastModified,
MetadataCollection,
MetadataChangeVector,
MetadataID:
continue
}
if stringArrayContains(newFields, prop) {
if changes == nil {
return true
}
v := newJson[prop]
jsonOperationNewChange(prop, v, nil, docChanges, DocumentChangeNewField)
continue
}
newProp := newJson[prop]
oldProp := originalJson[prop]
switch newPropVal := newProp.(type) {
case float64:
if isJSONFloatEqual(newPropVal, oldProp) {
break
}
if changes == nil {
return true
}
jsonOperationNewChange(prop, newProp, oldProp, docChanges, DocumentChangeFieldChanged)
case string:
if isJSONStringEqual(newPropVal, oldProp) {
break
}
if changes == nil {
return true
}
jsonOperationNewChange(prop, newProp, oldProp, docChanges, DocumentChangeFieldChanged)
case bool:
isJSONBoolEqual(newPropVal, oldProp)
case []interface{}:
if oldProp == nil || !isInstanceOfArrayOfInterface(oldProp) {
if changes == nil {
return true
}
jsonOperationNewChange(prop, newProp, oldProp, docChanges, DocumentChangeFieldChanged)
break
}
changed := jsonOperationCompareJsonArray(id, oldProp.([]interface{}), newProp.([]interface{}), changes, docChanges, prop)
if changes == nil && changed {
return true
}
case map[string]interface{}:
oldPropVal, ok := oldProp.(map[string]interface{})
// TODO: a better check for nil?
if !ok || oldProp == nil {
if changes == nil {
return true
}
jsonOperationNewChange(prop, newProp, nil, docChanges, DocumentChangeFieldChanged)
break
}
changed := jsonOperationCompareJson(id, oldPropVal, newPropVal, changes, docChanges)
if changes == nil && changed {
return true
}
default:
if newProp == nil {
if oldProp == nil {
break
}
if changes == nil {
return true
}
jsonOperationNewChange(prop, nil, oldProp, docChanges, DocumentChangeFieldChanged)
break
}
// TODO: array, nil
// Write tests for all types
panicIf(true, "unhandled type %T, newProp: '%v', oldProp: '%v'", newProp, newProp, oldProp)
}
}
if changes == nil || len(*docChanges) == 0 {
return false
}
changes[id] = *docChanges
return true
}
func isInstanceOfArrayOfInterface(v interface{}) bool {
_, ok := v.([]interface{})
return ok
}
func jsonOperationCompareJsonArray(id string, oldArray []interface{}, newArray []interface{}, changes map[string][]*DocumentsChanges, docChanges *[]*DocumentsChanges, propName string) bool {
// if we don't care about the changes
if len(oldArray) != len(newArray) && changes == nil {
return true
}
changed := false
position := 0
maxPos := len(oldArray)
if maxPos > len(newArray) {
maxPos = len(newArray)
}
for ; position < maxPos; position++ {
oldVal := oldArray[position]
newVal := newArray[position]
switch oldVal.(type) {
case map[string]interface{}:
if _, ok := newVal.(map[string]interface{}); ok {
newChanged := jsonOperationCompareJson(id, oldVal.(map[string]interface{}), newVal.(map[string]interface{}), changes, docChanges)
if newChanged {
changed = newChanged
}
} else {
changed = true
if changes != nil {
jsonOperationNewChange(propName, newVal, oldVal, docChanges, DocumentChangeArrayValueAdded)
}
}
case []interface{}:
if _, ok := newVal.([]interface{}); ok {
newChanged := jsonOperationCompareJsonArray(id, oldVal.([]interface{}), newVal.([]interface{}), changes, docChanges, propName)
if newChanged {
changed = newChanged
}
} else {
changed = true
if changes != nil {
jsonOperationNewChange(propName, newVal, oldVal, docChanges, DocumentChangeArrayValueChanged)
}
}
default:
// NULL case
if oldVal == nil {
if newVal != nil {
changed = true
if changes != nil {
jsonOperationNewChange(propName, newVal, oldVal, docChanges, DocumentChangeArrayValueChanged)
}
}
break
}
// Note: this matches Java but also means that 1 == "1"
oldValStr := fmt.Sprintf("%v", oldVal)
newValStr := fmt.Sprintf("%v", newVal)
if oldValStr != newValStr {
if changes != nil {
jsonOperationNewChange(propName, newVal, oldVal, docChanges, DocumentChangeArrayValueChanged)
}
changed = true
}
}
}
if changes == nil {
return changed
}
// if one of the arrays is larger than the other
for ; position < len(oldArray); position++ {
jsonOperationNewChange(propName, nil, oldArray[position], docChanges, DocumentChangeArrayValueRemoved)
}
for ; position < len(newArray); position++ {
jsonOperationNewChange(propName, newArray[position], nil, docChanges, DocumentChangeArrayValueAdded)
}
return changed
}
func jsonOperationNewChange(name string, newValue interface{}, oldValue interface{}, docChanges *[]*DocumentsChanges, change ChangeType) {
documentsChanges := &DocumentsChanges{
FieldNewValue: newValue,
FieldOldValue: oldValue,
FieldName: name,
Change: change,
}
*docChanges = append(*docChanges, documentsChanges)
}
func getObjectNodeFieldNames(o map[string]interface{}) []string {
n := len(o)
if n == 0 {
return nil
}
res := make([]string, n)
i := 0
for k := range o {
res[i] = k
i++
}
// Go randomizes order of map traversal but it's useful to have it
// fixed e.g. for tests
sort.Strings(res)
return res
}