-
Notifications
You must be signed in to change notification settings - Fork 32
/
decoder.go
715 lines (601 loc) · 21 KB
/
decoder.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// Copyright (c) 2022 MindStand Technologies, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package gogm
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
func traverseResultRecordValues(values []interface{}) ([]neo4j.Path, []neo4j.Relationship, []neo4j.Node) {
var paths []neo4j.Path
var strictRels []neo4j.Relationship
var isolatedNodes []neo4j.Node
for _, value := range values {
switch ct := value.(type) {
case neo4j.Path:
paths = append(paths, ct)
case neo4j.Relationship:
strictRels = append(strictRels, ct)
case neo4j.Node:
isolatedNodes = append(isolatedNodes, ct)
case []interface{}:
v, ok := value.([]interface{})
if ok {
p, r, n := traverseResultRecordValues(v)
paths = append(paths, p...)
strictRels = append(strictRels, r...)
isolatedNodes = append(isolatedNodes, n...)
}
default:
continue
}
}
return paths, strictRels, isolatedNodes
}
//decodes raw path response from driver
//example query `match p=(n)-[*0..5]-() return p`
func decode(gogm *Gogm, result neo4j.Result, respObj interface{}) (err error) {
//check nil params
if result == nil {
return fmt.Errorf("result can not be nil, %w", ErrInvalidParams)
}
//we're doing reflection now, lets set up a panic recovery
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v - PANIC RECOVERY - %w", r, ErrInternal)
}
}()
if respObj == nil {
return fmt.Errorf("response object can not be nil - %w", ErrInvalidParams)
}
returnValue := reflect.ValueOf(respObj)
returnType := reflect.TypeOf(respObj)
// check type is valid
if returnValue.Kind() != reflect.Ptr || returnValue.IsNil() {
return fmt.Errorf("invalid resp type %T - %w", respObj, ErrInvalidParams)
}
primaryLabel, err := getPrimaryLabel(returnType)
if err != nil {
return fmt.Errorf("failed to get primary label from returnType: %w", err)
}
if primaryLabel == "" {
return errors.New("label was empty for returnType")
}
var paths []neo4j.Path
var strictRels []neo4j.Relationship
var isolatedNodes []neo4j.Node
for result.Next() {
p, r, n := traverseResultRecordValues(result.Record().Values)
paths = append(paths, p...)
strictRels = append(strictRels, r...)
isolatedNodes = append(isolatedNodes, n...)
}
nodeLookup := make(map[int64]*reflect.Value)
relMaps := make(map[int64]map[string]*RelationConfig)
var pks []int64
rels := make(map[int64]*neoEdgeConfig)
labelLookup := map[int64]string{}
returnIsSingle := returnType.Elem().Kind() != reflect.Slice
returnUsed := ptrToBool(false)
if len(paths) != 0 {
err = sortPaths(gogm, paths, nodeLookup, rels, &pks, returnIsSingle, returnUsed, &returnValue, primaryLabel, relMaps)
if err != nil {
return err
}
}
if len(isolatedNodes) != 0 {
err = sortIsolatedNodes(gogm, isolatedNodes, labelLookup, nodeLookup, &pks, returnIsSingle, returnUsed, &returnValue, primaryLabel, relMaps)
if err != nil {
return err
}
}
if len(strictRels) != 0 {
err = sortStrictRels(strictRels, labelLookup, rels)
if err != nil {
return err
}
}
//check if we have anything to do
if len(pks) == 0 {
return fmt.Errorf("no primary nodes to return, %w", ErrNotFound)
}
//build relationships
for _, relationConfig := range rels {
if relationConfig.StartNodeType == "" || relationConfig.EndNodeType == "" {
continue
}
//grab reflect value for start
start, _, err := getValueAndConfig(gogm, relationConfig.StartNodeId, relationConfig.StartNodeType, nodeLookup)
if err != nil {
return err
}
//grab reflect value for end
end, _, err := getValueAndConfig(gogm, relationConfig.EndNodeId, relationConfig.EndNodeType, nodeLookup)
if err != nil {
return err
}
startConfig, endConfig, err := gogm.mappedRelations.GetConfigs(relationConfig.StartNodeType, relationConfig.EndNodeType,
relationConfig.EndNodeType, relationConfig.StartNodeType, relationConfig.Type)
if err != nil {
return err
}
// handle from start side of edge
if startMap, ok := relMaps[relationConfig.StartNodeId]; ok {
if conf, ok := startMap[startConfig.FieldName]; ok {
conf.Ids = append(conf.Ids, relationConfig.EndNodeId)
} else {
var rt RelationType
if startConfig.ManyRelationship {
rt = Multi
} else {
rt = Single
}
newConf := &RelationConfig{
Ids: []int64{relationConfig.EndNodeId},
RelationType: rt,
}
startMap[startConfig.FieldName] = newConf
}
} else {
return fmt.Errorf("relation config not found for id [%v]", relationConfig.StartNodeId)
}
// handle from end side of edge
if endMap, ok := relMaps[relationConfig.EndNodeId]; ok {
if conf, ok := endMap[endConfig.FieldName]; ok {
conf.Ids = append(conf.Ids, relationConfig.StartNodeId)
} else {
var rt RelationType
if endConfig.ManyRelationship {
rt = Multi
} else {
rt = Single
}
newConf := &RelationConfig{
Ids: []int64{relationConfig.StartNodeId},
RelationType: rt,
}
endMap[endConfig.FieldName] = newConf
}
} else {
return fmt.Errorf("relation config not found for id [%v]", relationConfig.StartNodeId)
}
if startConfig.UsesEdgeNode {
var typeConfig structDecoratorConfig
specialEdgeType := startConfig.Type
//get the actual type if its a slice
if specialEdgeType.Kind() == reflect.Slice {
specialEdgeType = specialEdgeType.Elem()
}
label := ""
if specialEdgeType.Kind() == reflect.Ptr {
label = specialEdgeType.Elem().Name()
} else {
label = specialEdgeType.Name()
specialEdgeType = reflect.PtrTo(specialEdgeType)
}
edgeDecoratorConfig, ok := gogm.mappedTypes.Get(label) // mappedTypes[boltNode.Labels[0]]
if !ok {
return fmt.Errorf("can not find mapping for node with label %s - %w", label, ErrInternal)
}
typeConfig = edgeDecoratorConfig.(structDecoratorConfig)
if !ok {
return fmt.Errorf("unable to cast [%T] to structDecoratorConfig - %w", edgeDecoratorConfig, ErrInternal)
}
//create value
specialEdgeValue, err := convertToValue(gogm, relationConfig.Id, typeConfig, relationConfig.Obj, specialEdgeType, false, false, returnUsed, nil)
if err != nil {
return err
}
var startCall reflect.Value
var endCall reflect.Value
if start.Kind() != reflect.Ptr {
startCall = start.Addr()
} else {
startCall = *start
}
if end.Kind() != reflect.Ptr {
endCall = end.Addr()
} else {
endCall = *end
}
//can ensure that specialEdgeType implements proper interface if specialEdgeType made specialEdgeType this far
res := specialEdgeValue.MethodByName("SetStartNode").Call([]reflect.Value{startCall})
if len(res) == 0 {
return fmt.Errorf("invalid response from edge callback - %w", err)
} else if !res[0].IsNil() {
return fmt.Errorf("failed call to SetStartNode - %w", res[0].Interface().(error))
}
res = specialEdgeValue.MethodByName("SetEndNode").Call([]reflect.Value{endCall})
if len(res) == 0 {
return fmt.Errorf("invalid response from edge callback - %w", err)
} else if !res[0].IsNil() {
return fmt.Errorf("failed call to SetEndNode - %w", res[0].Interface().(error))
}
//set edge for end
if start.Kind() == reflect.Ptr {
*start = start.Elem()
}
if end.Kind() == reflect.Ptr {
*end = end.Elem()
}
if end.FieldByName(endConfig.FieldName).Kind() == reflect.Slice {
end.FieldByName(endConfig.FieldName).Set(reflect.Append(end.FieldByName(endConfig.FieldName), *specialEdgeValue))
} else {
//non slice relationships are already asserted to be pointers
end.FieldByName(endConfig.FieldName).Set(*specialEdgeValue)
}
//set edge for start
if start.FieldByName(startConfig.FieldName).Kind() == reflect.Slice {
start.FieldByName(startConfig.FieldName).Set(reflect.Append(start.FieldByName(startConfig.FieldName), *specialEdgeValue))
} else {
start.FieldByName(startConfig.FieldName).Set(*specialEdgeValue)
}
} else {
if start.Kind() == reflect.Ptr {
*start = start.Elem()
}
if end.Kind() == reflect.Ptr {
*end = end.Elem()
}
if end.FieldByName(endConfig.FieldName).Kind() == reflect.Slice {
end.FieldByName(endConfig.FieldName).Set(reflect.Append(end.FieldByName(endConfig.FieldName), start.Addr()))
} else {
end.FieldByName(endConfig.FieldName).Set(start.Addr())
}
//relate end-start
if start.FieldByName(startConfig.FieldName).Kind() == reflect.Slice {
start.FieldByName(startConfig.FieldName).Set(reflect.Append(start.FieldByName(startConfig.FieldName), end.Addr()))
} else {
start.FieldByName(startConfig.FieldName).Set(end.Addr())
}
}
}
//set load maps
if len(rels) != 0 {
for id, val := range nodeLookup {
reflect.Indirect(*val).FieldByName(loadMapField).Set(reflect.ValueOf(relMaps[id]))
}
}
//handle if its returning a slice -- validation has been done at an earlier step
if !returnIsSingle {
reflection := reflect.MakeSlice(returnType.Elem(), 0, cap(pks))
reflectionValue := reflect.New(reflection.Type())
reflectionValue.Elem().Set(reflection)
slicePtr := reflect.ValueOf(reflectionValue.Interface())
sliceValuePtr := slicePtr.Elem()
sliceType := returnType.Elem().Elem()
for _, id := range pks {
val, ok := nodeLookup[id]
if !ok {
return fmt.Errorf("cannot find value with id (%v)", id)
}
//handle slice of pointers
if sliceType.Kind() == reflect.Ptr {
sliceValuePtr.Set(reflect.Append(sliceValuePtr, val.Addr()))
} else {
sliceValuePtr.Set(reflect.Append(sliceValuePtr, *val))
}
}
reflect.Indirect(returnValue).Set(sliceValuePtr)
return err
}
return err
}
// getPrimaryLabel gets the label from a reflect type
func getPrimaryLabel(rt reflect.Type) (string, error) {
//assume its already a pointer
rt = rt.Elem()
if rt.Kind() == reflect.Slice {
rt = rt.Elem()
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
} else if rt.Kind() == reflect.Ptr {
return "", errors.New("must be pointer to struct of slice, can not be a pointer to another pointer")
}
return rt.Name(), nil
}
// sortIsolatedNodes process nodes that are returned individually from bolt driver
func sortIsolatedNodes(gogm *Gogm, isolatedNodes []neo4j.Node, labelLookup map[int64]string, nodeLookup map[int64]*reflect.Value, pks *[]int64, pkSingle bool, passTypeUsed *bool, passValue *reflect.Value, pkLabel string, relMaps map[int64]map[string]*RelationConfig) error {
if isolatedNodes == nil {
return fmt.Errorf("isolatedNodes can not be nil, %w", ErrInternal)
}
for _, node := range isolatedNodes {
//check if node has already been found by another process
if _, ok := nodeLookup[node.Id]; !ok {
//primary to return
isPk := false
if node.Labels != nil && len(node.Labels) != 0 && node.Labels[0] == pkLabel {
*pks = append(*pks, node.Id)
isPk = true
}
//if it hasn't, map it
val, err := convertNodeToValue(gogm, node, pkSingle, isPk, passTypeUsed, passValue)
if err != nil {
return err
}
nodeLookup[node.Id] = val
relMaps[node.Id] = map[string]*RelationConfig{}
//set label map
if _, ok := labelLookup[node.Id]; !ok && len(node.Labels) != 0 { //&& node.Labels[0] == pkLabel {
labelLookup[node.Id] = node.Labels[0]
}
}
}
return nil
}
// sortStrictRels sorts relationships that are strictly defined (i.e direction is pre defined) from the bolt driver
func sortStrictRels(strictRels []neo4j.Relationship, labelLookup map[int64]string, rels map[int64]*neoEdgeConfig) error {
if strictRels == nil {
return fmt.Errorf("paths is empty, that shouldn't have happened, %w", ErrInternal)
}
for _, rel := range strictRels {
if _, ok := rels[rel.Id]; !ok {
startLabel, ok := labelLookup[rel.StartId]
if !ok {
return fmt.Errorf("label not found for node [%v], %w", rel.Id, ErrInternal)
}
endLabel, ok := labelLookup[rel.EndId]
if !ok {
return fmt.Errorf("label not found for node [%v], %w", rel.EndId, ErrInternal)
}
rels[rel.Id] = &neoEdgeConfig{
Id: rel.Id,
StartNodeId: rel.StartId,
StartNodeType: startLabel,
EndNodeId: rel.EndId,
EndNodeType: endLabel,
Obj: rel.Props,
Type: rel.Type,
}
}
}
return nil
}
// sortPaths sorts nodes and relationships from bolt driver that dont specify the direction explicitly, instead uses the bolt spec to determine direction
func sortPaths(gogm *Gogm, paths []neo4j.Path, nodeLookup map[int64]*reflect.Value, rels map[int64]*neoEdgeConfig, pks *[]int64, pkSingle bool, passTypeUsed *bool, passValue *reflect.Value, pkLabel string, relMaps map[int64]map[string]*RelationConfig) error {
if paths == nil {
return fmt.Errorf("paths is empty, that shouldn't have happened, %w", ErrInternal)
}
for _, path := range paths {
if path.Nodes == nil || len(path.Nodes) == 0 {
return fmt.Errorf("no nodes found, %w", ErrNotFound)
}
labelLookup := make(map[int64]string, len(path.Nodes))
for _, node := range path.Nodes {
if _, ok := labelLookup[node.Id]; !ok && len(node.Labels) != 0 {
labelLookup[node.Id] = node.Labels[0]
}
if _, ok := nodeLookup[node.Id]; !ok {
//primary to return
isPk := false
if node.Labels != nil && len(node.Labels) != 0 && node.Labels[0] == pkLabel {
*pks = append(*pks, node.Id)
isPk = true
}
//we haven't parsed this one yet, lets do that now
val, err := convertNodeToValue(gogm, node, pkSingle, isPk, passTypeUsed, passValue)
if err != nil {
return fmt.Errorf("failed to convert node to value, %w", err)
}
nodeLookup[node.Id] = val
relMaps[node.Id] = map[string]*RelationConfig{}
}
}
for _, rel := range path.Relationships {
startLabel, ok := labelLookup[rel.StartId]
if !ok {
return fmt.Errorf("label not found for node with graphId [%v], %w", rel.StartId, ErrInternal)
}
endLabel, ok := labelLookup[rel.EndId]
if !ok {
return fmt.Errorf("label not found for node with graphId [%v], %w", rel.EndId, ErrInternal)
}
if _, ok := rels[rel.Id]; !ok {
rels[rel.Id] = &neoEdgeConfig{
Id: rel.Id,
StartNodeId: rel.StartId,
StartNodeType: startLabel,
EndNodeId: rel.EndId,
EndNodeType: endLabel,
Obj: rel.Props,
Type: rel.Type,
}
}
}
}
return nil
}
// getValueAndConfig returns reflect value of specific node and the configuration for the node
func getValueAndConfig(gogm *Gogm, id int64, _type string, nodeLookup map[int64]*reflect.Value) (val *reflect.Value, conf structDecoratorConfig, err error) {
var ok bool
val, ok = nodeLookup[id]
if !ok {
return nil, structDecoratorConfig{}, fmt.Errorf("value for id (%v) not found", id)
}
temp, ok := gogm.mappedTypes.Get(_type)
if !ok {
return nil, structDecoratorConfig{}, fmt.Errorf("no config found for type (%s)", _type)
}
conf, ok = temp.(structDecoratorConfig)
if !ok {
return nil, structDecoratorConfig{}, errors.New("unable to cast to structDecoratorConfig")
}
return
}
var sliceOfEmptyInterface []interface{}
var emptyInterfaceType = reflect.TypeOf(sliceOfEmptyInterface).Elem()
// convertToValue converts properties map from neo4j to golang reflect value
func convertToValue(gogm *Gogm, graphId int64, conf structDecoratorConfig, props map[string]interface{}, rtype reflect.Type, pkSingle, isPkValue bool, passValueUsed *bool, passValue *reflect.Value) (valPtr *reflect.Value, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recovered converToValue: %v", r)
}
}()
if rtype == nil {
return nil, errors.New("rtype can not be nil")
}
isPtr := false
if rtype.Kind() == reflect.Ptr {
isPtr = true
rtype = rtype.Elem()
}
val := reflect.New(rtype)
if graphId >= 0 {
reflect.Indirect(val).FieldByName("Id").Set(reflect.ValueOf(&graphId))
}
for field, fieldConfig := range conf.Fields {
if fieldConfig.Name == "id" {
continue //id is handled above
}
//skip if its a relation field
if fieldConfig.Relationship != "" {
continue
}
if fieldConfig.Ignore {
continue
}
raw, ok := props[fieldConfig.Name]
if !ok {
if fieldConfig.IsTypeDef {
gogm.logger.Debugf("skipping field %s since it is typedeffed and not defined", fieldConfig.Name)
continue
}
}
rawVal := reflect.ValueOf(raw)
indirect := reflect.Indirect(val)
if fieldConfig.Properties && fieldConfig.PropConfig != nil {
if fieldConfig.PropConfig.IsMap {
var sub reflect.Type
if fieldConfig.PropConfig.IsMapSlice {
sub = fieldConfig.PropConfig.MapSliceType
} else {
sub = fieldConfig.PropConfig.SubType
}
mapType := reflect.MapOf(reflect.TypeOf(""), sub)
mapVal := reflect.MakeMap(mapType)
for k, v := range props {
if !strings.HasPrefix(k, fmt.Sprintf("%s.", fieldConfig.Name)) {
continue
}
mapKey := strings.Replace(k, fieldConfig.Name+".", "", 1)
if fieldConfig.PropConfig.IsMapSlice {
if v == nil {
// skip if nil
continue
}
sliceVal := reflect.ValueOf(v)
if sliceVal.IsZero() {
// cant do anything with a zero value
continue
}
rawLen := sliceVal.Len()
sl := reflect.MakeSlice(fieldConfig.PropConfig.MapSliceType, rawLen, sliceVal.Cap())
for i := 0; i < rawLen; i++ {
slVal := sliceVal.Index(i)
if fieldConfig.PropConfig.SubType == slVal.Type() {
sl.Index(i).Set(slVal)
} else {
sl.Index(i).Set(slVal.Elem().Convert(fieldConfig.PropConfig.SubType))
}
}
if fieldConfig.PropConfig.IsMapSliceTd {
sl = sl.Convert(fieldConfig.PropConfig.MapSliceType)
}
mapVal.SetMapIndex(reflect.ValueOf(mapKey), sl)
} else {
vVal := reflect.ValueOf(v)
if fieldConfig.PropConfig.SubType == vVal.Type() {
mapVal.SetMapIndex(reflect.ValueOf(mapKey), vVal)
} else {
mapVal.SetMapIndex(reflect.ValueOf(mapKey), vVal.Convert(fieldConfig.PropConfig.SubType))
}
}
}
if mapVal.Type() != fieldConfig.Type {
mapVal = mapVal.Convert(fieldConfig.Type)
}
indirect.FieldByName(field).Set(mapVal)
} else {
if raw == nil || rawVal.IsZero() {
// cant do anything with a zero value
continue
}
rawLen := rawVal.Len()
sl := reflect.MakeSlice(reflect.SliceOf(fieldConfig.PropConfig.SubType), rawLen, rawVal.Cap())
for i := 0; i < rawLen; i++ {
slVal := rawVal.Index(i)
if fieldConfig.PropConfig.SubType == slVal.Type() {
sl.Index(i).Set(slVal)
} else {
sl.Index(i).Set(slVal.Elem().Convert(fieldConfig.PropConfig.SubType))
}
}
if sl.Type() != fieldConfig.Type {
sl = sl.Convert(fieldConfig.Type)
}
indirect.FieldByName(field).Set(sl)
}
} else {
if raw == nil || rawVal.IsZero() {
continue
}
if indirect.FieldByName(field).Type() == rawVal.Type() {
indirect.FieldByName(field).Set(rawVal)
} else {
indirect.FieldByName(field).Set(rawVal.Convert(indirect.FieldByName(field).Type()))
}
}
}
// handle pk case
if isPkValue && pkSingle && !*passValueUsed {
// pass value will always be a pointer
if isPtr {
reflect.Indirect(*passValue).Set(val)
} else {
reflect.Indirect(*passValue).Set(val.Elem())
}
*passValueUsed = true
return passValue, nil
}
//if its not a pointer, dereference it
if !isPtr {
retV := reflect.Indirect(val)
return &retV, nil
}
return &val, err
}
// convertNodeToValue converts raw bolt node to reflect value
func convertNodeToValue(gogm *Gogm, boltNode neo4j.Node, isPkValue, pkSingle bool, passTypeUsed *bool, passValue *reflect.Value) (*reflect.Value, error) {
if boltNode.Labels == nil || len(boltNode.Labels) == 0 {
return nil, errors.New("boltNode has no labels")
}
var typeConfig structDecoratorConfig
temp, ok := gogm.mappedTypes.Get(boltNode.Labels[0]) // mappedTypes[boltNode.Labels[0]]
if !ok {
return nil, fmt.Errorf("can not find mapping for node with label %s", boltNode.Labels[0])
}
typeConfig, ok = temp.(structDecoratorConfig)
if !ok {
return nil, errors.New("unable to cast to struct decorator config")
}
return convertToValue(gogm, boltNode.Id, typeConfig, boltNode.Props, typeConfig.Type, pkSingle, isPkValue, passTypeUsed, passValue)
}