-
Notifications
You must be signed in to change notification settings - Fork 2
/
edge.go
78 lines (63 loc) · 1.61 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
package graphgo
// Edge has a unique key, properties, start and end node
type Edge struct {
Key string `json:"key"`
Label string `json:"label"`
Props map[string]interface{} `json:"props"`
Start string `json:"start"`
End string `json:"end"`
}
// NewEdge instanciates
func NewEdge(key, label string, start, end string, props map[string]interface{}) *Edge {
return &Edge{
Key: key,
Label: label,
Props: props,
Start: start,
End: end,
}
}
// SetProperty one by one
func (edge *Edge) SetProperty(key string, value interface{}) {
edge.Props[key] = value
}
// Get a property
func (edge *Edge) Get(key string) (interface{}, error) {
value, ok := edge.Props[key]
if !ok {
return nil, errorEdgePropNotFound(edge.Key, key)
}
return value, nil
}
// Hop returns the other node
func (edge *Edge) Hop(graph IGraph, key string) (INode, error) {
otherNodeKey := edge.Start
if otherNodeKey == key {
otherNodeKey = edge.End
}
node, err := graph.GetNode(otherNodeKey)
if err != nil {
return nil, err
}
return node, nil
}
// StartN returns the start node
func (edge *Edge) StartN(graph IGraph) (INode, error) {
return edge.Hop(graph, edge.End)
}
// EndN returns the end node
func (edge *Edge) EndN(graph IGraph) (INode, error) {
return edge.Hop(graph, edge.Start)
}
// GetLabel returns the label
func (edge *Edge) GetLabel() string {
return edge.Label
}
// GetKey returns the key
func (edge *Edge) GetKey() string {
return edge.Key
}
// GetProps returns all properties
func (edge *Edge) GetProps() map[string]interface{} {
return edge.Props
}