forked from manhcuongbk56/cypher-go-dsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_operator.go
268 lines (232 loc) · 6.96 KB
/
list_operator.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
package cypher
import "errors"
type ListOperator struct {
ExpressionContainer
targetExpression Expression
details ListOperatorDetails
key string
notNil bool
err error
}
func listOperatorCreate(targetExpression Expression, optionalStart Expression, dots Dot, optionalEnd Expression) ListOperator {
operator := ListOperator{
targetExpression: targetExpression,
details: ListOperatorDetailsCreate(optionalStart, dots, optionalEnd),
notNil: true,
}
operator.key = getAddress(&operator)
operator.ExpressionContainer = ExpressionWrap(operator)
return operator
}
func ListOperatorError(err error) ListOperator {
return ListOperator{err: err}
}
/**
* Creates a closed range with given boundaries.
*
* @param targetExpression The target expression for the range
* @param start The inclusive start
* @param end The exclusive end
* @return A range literal.
*/
func SubList(targetExpression Expression, start Expression, end Expression) ListOperator {
if targetExpression != nil && targetExpression.GetError() != nil {
return ListOperatorError(targetExpression.GetError())
}
if start != nil && start.GetError() != nil {
return ListOperatorError(start.GetError())
}
if end != nil && end.GetError() != nil {
return ListOperatorError(end.GetError())
}
if targetExpression == nil || !targetExpression.isNotNil() {
return ListOperatorError(errors.New("the range's target expression must not be nil"))
}
if start == nil || !start.isNotNil() {
return ListOperatorError(errors.New("the range's start expression must not be nil"))
}
if end == nil || !end.isNotNil() {
return ListOperatorError(errors.New("the range's end expression must not be nil"))
}
return listOperatorCreate(targetExpression, start, DOTS, end)
}
/**
* Creates an open range starting at {@code start}.
*
* @param targetExpression The target expression for the range
* @param start The inclusive start
* @return A range literal.
*/
func SubListFrom(targetExpression Expression, start Expression) ListOperator {
if targetExpression != nil && targetExpression.GetError() != nil {
return ListOperatorError(targetExpression.GetError())
}
if start != nil && start.GetError() != nil {
return ListOperatorError(start.GetError())
}
if targetExpression == nil || !targetExpression.isNotNil() {
return ListOperatorError(errors.New("the range's target expression must not be nil"))
}
if start == nil || !start.isNotNil() {
return ListOperatorError(errors.New("the range's start expression must not be nil"))
}
return listOperatorCreate(targetExpression, start, DOTS, nil)
}
/**
* Creates an open range starting at {@code start}.
*
* @param targetExpression The target expression for the range
* @param end The exclusive end
* @return A range literal.
*/
func SubListUntil(targetExpression Expression, end Expression) ListOperator {
if targetExpression != nil && targetExpression.GetError() != nil {
return ListOperatorError(targetExpression.GetError())
}
if end != nil && end.GetError() != nil {
return ListOperatorError(end.GetError())
}
if targetExpression == nil || !targetExpression.isNotNil() {
return ListOperatorError(errors.New("the range's target expression must not be nil"))
}
if end == nil || !end.isNotNil() {
return ListOperatorError(errors.New("the range's end expression must not be nil"))
}
return listOperatorCreate(targetExpression, nil, DOTS, end)
}
/**
* Creates a single valued range at {@code index}.
*
* @param targetExpression The target expression for the range
* @param index The index of the range
* @return A range literal.
*/
func ValueAt(targetExpression Expression, index Expression) ListOperator {
if targetExpression != nil && targetExpression.GetError() != nil {
return ListOperatorError(targetExpression.GetError())
}
if index != nil && index.GetError() != nil {
return ListOperatorError(index.GetError())
}
if targetExpression == nil || !targetExpression.isNotNil() {
return ListOperatorError(errors.New("the range's target expression must not be nil"))
}
if index == nil || !index.isNotNil() {
return ListOperatorError(errors.New("the index of the range must not be nil"))
}
return listOperatorCreate(targetExpression, index, Dot{}, nil)
}
func (l ListOperator) GetError() error {
return l.err
}
func (l ListOperator) accept(visitor *CypherRenderer) {
visitor.enter(l)
l.targetExpression.accept(visitor)
l.details.accept(visitor)
visitor.leave(l)
}
func (l ListOperator) enter(renderer *CypherRenderer) {
}
func (l ListOperator) leave(renderer *CypherRenderer) {
}
func (l ListOperator) getKey() string {
return l.key
}
func (l ListOperator) isNotNil() bool {
return l.notNil
}
func (l ListOperator) GetExpressionType() ExpressionType {
return "ListOperator"
}
//Dot struct
type Dot struct {
ExpressionContainer
content string
key string
notNil bool
err error
}
var DOTS = DotCreate("..")
func DotCreate(content string) Dot {
dot := Dot{
content: content,
notNil: true,
}
dot.key = getAddress(&dot)
dot.ExpressionContainer = ExpressionWrap(dot)
return dot
}
func (s Dot) GetError() error {
return s.err
}
func (s Dot) isNotNil() bool {
return s.notNil
}
func (s Dot) getKey() string {
return s.key
}
func (s Dot) GetExpressionType() ExpressionType {
return "Dot"
}
func (s Dot) GetContent() interface{} {
return s.content
}
func (s Dot) AsString() string {
return s.content
}
func (s Dot) accept(visitor *CypherRenderer) {
visitor.enter(s)
visitor.leave(s)
}
func (s Dot) enter(renderer *CypherRenderer) {
renderer.append(s.AsString())
}
func (s Dot) leave(renderer *CypherRenderer) {
}
//ListOperatorDetails struct
type ListOperatorDetails struct {
optionalStart Expression
dots Dot
optionalEnd Expression
key string
err error
notNil bool
}
func ListOperatorDetailsCreate(optionalStart Expression, dots Dot, optionalEnd Expression) ListOperatorDetails {
if optionalStart != nil && optionalStart.GetError() != nil {
return ListOperatorDetails{err: optionalStart.GetError()}
}
if optionalEnd != nil && optionalEnd.GetError() != nil {
return ListOperatorDetails{err: optionalEnd.GetError()}
}
operator := ListOperatorDetails{
optionalStart: optionalStart,
dots: dots,
optionalEnd: optionalEnd,
notNil: true,
}
operator.key = getAddress(&operator)
return operator
}
func (l ListOperatorDetails) GetError() error {
return l.err
}
func (l ListOperatorDetails) accept(visitor *CypherRenderer) {
visitor.enter(l)
VisitIfNotNull(l.optionalStart, visitor)
VisitIfNotNull(l.dots, visitor)
VisitIfNotNull(l.optionalEnd, visitor)
visitor.leave(l)
}
func (l ListOperatorDetails) enter(renderer *CypherRenderer) {
renderer.append("[")
}
func (l ListOperatorDetails) leave(renderer *CypherRenderer) {
renderer.append("]")
}
func (l ListOperatorDetails) getKey() string {
return l.key
}
func (l ListOperatorDetails) isNotNil() bool {
return l.notNil
}