forked from bbuck/dragon-mud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relationship.go
144 lines (126 loc) · 3.58 KB
/
relationship.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
// Copyright (c) 2016-2017 Brandon Buck
package talon
import (
bolt "github.com/johnnadratowski/golang-neo4j-bolt-driver/structures/graph"
)
// Relationship represents a link between two nodes, it can come in two
// different kinds -- bounded or unbounded. If it's bounded then StartNodeID
// and EndNodeID will be set (and Bounded will be true). If it's unbounded
// these values will be 0.
type Relationship struct {
ID int64
StartNodeID int64
EndNodeID int64
Name string
Properties Properties
Bounded bool
}
// for bounded relationships
func wrapBoltRelationship(r bolt.Relationship) (*Relationship, error) {
var err error
p := Properties(r.Properties)
p, err = p.UnmarshaledProperties()
if err != nil {
return nil, err
}
return &Relationship{
ID: r.RelIdentity,
StartNodeID: r.StartNodeIdentity,
EndNodeID: r.EndNodeIdentity,
Name: r.Type,
Properties: Properties(r.Properties),
Bounded: true,
}, nil
}
// for unbounded relationships
func wrapBoltUnboundRelationship(r bolt.UnboundRelationship) (*Relationship, error) {
var err error
p := Properties(r.Properties)
p, err = p.UnmarshaledProperties()
if err != nil {
return nil, err
}
return &Relationship{
ID: r.RelIdentity,
Name: r.Type,
Properties: Properties(r.Properties),
Bounded: false,
}, nil
}
// Get will fetch the property assocaited with the relationship, returning a
// bool to signify if the property did exist.
func (r *Relationship) Get(key string) (val interface{}, ok bool) {
val, ok = r.Properties[key]
return
}
// GetString performs the same function as get, except it handles fetching
// a string or string pointer value (if it is a string). Unlike Get, the type
// Get methods will return 'false' for it's second return if the value is not
// of the requested type.
func (r *Relationship) GetString(key string) (string, bool) {
val, ok := r.Get(key)
if ok {
switch str := val.(type) {
case string:
return str, ok
case *string:
return *str, ok
}
}
return "", ok
}
// GetInt is similar to get except it handles fetching any integer type and
// converts it to an int64 (if it is any sized integer). Unlike Get, the type
// Get methods will return 'false' for it's second return if the value is not
// of the requested type.
func (r *Relationship) GetInt(key string) (int64, bool) {
val, ok := r.Get(key)
if ok {
switch i := val.(type) {
case int:
return int64(i), ok
case int8:
return int64(i), ok
case int16:
return int64(i), ok
case int32:
return int64(i), ok
case int64:
return i, ok
}
}
return 0, ok
}
// GetFloat is similar to get except it attempts to convert the found value
// to a float64 (if it is a float32 or float64). Unlike Get, the type Get
// methods will return 'false' for it's second return if the value is not of
// the requested type.
func (r *Relationship) GetFloat(key string) (float64, bool) {
val, ok := r.Get(key)
if ok {
switch f := val.(type) {
case float32:
return float64(f), ok
case float64:
return f, ok
}
}
return 0, ok
}
// GetBool is similar to Get except it attempts to convert the found value
// to a bool (if it is a bool). Unlike Get, the type Get methods will return
// 'false' for it's second return if the value is not of the requested type.
func (r *Relationship) GetBool(key string) (bool, bool) {
val, ok := r.Get(key)
if ok {
switch b := val.(type) {
case bool:
return b, ok
}
}
return false, ok
}
// Set will assign the given value to the associated Key.
func (r *Relationship) Set(key string, val interface{}) {
r.Properties[key] = val
}