forked from manhcuongbk56/cypher-go-dsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation.go
155 lines (141 loc) · 3.78 KB
/
operation.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
package cypher
import (
"errors"
"golang.org/x/xerrors"
)
type Operation struct {
ExpressionContainer
left Expression
operator Operator
right Visitable
key string
notNil bool
err error
}
func OperationCreate(left Expression, operator Operator, right Expression) Operation {
if left != nil && left.GetError() != nil {
return OperationError(left.GetError())
}
if operator.GetError() != nil {
return OperationError(operator.GetError())
}
if right != nil && right.GetError() != nil {
return OperationError(right.GetError())
}
if left == nil || !left.isNotNil() {
return OperationError(errors.New("operation: left can not be nil"))
}
if !operator.isNotNil() {
return OperationError(errors.New("operation: operator can not be nil"))
}
if right == nil || !right.isNotNil() {
return OperationError(errors.New("operation: right can not be nil"))
}
o := Operation{
left: left,
operator: operator,
right: right,
notNil: true,
}
o.key = getAddress(&o)
o.ExpressionContainer = ExpressionWrap(o)
return o
}
func OperationCreate1(left Expression, operator Operator, right NodeLabel) Operation {
if left != nil && left.GetError() != nil {
return OperationError(left.GetError())
}
if operator.GetError() != nil {
return OperationError(operator.GetError())
}
if right.GetError() != nil {
return OperationError(right.GetError())
}
if left == nil || left.isNotNil() {
return OperationError(errors.New("left can not be nil"))
}
if operator.isNotNil() {
return OperationError(errors.New("operator can not be nil"))
}
if right.isNotNil() {
return OperationError(errors.New("right can not be nil"))
}
o := Operation{
left: left,
operator: operator,
right: right,
notNil: true,
}
o.key = getAddress(&o)
o.ExpressionContainer = ExpressionWrap(o)
return o
}
func OperationCreate2(op1 Node, operator Operator, nodeLabels ...string) Operation {
if op1.GetError() != nil {
return OperationError(op1.GetError())
}
if operator.GetError() != nil {
return OperationError(operator.GetError())
}
if !op1.isNotNil() {
return OperationError(errors.New("left can not be nil"))
}
if !operator.isNotNil() {
return OperationError(errors.New("operator can not be nil"))
}
if !(operator.representation == SET_LABEL.representation || operator.representation == REMOVE_LABEL.representation) {
return OperationError(xerrors.Errorf("operator %s can not use to modify label", operator.representation))
}
if nodeLabels == nil || len(nodeLabels) == 0 {
return OperationError(errors.New("labels can not be nil or empty"))
}
labels := make([]NodeLabel, 0)
for _, nodeLabel := range nodeLabels {
labels = append(labels, NodeLabelCreate(nodeLabel))
}
o := Operation{
left: op1.GetRequiredSymbolicName(),
operator: operator,
right: NodeLabelsCreate(labels),
notNil: true,
}
o.key = getAddress(&o)
o.ExpressionContainer = ExpressionWrap(o)
return o
}
func OperationError(err error) Operation {
return Operation{err: err}
}
func (o Operation) GetError() error {
return o.err
}
func (o Operation) accept(visitor *CypherRenderer) {
visitor.enter(o)
NameOrExpression(o.left).accept(visitor)
o.operator.accept(visitor)
o.right.accept(visitor)
visitor.leave(o)
}
func (o Operation) enter(renderer *CypherRenderer) {
if o.needsGrouping() {
renderer.append("(")
}
}
func (o Operation) leave(renderer *CypherRenderer) {
if o.needsGrouping() {
renderer.append(")")
}
}
func (o Operation) getKey() string {
return o.key
}
func (o Operation) isNotNil() bool {
return o.notNil
}
func (o Operation) GetExpressionType() ExpressionType {
return "Operation"
}
func (o Operation) needsGrouping() bool {
return (o.operator.operatorType != PROPERTY && o.operator.operatorType != LABEL) &&
(o.operator != EXPONENTIATION && o.operator != PIPE)
}