-
Notifications
You must be signed in to change notification settings - Fork 38
/
node.go
84 lines (68 loc) · 1.59 KB
/
node.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
package redisgraph
import (
"fmt"
"strings"
)
// Node represents a node within a graph
type Node struct {
ID uint64
Labels []string
Alias string
Properties map[string]interface{}
graph *Graph
}
// NodeNew create a new Node
func NodeNew(labels []string, alias string, properties map[string]interface{}) *Node {
p := properties
if p == nil {
p = make(map[string]interface{})
}
return &Node{
Labels: labels,
Alias: alias,
Properties: p,
graph: nil,
}
}
// SetProperty asssign a new property to node
func (n *Node) SetProperty(key string, value interface{}) {
n.Properties[key] = value
}
// GetProperty retrieves property from node
func (n Node) GetProperty(key string) interface{} {
v, _ := n.Properties[key]
return v
}
// Returns a string representation of a node
func (n Node) String() string {
if len(n.Properties) == 0 {
return "{}"
}
p := make([]string, 0, len(n.Properties))
for k, v := range n.Properties {
p = append(p, fmt.Sprintf("%s:%v", k, ToString(v)))
}
s := fmt.Sprintf("{%s}", strings.Join(p, ","))
return s
}
// Encode makes Node satisfy the Stringer interface
func (n Node) Encode() string {
s := []string{"("}
if n.Alias != "" {
s = append(s, n.Alias)
}
for _, label := range n.Labels {
s = append(s, ":", label)
}
if len(n.Properties) > 0 {
p := make([]string, 0, len(n.Properties))
for k, v := range n.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, ")")
return strings.Join(s, "")
}