forked from z5labs/gogm
/
save.go
885 lines (735 loc) · 21.9 KB
/
save.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
// Copyright (c) 2020 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"
dsl "github.com/mindstand/go-cypherdsl"
"github.com/neo4j/neo4j-go-driver/v4/neo4j"
"reflect"
)
// maximum supported depth
const maxSaveDepth = 10
const defaultSaveDepth = 1
// neoRunFunc typedefs function signature from neo4j driver
type neoRunFunc func(cypher string, params map[string]interface{}) (neo4j.Result, error)
// runWrap is used to wrap the session run function into a consistent function signature
func runWrap(sess neo4j.Session) neoRunFunc {
return func(cypher string, params map[string]interface{}) (neo4j.Result, error) {
if sess == nil {
return nil, fmt.Errorf("session can not be nil, %w", ErrInternal)
}
return sess.Run(cypher, params)
}
}
// nodeCreateConf holds configuration for creating new nodes
type nodeCreateConf struct {
// params to save
Params map[string]interface{}
// type to save by
Type reflect.Type
// whether the node is new or not
IsNew bool
}
// relCreateConf holds configuration for nodes to link together
type relCreateConf struct {
// start uuid of relationship
StartNodeUUID string
// end uuid of relationship
EndNodeUUID string
// any data to store in edge
Params map[string]interface{}
// holds direction of the edge
Direction dsl.Direction
}
// saves target node and connected node to specified depth
func saveDepth(runFunc neoRunFunc, obj interface{}, depth int) error {
if runFunc == nil {
return errors.New("session can not be nil")
}
if obj == nil {
return errors.New("obj can not be nil")
}
if depth < 0 {
return errors.New("cannot save a depth less than 0")
}
if depth > maxSaveDepth {
return fmt.Errorf("saving depth of (%v) is currently not supported, maximum depth is (%v), %w", depth, maxSaveDepth, ErrConfiguration)
}
//validate that obj is a pointer
rawType := reflect.TypeOf(obj)
if rawType.Kind() != reflect.Ptr {
return fmt.Errorf("obj must be of type pointer, not %T", obj)
}
//validate that the dereference type is a struct
derefType := rawType.Elem()
if derefType.Kind() != reflect.Struct {
return fmt.Errorf("dereference type can not be of type %T", obj)
}
//signature is [LABEL][UUID]{config}
nodes := map[string]map[string]nodeCreateConf{}
//signature is [LABEL] []{config}
relations := map[string][]relCreateConf{}
// node id -- [field] config
oldRels := map[string]map[string]*RelationConfig{}
curRels := map[string]map[string]*RelationConfig{}
// uuid -> reflect value
nodeRef := map[string]*reflect.Value{}
newNodes := []*string{}
visited := []string{}
rootVal := reflect.ValueOf(obj)
err := parseStruct("", "", false, dsl.DirectionBoth, nil, &rootVal, 0, depth, &nodes, &relations, &oldRels, &newNodes, &nodeRef, &visited)
if err != nil {
return err
}
ids, err := createNodes(runFunc, nodes, &nodeRef)
if err != nil {
return err
}
err = generateCurRels("", &rootVal, 0, depth, &curRels)
if err != nil {
return err
}
dels := calculateDels(oldRels, curRels)
//fix the cur rels and write them to their perspective nodes
for uuid, val := range nodeRef {
loadConf, ok := (curRels)[uuid]
if !ok {
return fmt.Errorf("load config not found for node [%s]", uuid)
}
//handle if its a pointer
if val.Kind() == reflect.Ptr {
*val = val.Elem()
}
reflect.Indirect(*val).FieldByName("LoadMap").Set(reflect.ValueOf(loadConf))
}
//execute concurrently
//calculate dels
if len(dels) != 0 {
err := removeRelations(runFunc, dels)
if err != nil {
return err
}
}
if len(relations) != 0 {
err := relateNodes(runFunc, relations, ids)
if err != nil {
return err
}
}
return nil
}
// calculates which relationships to delete
func calculateDels(oldRels, curRels map[string]map[string]*RelationConfig) map[string][]int64 {
if len(oldRels) == 0 {
return map[string][]int64{}
}
dels := map[string][]int64{}
for uuid, oldRelConf := range oldRels {
curRelConf, ok := curRels[uuid]
deleteAllRels := false
if !ok {
//this means that the node is gone, remove all rels to this node
deleteAllRels = true
} else {
for field, oldConf := range oldRelConf {
curConf, ok := curRelConf[field]
deleteAllRelsOnField := false
if !ok {
//this means that either the field has been removed or there are no more rels on this field,
//either way delete anything left over
deleteAllRelsOnField = true
}
for _, id := range oldConf.Ids {
//check if this id is new rels in the same location
if deleteAllRels || deleteAllRelsOnField {
if _, ok := dels[uuid]; !ok {
dels[uuid] = []int64{id}
} else {
dels[uuid] = append(dels[uuid], id)
}
} else {
if !int64SliceContains(curConf.Ids, id) {
if _, ok := dels[uuid]; !ok {
dels[uuid] = []int64{id}
} else {
dels[uuid] = append(dels[uuid], id)
}
}
}
}
}
}
}
return dels
}
// removes relationships between specified nodes
func removeRelations(runFunc neoRunFunc, dels map[string][]int64) error {
if dels == nil || len(dels) == 0 {
return nil
}
if runFunc == nil {
return fmt.Errorf("runFunc can not be nil, %w", ErrInternal)
}
var params []interface{}
for uuid, ids := range dels {
params = append(params, map[string]interface{}{
"startNodeId": uuid,
"endNodeIds": ids,
})
}
startParams, err := dsl.ParamsFromMap(map[string]interface{}{
"uuid": dsl.ParamString("row.startNodeId"),
})
if err != nil {
return fmt.Errorf("%s, %w", err.Error(), ErrInternal)
}
cyq, err := dsl.QB().
Cypher("UNWIND $rows as row").
Match(dsl.Path().
V(dsl.V{
Name: "start",
Params: startParams,
}).E(dsl.E{
Name: "e",
}).V(dsl.V{
Name: "end",
}).Build()).
Cypher("WHERE id(end) IN row.endNodeIds").
Delete(false, "e").
ToCypher()
if err != nil {
return err
}
res, err := runFunc(cyq, map[string]interface{}{
"rows": params,
})
if err != nil {
return fmt.Errorf("%s: %w", err.Error(), ErrInternal)
} else if err = res.Err(); err != nil {
return fmt.Errorf("%s: %w", err.Error(), ErrInternal)
}
//todo sanity check to make sure the affects worked
return nil
}
// creates nodes
func createNodes(runFunc neoRunFunc, crNodes map[string]map[string]nodeCreateConf, nodeRef *map[string]*reflect.Value) (map[string]int64, error) {
idMap := map[string]int64{}
for label, nodes := range crNodes {
var rows []interface{}
for _, config := range nodes {
rows = append(rows, config.Params)
}
params, err := dsl.ParamsFromMap(
map[string]interface{}{
"uuid": dsl.ParamString("row.uuid"),
})
if err != nil {
return nil, err
}
path, err := dsl.Path().V(dsl.V{
Name: "n",
Type: "`" + label + "`",
Params: params,
}).ToCypher()
if err != nil {
return nil, err
}
//todo replace once unwind is fixed and path
cyp, err := dsl.QB().
Cypher("UNWIND $rows as row").
Merge(&dsl.MergeConfig{
Path: path,
}).
Cypher("SET n += row").
Return(false, dsl.ReturnPart{
Name: "row.uuid",
Alias: "uuid",
}, dsl.ReturnPart{
Function: &dsl.FunctionConfig{
Name: "ID",
Params: []interface{}{dsl.ParamString("n")},
},
Alias: "id",
}).
ToCypher()
res, err := runFunc(cyp, map[string]interface{}{
"rows": rows,
})
if err != nil {
return nil, err
} else if res.Err() != nil {
return nil, res.Err()
}
for res.Next() {
row := res.Record().Values
if len(row) != 2 {
continue
}
uuid, ok := row[0].(string)
if !ok {
return nil, fmt.Errorf("cannot cast row[0] to string, %w", ErrInternal)
}
graphId, ok := row[1].(int64)
if !ok {
return nil, fmt.Errorf("cannot cast row[1] to int64, %w", ErrInternal)
}
idMap[uuid] = graphId
//set the new id
val, ok := (*nodeRef)[uuid]
if !ok {
return nil, fmt.Errorf("cannot find val for uuid [%s]", uuid)
}
reflect.Indirect(*val).FieldByName("Id").Set(reflect.ValueOf(graphId))
}
}
return idMap, nil
}
// relateNodes connects nodes together using edge config
func relateNodes(runFunc neoRunFunc, relations map[string][]relCreateConf, ids map[string]int64) error {
if relations == nil || len(relations) == 0 {
return errors.New("relations can not be nil or empty")
}
if ids == nil || len(ids) == 0 {
return errors.New("ids can not be nil or empty")
}
for label, rels := range relations {
var params []interface{}
if len(rels) == 0 {
continue
}
for _, rel := range rels {
startId, ok := ids[rel.StartNodeUUID]
if !ok {
return fmt.Errorf("can not find nodeId for uuid %s", rel.StartNodeUUID)
}
endId, ok := ids[rel.EndNodeUUID]
if !ok {
return fmt.Errorf("can not find nodeId for %T", rel)
}
//set map if its empty
if rel.Params == nil {
rel.Params = map[string]interface{}{}
}
params = append(params, map[string]interface{}{
"startNodeId": startId,
"endNodeId": endId,
"props": rel.Params,
})
}
mergePath, err := dsl.Path().
V(dsl.V{
Name: "startNode",
}).
E(dsl.E{
Name: "rel",
Types: []string{
label,
},
Direction: dsl.DirectionOutgoing,
}).
V(dsl.V{
Name: "endNode",
}).
ToCypher()
if err != nil {
return err
}
cyp, err := dsl.QB().
Cypher("UNWIND $rows as row").
Match(dsl.Path().V(dsl.V{Name: "startNode"}).Build()).
Where(dsl.C(&dsl.ConditionConfig{
FieldManipulationFunction: "ID",
Name: "startNode",
ConditionOperator: dsl.EqualToOperator,
Check: dsl.ParamString("row.startNodeId"),
})).
With(&dsl.WithConfig{
Parts: []dsl.WithPart{
{
Name: "row",
},
{
Name: "startNode",
},
},
}).
Match(dsl.Path().V(dsl.V{Name: "endNode"}).Build()).
Where(dsl.C(&dsl.ConditionConfig{
FieldManipulationFunction: "ID",
Name: "endNode",
ConditionOperator: dsl.EqualToOperator,
Check: dsl.ParamString("row.endNodeId"),
})).
Merge(&dsl.MergeConfig{
Path: mergePath,
}).
Cypher("SET rel += row.props").
ToCypher()
res, err := runFunc(cyp, map[string]interface{}{
"rows": params,
})
if err != nil {
return err
} else if err = res.Err(); err != nil {
return err
}
}
return nil
}
// validates data used by parse struct
func parseValidate(currentDepth, maxDepth int, current *reflect.Value, nodesPtr *map[string]map[string]nodeCreateConf, relationsPtr *map[string][]relCreateConf) error {
if currentDepth > maxDepth {
return nil
}
if nodesPtr == nil || relationsPtr == nil {
return errors.New("nodesPtr and/or relationsPtr can not be nil")
}
if current == nil {
return errors.New("current should never be nil")
}
return nil
}
// generates load map for updated structs
func generateCurRels(parentId string, current *reflect.Value, currentDepth, maxDepth int, curRels *map[string]map[string]*RelationConfig) error {
if currentDepth > maxDepth {
return nil
}
uuid := reflect.Indirect(*current).FieldByName("UUID").String()
if uuid == "" {
return errors.New("uuid not set")
}
if _, ok := (*curRels)[uuid]; ok {
//this node has already been seen
return nil
} else {
//create the record for it
(*curRels)[uuid] = map[string]*RelationConfig{}
}
//get the type
tString, err := getTypeName(current.Type())
if err != nil {
return err
}
//get the config
actual, ok := mappedTypes.Get(tString)
if !ok {
return fmt.Errorf("struct config not found type (%s)", tString)
}
//cast the config
currentConf, ok := actual.(structDecoratorConfig)
if !ok {
return errors.New("unable to cast into struct decorator config")
}
for _, conf := range currentConf.Fields {
if conf.Relationship == "" {
continue
}
relField := reflect.Indirect(*current).FieldByName(conf.FieldName)
//if its nil, just skip it
if relField.IsNil() {
continue
}
if conf.ManyRelationship {
slLen := relField.Len()
if slLen == 0 {
continue
}
for i := 0; i < slLen; i++ {
relVal := relField.Index(i)
newParentId, _, _, _, _, followVal, followId, _, err := processStruct(conf, &relVal, uuid, parentId)
if err != nil {
return err
}
//check the config is there for the specified field
if _, ok = (*curRels)[uuid][conf.FieldName]; !ok {
(*curRels)[uuid][conf.FieldName] = &RelationConfig{
Ids: []int64{},
RelationType: Multi,
}
}
(*curRels)[uuid][conf.FieldName].Ids = append((*curRels)[uuid][conf.FieldName].Ids, followId)
err = generateCurRels(newParentId, followVal, currentDepth+1, maxDepth, curRels)
if err != nil {
return err
}
}
} else {
newParentId, _, _, _, _, followVal, followId, _, err := processStruct(conf, &relField, uuid, parentId)
if err != nil {
return err
}
//check the config is there for the specified field
if _, ok = (*curRels)[uuid][conf.FieldName]; !ok {
(*curRels)[uuid][conf.FieldName] = &RelationConfig{
Ids: []int64{},
RelationType: Single,
}
}
(*curRels)[uuid][conf.FieldName].Ids = append((*curRels)[uuid][conf.FieldName].Ids, followId)
err = generateCurRels(newParentId, followVal, currentDepth+1, maxDepth, curRels)
if err != nil {
return err
}
}
}
return nil
}
// parses tree of structs
func parseStruct(parentId, edgeLabel string, parentIsStart bool, direction dsl.Direction, edgeParams map[string]interface{}, current *reflect.Value,
currentDepth, maxDepth int, nodesPtr *map[string]map[string]nodeCreateConf, relationsPtr *map[string][]relCreateConf, oldRels *map[string]map[string]*RelationConfig,
newNodes *[]*string, nodeRef *map[string]*reflect.Value, visited *[]string) error {
//check if its done
if currentDepth > maxDepth {
return nil
}
log.Debugf("on cycle %v", currentDepth)
//validate params
err := parseValidate(currentDepth, maxDepth, current, nodesPtr, relationsPtr)
if err != nil {
return err
}
//get the type
tString, err := getTypeName(current.Type())
if err != nil {
return err
}
//get the config
actual, ok := mappedTypes.Get(tString)
if !ok {
return fmt.Errorf("struct config not found type (%s)", tString)
}
//cast the config
currentConf, ok := actual.(structDecoratorConfig)
if !ok {
return errors.New("unable to cast into struct decorator config")
}
//set this to the actual field name later
isNewNode, id, relConf, err := handleNodeState(current, "UUID")
if err != nil {
return err
}
//set edge
if parentId != "" {
if _, ok := (*relationsPtr)[edgeLabel]; !ok {
(*relationsPtr)[edgeLabel] = []relCreateConf{}
}
start := ""
end := ""
curDir := direction
if parentIsStart {
start = parentId
end = id
} else {
start = id
end = parentId
if curDir == dsl.DirectionIncoming {
curDir = dsl.DirectionOutgoing
} else if curDir == dsl.DirectionOutgoing {
curDir = dsl.DirectionIncoming
}
}
if edgeParams == nil {
edgeParams = map[string]interface{}{}
}
found := false
//check if this edge is already here
if len((*relationsPtr)[edgeLabel]) != 0 {
for _, conf := range (*relationsPtr)[edgeLabel] {
if conf.StartNodeUUID == start && conf.EndNodeUUID == end {
found = true
}
}
}
if !found {
(*relationsPtr)[edgeLabel] = append((*relationsPtr)[edgeLabel], relCreateConf{
Direction: curDir,
Params: edgeParams,
StartNodeUUID: start,
EndNodeUUID: end,
})
}
}
//check if this has been visited yet, do this after edge is intentional
if stringSliceContains(*visited, id) {
return nil
} else {
*visited = append(*visited, id)
}
if !isNewNode {
if _, ok := (*oldRels)[id]; !ok {
(*oldRels)[id] = relConf
}
} else {
*newNodes = append(*newNodes, &id)
}
//set the reflect pointer so we can update the map later
if _, ok := (*nodeRef)[id]; !ok {
(*nodeRef)[id] = current
}
//convert params
params, err := toCypherParamsMap(*current, currentConf)
if err != nil {
return err
}
//if its nil, just default it
if params == nil {
params = map[string]interface{}{}
}
//set the map
if _, ok := (*nodesPtr)[currentConf.Label]; !ok {
(*nodesPtr)[currentConf.Label] = map[string]nodeCreateConf{}
}
(*nodesPtr)[currentConf.Label][id] = nodeCreateConf{
Type: current.Type(),
IsNew: isNewNode,
Params: params,
}
for _, conf := range currentConf.Fields {
if conf.Relationship == "" {
continue
}
relField := reflect.Indirect(*current).FieldByName(conf.FieldName)
//if its nil, just skip it
if relField.IsNil() {
continue
}
if conf.ManyRelationship {
slLen := relField.Len()
if slLen == 0 {
continue
}
for i := 0; i < slLen; i++ {
relVal := relField.Index(i)
newParentId, newEdgeLabel, newParentIdStart, newDirection, newEdgeParams, followVal, _, _, err := processStruct(conf, &relVal, id, parentId)
if err != nil {
return err
}
////makes us go backwards
//if skip {
// continue
//}
err = parseStruct(newParentId, newEdgeLabel, newParentIdStart, newDirection, newEdgeParams, followVal, currentDepth+1, maxDepth, nodesPtr, relationsPtr, oldRels, newNodes, nodeRef, visited)
if err != nil {
return err
}
}
} else {
newParentId, newEdgeLabel, newParentIdStart, newDirection, newEdgeParams, followVal, _, _, err := processStruct(conf, &relField, id, parentId)
if err != nil {
return err
}
//if skip {
// continue
//}
err = parseStruct(newParentId, newEdgeLabel, newParentIdStart, newDirection, newEdgeParams, followVal, currentDepth+1, maxDepth, nodesPtr, relationsPtr, oldRels, newNodes, nodeRef, visited)
if err != nil {
return err
}
}
}
return nil
}
// processStruct generates configuration for individual struct for saving
func processStruct(fieldConf decoratorConfig, relVal *reflect.Value, id, oldParentId string) (parentId, edgeLabel string, parentIsStart bool, direction dsl.Direction, edgeParams map[string]interface{}, followVal *reflect.Value, followId int64, skip bool, err error) {
edgeLabel = fieldConf.Relationship
relValName, err := getTypeName(relVal.Type())
if err != nil {
return "", "", false, 0, nil, nil, -1, false, err
}
actual, ok := mappedTypes.Get(relValName)
if !ok {
return "", "", false, 0, nil, nil, -1, false, fmt.Errorf("cannot find config for %s", edgeLabel)
}
edgeConf, ok := actual.(structDecoratorConfig)
if !ok {
return "", "", false, 0, nil, nil, -1, false, errors.New("can not cast to structDecoratorConfig")
}
if relVal.Type().Implements(edgeType) {
startValSlice := relVal.MethodByName("GetStartNode").Call(nil)
endValSlice := relVal.MethodByName("GetEndNode").Call(nil)
if len(startValSlice) == 0 || len(endValSlice) == 0 {
return "", "", false, 0, nil, nil, -1, false, errors.New("edge is invalid, sides are not set")
}
startId := reflect.Indirect(startValSlice[0].Elem()).FieldByName("UUID").String()
endId := reflect.Indirect(endValSlice[0].Elem()).FieldByName("UUID").String()
params, err := toCypherParamsMap(*relVal, edgeConf)
if err != nil {
return "", "", false, 0, nil, nil, -1, false, err
}
//if its nil, just default it
if params == nil {
params = map[string]interface{}{}
}
if startId == id {
//follow the end
retVal := endValSlice[0].Elem()
Iid := reflect.Indirect(retVal).FieldByName("Id").Interface()
followId, ok := Iid.(int64)
if !ok {
followId = 0
}
//check that we're not going in circles
if oldParentId != "" {
if endId == oldParentId {
return startId, edgeLabel, true, fieldConf.Direction, params, &retVal, followId, true, nil
}
}
return startId, edgeLabel, true, fieldConf.Direction, params, &retVal, followId, false, nil
} else if endId == id {
///follow the start
retVal := startValSlice[0].Elem()
Iid := reflect.Indirect(retVal).FieldByName("Id").Interface()
followId, ok := Iid.(int64)
if !ok {
followId = 0
}
if oldParentId != "" {
if startId == oldParentId {
return endId, edgeLabel, false, fieldConf.Direction, params, &retVal, followId, true, nil
}
}
return endId, edgeLabel, false, fieldConf.Direction, params, &retVal, followId, false, nil
} else {
return "", "", false, 0, nil, nil, -1, false, errors.New("edge is invalid, doesn't point to parent vertex")
}
} else {
var followId int64
if oldParentId != "" {
if relVal.Kind() == reflect.Ptr {
*relVal = relVal.Elem()
}
Iid := reflect.Indirect(*relVal).FieldByName("Id").Interface()
followId, ok = Iid.(int64)
if !ok {
followId = 0
}
if relVal.FieldByName("UUID").String() == oldParentId {
return id, edgeLabel, fieldConf.Direction == dsl.DirectionOutgoing, fieldConf.Direction, map[string]interface{}{}, relVal, followId, true, nil
}
} else {
Iid := reflect.Indirect(*relVal).FieldByName("Id").Interface()
followId, ok = Iid.(int64)
if !ok {
followId = 0
}
}
return id, edgeLabel, fieldConf.Direction == dsl.DirectionOutgoing, fieldConf.Direction, map[string]interface{}{}, relVal, followId, false, nil
}
}