-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathedge.go
105 lines (87 loc) · 2.07 KB
/
edge.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
package redisgraph
import (
"fmt"
"strings"
)
// Edge represents an edge connecting two nodes in the graph.
type Edge struct {
ID uint64
Relation string
Source *Node
Destination *Node
Properties map[string]interface{}
srcNodeID uint64
destNodeID uint64
graph *Graph
}
// EdgeNew create a new Edge
func EdgeNew(relation string, srcNode *Node, destNode *Node, properties map[string]interface{}) *Edge {
p := properties
if p == nil {
p = make(map[string]interface{})
}
return &Edge{
Relation: relation,
Source: srcNode,
Destination: destNode,
Properties: p,
graph: nil,
}
}
// SetProperty assign a new property to edge
func (e *Edge) SetProperty(key string, value interface{}) {
e.Properties[key] = value
}
// GetProperty retrieves property from edge
func (e *Edge) GetProperty(key string) interface{} {
v, _ := e.Properties[key]
return v
}
// SourceNodeID returns edge source node ID
func (e Edge) SourceNodeID() uint64 {
if e.Source != nil {
return e.Source.ID
} else {
return e.srcNodeID
}
}
// DestNodeID returns edge destination node ID
func (e Edge) DestNodeID() uint64 {
if e.Source != nil {
return e.Destination.ID
} else {
return e.destNodeID
}
}
// Returns a string representation of edge
func (e Edge) String() string {
if len(e.Properties) == 0 {
return "{}"
}
p := make([]string, 0, len(e.Properties))
for k, v := range e.Properties {
p = append(p, fmt.Sprintf("%s:%v", k, ToString(v)))
}
s := fmt.Sprintf("{%s}", strings.Join(p, ","))
return s
}
// Encode makes Edge satisfy the Stringer interface
func (e Edge) Encode() string {
s := []string{"(", e.Source.Alias, ")"}
s = append(s, "-[")
if e.Relation != "" {
s = append(s, ":", e.Relation)
}
if len(e.Properties) > 0 {
p := make([]string, 0, len(e.Properties))
for k, v := range e.Properties {
p = append(p, fmt.Sprintf("%s:%v", k, ToString(v)))
}
s = append(s, "{")
s = append(s, strings.Join(p, ","))
s = append(s, "}")
}
s = append(s, "]->")
s = append(s, "(", e.Destination.Alias, ")")
return strings.Join(s, "")
}