-
Notifications
You must be signed in to change notification settings - Fork 5
/
node.go
276 lines (234 loc) · 6.19 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
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
package cypher
import (
"errors"
)
type Node struct {
symbolicName SymbolicName
labels []NodeLabel
properties Properties
key string
notNil bool
err error
}
func NodeCreate() Node {
node := Node{
notNil: true,
}
return node
}
func NodeCreate1(primaryLabel string, properties Properties, additionalLabels ...string) Node {
if primaryLabel == "" {
return NodeError(errors.New("primary label is required"))
}
for _, label := range additionalLabels {
if label == "" {
return NodeError(errors.New("empty label is not allowed"))
}
}
labels := make([]NodeLabel, 0)
labels = append(labels, NodeLabel{value: primaryLabel})
for _, label := range additionalLabels {
labels = append(labels, NodeLabelCreate(label))
}
node := Node{
notNil: true,
properties: properties,
labels: labels,
}
node.injectKey()
return node
}
func NodeCreate2(primaryLabel string) Node {
if primaryLabel == "" {
return NodeError(errors.New("primary label is required"))
}
var labels = make([]NodeLabel, 0)
labels = append(labels, NodeLabelCreate(primaryLabel))
node := Node{
labels: labels,
notNil: true,
}
node.injectKey()
return node
}
func NodeCreate3(primaryLabel string, additionalLabel ...string) Node {
if primaryLabel == "" {
return NodeError(errors.New("primary label is required"))
}
for _, label := range additionalLabel {
if label == "" {
return NodeError(errors.New("empty label is not allowed"))
}
}
var labels = make([]NodeLabel, 0)
labels = append(labels, NodeLabel{value: primaryLabel})
for _, label := range additionalLabel {
labels = append(labels, NodeLabelCreate(label))
}
node := Node{
labels: labels,
}
node.injectKey()
return node
}
func NodeCreate4(newProperties MapExpression, node Node) Node {
newNode := Node{symbolicName: node.symbolicName, labels: node.labels, notNil: true, properties: PropertiesCreate(newProperties)}
newNode.injectKey()
return newNode
}
func NodeCreate5(primaryLabel string, properties MapExpression, additionalLabel ...string) Node {
return NodeCreate1(primaryLabel, PropertiesCreate(properties), additionalLabel...)
}
func (node *Node) injectKey() {
node.key = getAddress(node)
}
func NodeError(err error) Node {
return Node{
err: err,
}
}
func (node Node) GetRequiredSymbolicName() SymbolicName {
if node.symbolicName.isNotNil() {
return node.symbolicName
}
return SymbolicNameError(errors.New("node get symbolic name: no name present"))
}
func (node Node) GetSymbolicName() SymbolicName {
return node.symbolicName
}
func (node Node) isNotNil() bool {
return node.notNil
}
func (node Node) IsPatternElement() bool {
return true
}
func (node Node) getKey() string {
return node.key
}
func (node Node) hasSymbolic() bool {
return node.symbolicName.isNotNil()
}
func (node Node) GetError() error {
return node.err
}
func (node Node) accept(visitor *CypherRenderer) {
visitor.enter(node)
VisitIfNotNull(node.symbolicName, visitor)
for _, label := range node.labels {
label.accept(visitor)
}
VisitIfNotNull(node.properties, visitor)
visitor.leave(node)
}
func (node Node) enter(renderer *CypherRenderer) {
renderer.append("(")
if !node.hasSymbolic() {
return
}
_, renderer.skipNodeContent = renderer.visitedNamed[node.key]
renderer.visitedNamed[node.key] = 1
if renderer.skipNodeContent {
renderer.append(node.symbolicName.value)
}
}
func (node Node) leave(renderer *CypherRenderer) {
renderer.append(")")
renderer.skipNodeContent = false
}
func (node Node) RelationshipTo(other Node, types ...string) Relationship {
return RelationshipCreate(node, LTR(), other, types...)
}
func (node Node) RelationshipFrom(other Node, types ...string) Relationship {
return RelationshipCreate(node, RTL(), other, types...)
}
func (node Node) RelationshipBetween(other Node, types ...string) Relationship {
return RelationshipCreate(node, UNI(), other, types...)
}
func (node Node) WithRawProperties(keysAndValues ...interface{}) Node {
properties := MapExpression{}
if keysAndValues != nil && len(keysAndValues) != 0 {
properties = NewMapExpression(keysAndValues...)
if properties.GetError() != nil {
return NodeError(properties.GetError())
}
}
return node.WithProperties(properties)
}
func (node Node) WithProperties(newProperties MapExpression) Node {
return NodeCreate4(newProperties, node)
}
func (node Node) Property(name string) Property {
return PropertyCreate(node, name)
}
func (node Node) NamedByString(name string) Node {
node.symbolicName = SymbolicNameCreate(name)
return node
}
func (node Node) Named(name SymbolicName) Node {
if name.GetError() != nil {
return NodeError(name.GetError())
}
if !name.isNotNil() {
return NodeError(errors.New("node named: symbolic name is required"))
}
node.symbolicName = name
return node
}
func (node Node) As(alias string) AliasedExpression {
symbolicName := node.GetRequiredSymbolicName()
if symbolicName.GetError() != nil {
return AliasedExpressionError(symbolicName.GetError())
}
return ExpressionWrap(symbolicName).As(alias).Get().(AliasedExpression)
}
func (node Node) InternalId() FunctionInvocation {
return IdByNode(node)
}
func (node Node) HasLabels(labelsToQuery ...string) Condition {
return HasLabelConditionCreate(node.GetRequiredSymbolicName(), labelsToQuery...)
}
func (node Node) Labels() FunctionInvocation {
return Labels(node)
}
func (node Node) Project(entries ...interface{}) MapProjection {
return MapProjectionCreate(node.GetRequiredSymbolicName(), entries...)
}
type NodeLabel struct {
value string
key string
notNil bool
err error
}
func NodeLabelCreate(value string) NodeLabel {
n := NodeLabel{
value: value,
notNil: true,
}
n.key = getAddress(&n)
return n
}
func NodeLabelError(err error) NodeLabel {
return NodeLabel{err: err}
}
func (n NodeLabel) GetError() error {
return n.err
}
func (n NodeLabel) isNotNil() bool {
return n.notNil
}
func (n NodeLabel) getKey() string {
return n.key
}
func (n NodeLabel) accept(visitor *CypherRenderer) {
visitor.enter(n)
visitor.leave(n)
}
func (n NodeLabel) enter(renderer *CypherRenderer) {
if n.value == "" {
return
}
renderer.append(NodeLabelStart)
renderer.append(escapeName(n.value))
}
func (n NodeLabel) leave(renderer *CypherRenderer) {
}