-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity_json.go
491 lines (462 loc) · 10.7 KB
/
entity_json.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package gorb
import (
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
"strings"
"time"
)
func sameValue(value1, value2 reflect.Value) bool {
var ok bool
switch value1.Kind() {
case reflect.Bool:
return value1.Bool() == value2.Bool()
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
return value1.Int() == value2.Int()
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return value1.Uint() == value2.Uint()
case reflect.Float32, reflect.Float64:
return math.Abs(value1.Float()-value2.Float()) < 0.0000001
case reflect.String:
return value1.String() == value2.String()
case reflect.Slice:
if value1.Len() == value2.Len() {
var b1, b2 []byte
b1, ok = value1.Interface().([]byte)
if ok {
b2, ok = value2.Interface().([]byte)
if ok {
for i := 0; i < len(b1); i++ {
if b1[i] != b2[i] {
return false
}
}
return true
}
}
}
default:
var d1, d2 time.Time
d1, ok = value1.Interface().(time.Time)
if ok {
d2, ok = value2.Interface().(time.Time)
if ok {
return d1.Unix() == d2.Unix()
}
}
}
return false
}
func (t *Table) marshalToJson(newRow reflect.Value, oldRow *reflect.Value) map[string]interface{} {
res := make(map[string]interface{}, len(t.Fields))
var allSkipped bool = oldRow != nil
for _, f := range t.Fields {
skip := false
vF := newRow.FieldByIndex(f.ClassIdx)
if oldRow != nil {
vFF := (*oldRow).FieldByIndex(f.ClassIdx)
if vF.Kind() == reflect.Ptr {
if vF.IsNil() || vFF.IsNil() {
skip = vF.IsNil() && vFF.IsNil()
} else {
skip = sameValue(vF.Elem(), vFF.Elem())
}
} else {
skip = sameValue(vF, vFF)
}
allSkipped = allSkipped && skip
}
if !f.IsRequired && skip {
continue
}
var vSet interface{} = nil
if vF.Kind() == reflect.Ptr {
if !vF.IsNil() {
vSet = vF.Elem().Interface()
}
} else {
vSet = vF.Interface()
}
if vSet != nil {
if f.DataType == DateTime {
t, ok := vSet.(time.Time)
if ok {
vSet = timeToString(t)
}
}
}
res[f.FieldName] = vSet
}
for _, ch := range t.Children {
var chJson map[string]interface{}
chV := newRow.FieldByIndex(ch.ClassIdx)
switch ch.ChildClass.Kind() {
case reflect.Ptr:
if chV.IsNil() {
if oldRow != nil {
if !(*oldRow).IsNil() {
allSkipped = false
res[ch.TableName] = nil
}
} else {
allSkipped = false
res[ch.TableName] = nil
}
} else {
if oldRow != nil {
chOV := (*oldRow).FieldByIndex(ch.ClassIdx)
if chOV.IsNil() {
chJson = ch.marshalToJson(chV.Elem(), nil)
} else {
var chOVP reflect.Value = chOV.Elem()
chJson = ch.marshalToJson(chV.Elem(), &chOVP)
}
} else {
chJson = ch.marshalToJson(chV.Elem(), nil)
}
if chJson != nil {
allSkipped = false
res[ch.TableName] = chJson
}
}
case reflect.Slice:
var chOV reflect.Value
jsonChArray := make([]map[string]interface{}, 0, 10)
oldRows := make(map[int64]reflect.Value, 10)
if oldRow != nil {
chOV = (*oldRow).FieldByIndex(ch.ClassIdx)
if !chOV.IsNil() {
ol := chOV.Len()
for i := 0; i < ol; i++ {
vv := chOV.Index(i)
vv = vv.Elem()
vID := vv.FieldByIndex(ch.PrimaryKey.ClassIdx)
id, ee := parseInt(vID.Interface())
if ee != nil {
oldRows[id] = vv
}
}
}
}
if !chV.IsNil() {
l := chV.Len()
for i := 0; i < l; i++ {
vv := chV.Index(i)
vv = vv.Elem()
vID := vv.FieldByIndex(ch.PrimaryKey.ClassIdx)
id, ee := parseInt(vID.Interface())
if ee == nil {
chOV, ok := oldRows[id]
if ok {
chJson = ch.marshalToJson(vv, &chOV)
delete(oldRows, id)
} else {
chJson = ch.marshalToJson(vv, nil)
}
} else {
chJson = ch.marshalToJson(vv, nil)
}
if chJson != nil {
jsonChArray = append(jsonChArray, chJson)
}
}
if len(jsonChArray) > 0 {
allSkipped = false
res[ch.TableName] = jsonChArray
}
}
if len(oldRows) > 0 {
allSkipped = false
for id, _ := range oldRows {
res[fmt.Sprintf("%s[%d]", ch.TableName, id)] = nil
}
}
}
}
if allSkipped {
return nil
} else {
return res
}
}
func parseName(fullName string) (name string, id int64, e error) {
if strings.HasSuffix(fullName, "]") {
idx := strings.Index(fullName, "[")
if idx > 0 {
ids := fullName[idx+1 : len(fullName)-1]
id, e = strconv.ParseInt(ids, 10, 64)
if e != nil {
return
}
name = fullName[:idx]
if idx == 0 {
e = fmt.Errorf("Invalid name: %s", fullName)
}
}
} else {
name = fullName
}
return
}
func (t *Table) getId(row reflect.Value) int64 {
vId := row.FieldByIndex(t.PrimaryKey.ClassIdx)
k := vId.Type().Kind()
if k == reflect.Ptr {
vId = vId.Elem()
}
switch k {
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
return vId.Int()
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return int64(vId.Uint())
}
return 0
}
func (t *Table) idxWithId(id int64, slice reflect.Value, lastIdx int) int {
for i := 0; i < lastIdx; i++ {
vR := slice.Index(i)
if vR.IsValid() {
vR = vR.Elem()
rowId := t.getId(vR)
if rowId == id {
return i
}
}
}
return -1
}
func (t *Table) applyJson(row reflect.Value, js map[string]interface{}) error {
var scanner gorbScanner
var e error
var name string
var idx int
var rowId int64
for key, value := range js {
switch jn := value.(type) {
case []interface{}:
ch := t.ChildByName(key)
if ch == nil {
return fmt.Errorf("There is no scope \"%s\"", key)
}
if ch.ChildClass.Kind() != reflect.Slice {
return fmt.Errorf("Scope \"%s\" is expected to be a slice", key)
}
chV := row.FieldByIndex(ch.ClassIdx)
if chV.IsNil() {
chV.Set(reflect.MakeSlice(ch.ChildClass, 0, 10))
}
lastIdx := chV.Len()
for _, jsn1 := range jn {
jsn, ok := jsn1.(map[string]interface{})
if ok {
rowId = 0
iv, ok := jsn[ch.PrimaryKey.FieldName]
if ok {
rowId, e = parseInt(iv)
if e != nil {
return e
}
}
idx = -1
if rowId != 0 {
idx = ch.idxWithId(rowId, chV, lastIdx)
}
if idx >= 0 {
chVV := chV.Index(idx)
e = ch.applyJson(chVV.Elem(), jsn)
} else {
chVV := reflect.New(ch.RowClass)
chV.Set(reflect.Append(chV, chVV))
e = ch.applyJson(chVV.Elem(), jsn)
}
if e != nil {
return e
}
} else {
return fmt.Errorf("Unsupported JSON type: %T", jsn1)
}
}
case map[string]interface{}:
name, rowId, e = parseName(key)
if e != nil {
return e
}
ch := t.ChildByName(name)
if ch == nil {
return fmt.Errorf("There is no scope \"%s\"", name)
}
chV := row.FieldByIndex(ch.ClassIdx)
if chV.IsNil() {
chVV := reflect.New(ch.RowClass)
e = ch.applyJson(chVV.Elem(), jn)
if e != nil {
return e
}
switch ch.ChildClass.Kind() {
case reflect.Ptr:
chV.Set(chVV)
case reflect.Slice:
chV.Set(reflect.MakeSlice(ch.ChildClass, 0, 10))
chV.Set(reflect.Append(chV, chVV))
}
} else {
switch ch.ChildClass.Kind() {
case reflect.Ptr:
e = ch.applyJson(chV.Elem(), jn)
if e != nil {
return e
}
case reflect.Slice:
idx = -1
if rowId == 0 {
iv, ok := jn[ch.PrimaryKey.FieldName]
if ok {
rowId, e = parseInt(iv)
if e != nil {
return e
}
}
}
if rowId != 0 {
idx = ch.idxWithId(rowId, chV, chV.Len())
}
if idx >= 0 {
chVV := chV.Index(idx)
e = ch.applyJson(chVV, jn)
} else {
chVV := reflect.New(ch.RowClass)
chV.Set(reflect.Append(chV, chVV))
e = ch.applyJson(chVV.Elem(), jn)
}
if e != nil {
return e
}
}
}
case nil:
name, rowId, e = parseName(key)
if e != nil {
return e
}
ch := t.ChildByName(name)
if ch != nil {
chV := row.FieldByIndex(ch.ClassIdx)
if !chV.IsNil() {
switch ch.ChildClass.Kind() {
case reflect.Ptr:
if idx != 0 {
rId := ch.getId(chV.Elem())
if rId == rowId {
chV.Set(reflect.Zero(ch.ChildClass))
}
} else {
chV.Set(reflect.Zero(ch.ChildClass))
}
case reflect.Slice:
if rowId != 0 {
idx = ch.idxWithId(rowId, chV, chV.Len())
if idx >= 0 {
l := chV.Len()
chIdV := chV.Index(idx)
chIdV.Set(reflect.Zero(chIdV.Type()))
if idx == 0 {
chV.Set(chV.Slice(1, l))
} else if idx == l-1 {
chV.Set(chV.Slice(0, l-1))
} else {
nS := chV.Slice(0, idx)
nS = reflect.AppendSlice(nS, chV.Slice(idx+1, l))
chV.Set(nS)
}
}
} else {
chV.Set(reflect.Zero(ch.ChildClass))
}
}
}
} else {
f := t.FieldByName(key)
if f == nil {
return fmt.Errorf("Field %s not found", key)
}
fV := row.FieldByIndex(f.ClassIdx)
fV.Set(reflect.Zero(fV.Type()))
}
default:
f := t.FieldByName(key)
if f == nil {
return fmt.Errorf("Field %s not found", key)
}
scanner.ptr = row.FieldByIndex(f.ClassIdx).Addr().Interface()
e = scanner.Scan(value)
if e != nil {
return e
}
}
}
return nil
}
func (conn *GorbManager) EntityJsonApply(entity interface{}, js []byte) error {
if entity == nil {
return fmt.Errorf("Entity is nil")
}
var ent *Entity
eType := reflect.TypeOf(entity)
var isPtr = eType.Kind() == reflect.Ptr
if !isPtr {
return fmt.Errorf("Pointer type is expected")
}
eType = eType.Elem()
ent = conn.LookupEntity(eType)
if ent == nil {
return fmt.Errorf("Unsupported entity %s", eType.Name())
}
var e error
var j map[string]interface{}
e = json.Unmarshal(js, &j)
if e != nil {
return e
}
return ent.applyJson(reflect.ValueOf(entity).Elem(), j)
}
func (conn *GorbManager) EntityJsonGet(newEntity, oldEntity interface{}) (ret []byte, e error) {
if newEntity == nil {
return nil, nil
}
var ent *Entity
eType := reflect.TypeOf(newEntity)
var isPtr = eType.Kind() == reflect.Ptr
if isPtr {
eType = eType.Elem()
}
ent = conn.LookupEntity(eType)
if ent == nil {
return nil, fmt.Errorf("Unsupported entity %s", eType.Name())
}
newValue := reflect.ValueOf(newEntity)
if isPtr {
newValue = newValue.Elem()
}
var res map[string]interface{}
if oldEntity != nil {
oldType := reflect.TypeOf(oldEntity)
isOldPtr := oldType.Kind() == reflect.Ptr
if isOldPtr {
oldType = oldType.Elem()
}
if eType != oldType {
return nil, fmt.Errorf("Old entity value has not the same type as ")
}
oldValue := reflect.ValueOf(oldEntity)
if isOldPtr {
oldValue = oldValue.Elem()
}
res = ent.marshalToJson(newValue, &oldValue)
} else {
res = ent.marshalToJson(newValue, nil)
}
ret, e = json.Marshal(res)
return
}