forked from manhcuongbk56/cypher-go-dsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compound_condition.go
195 lines (170 loc) · 5.23 KB
/
compound_condition.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
package cypher
import (
"errors"
"golang.org/x/xerrors"
)
type CompoundCondition struct {
ConditionContainer
operator Operator
conditions []Condition
conditionType ExpressionType
key string
notNil bool
err error
}
func CompoundConditionCreate(left Condition, operator Operator, right Condition) CompoundCondition {
if left != nil && left.GetError() != nil {
return CompoundConditionError(left.GetError())
}
if operator.GetError() != nil {
return CompoundConditionError(operator.GetError())
}
if right != nil && right.GetError() != nil {
return CompoundConditionError(right.GetError())
}
if left == nil || !left.isNotNil() {
return CompoundConditionError(errors.New("left hand side condition is required"))
}
if !operator.isNotNil() {
return CompoundConditionError(errors.New("operator is required"))
}
isOperatorValid := false
for _, validOperator := range VALID_OPERATORS {
if validOperator == operator {
isOperatorValid = true
}
}
if !isOperatorValid {
return CompoundConditionError(xerrors.Errorf("operator %s is not valid", operator.representation))
}
if right == nil || !right.isNotNil() {
return CompoundConditionError(errors.New("right hand side condition is required"))
}
condition := CompoundCondition{operator: operator, notNil: true}
condition.add(operator, left)
condition.add(operator, right)
condition.injectKey()
condition.ConditionContainer = ConditionWrap(condition)
return condition
}
func CompoundConditionCreate1(operator Operator) CompoundCondition {
condition := CompoundCondition{
operator: operator,
conditions: make([]Condition, 0),
notNil: true,
}
condition.injectKey()
condition.ConditionContainer = ConditionWrap(condition)
return condition
}
func CompoundConditionError(err error) CompoundCondition {
return CompoundCondition{
err: err,
}
}
func (c CompoundCondition) GetError() error {
return c.err
}
func (c CompoundCondition) getConditionType() string {
return "CompoundCondition"
}
func (c CompoundCondition) isNotNil() bool {
return c.notNil
}
func (c CompoundCondition) accept(visitor *CypherRenderer) {
if len(c.conditions) == 0 {
return
}
hasManyConditions := len(c.conditions) > 1
if hasManyConditions {
visitor.enter(c)
}
acceptVisitorWithOperatorForChildCondition(visitor, Operator{}, c.conditions[0])
if hasManyConditions {
for _, condition := range c.conditions[1:] {
var actualOperator Operator
compound, isCompound := condition.(CompoundCondition)
if isCompound {
actualOperator = compound.operator
} else {
actualOperator = c.operator
}
acceptVisitorWithOperatorForChildCondition(visitor, actualOperator, condition)
}
visitor.leave(c)
}
}
func (c CompoundCondition) enter(renderer *CypherRenderer) {
renderer.append("(")
}
func (c CompoundCondition) leave(renderer *CypherRenderer) {
renderer.append(")")
}
func (c CompoundCondition) getKey() string {
return c.key
}
func (c CompoundCondition) GetExpressionType() ExpressionType {
return c.conditionType
}
func (c CompoundCondition) Or(condition Condition) ConditionContainer {
return ConditionWrap(c.add(OR, condition))
}
func (c CompoundCondition) And(condition Condition) ConditionContainer {
return ConditionWrap(c.add(AND, condition))
}
func (c CompoundCondition) Xor(condition Condition) ConditionContainer {
return ConditionWrap(c.add(XOR, condition))
}
func (c *CompoundCondition) injectKey() {
c.key = getAddress(c)
}
var EMPTY_CONDITION = CompoundCondition{
conditions: make([]Condition, 0),
conditionType: EMPTY_CONDITION_EXPRESSION,
notNil: true,
}
var VALID_OPERATORS = []Operator{AND, OR, XOR}
func (c *CompoundCondition) add(chainingOperator Operator, condition Condition) CompoundCondition {
if c.GetExpressionType() == EMPTY_CONDITION_EXPRESSION {
newCompound := CompoundConditionCreate1(chainingOperator)
return newCompound.add(chainingOperator, condition)
}
if compoundCondition, isCompound := condition.(CompoundCondition); isCompound {
if !compoundCondition.hasCondition() {
return *c
}
if c.operator == chainingOperator && chainingOperator == compoundCondition.operator {
if compoundCondition.canBeFlattenedWith(chainingOperator) {
c.conditions = append(c.conditions, compoundCondition.conditions...)
} else {
c.conditions = append(c.conditions, compoundCondition)
}
} else {
inner := CompoundConditionCreate1(chainingOperator)
inner.conditions = append(inner.conditions, compoundCondition)
c.conditions = append(c.conditions, inner)
}
return *c
}
if c.operator == chainingOperator {
c.conditions = append(c.conditions, condition)
return *c
}
return CompoundConditionCreate(*c, chainingOperator, condition)
}
func (c CompoundCondition) hasCondition() bool {
return !(c.GetExpressionType() == EMPTY_CONDITION_EXPRESSION ||
len(c.conditions) == 0)
}
func (c CompoundCondition) canBeFlattenedWith(operatorBefore Operator) bool {
for _, c := range c.conditions {
if compound, isCompound := c.(CompoundCondition); isCompound && compound.operator != operatorBefore {
return false
}
}
return true
}
func acceptVisitorWithOperatorForChildCondition(visitor *CypherRenderer, operator Operator, condition Condition) {
VisitIfNotNull(operator, visitor)
condition.accept(visitor)
}