forked from manhcuongbk56/cypher-go-dsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparison.go
133 lines (119 loc) · 3.07 KB
/
comparison.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
package cypher
import (
"errors"
)
type Comparison struct {
ConditionContainer
left Expression
operator Operator
right Expression
key string
notNil bool
err error
}
func ComparisonCreate(left Expression, operator Operator, right Expression) Comparison {
if left != nil && left.GetError() != nil {
return ComparisonError(left.GetError())
}
if operator.GetError() != nil {
return ComparisonError(operator.GetError())
}
if right != nil && right.GetError() != nil {
return ComparisonError(right.GetError())
}
if !operator.isNotNil() {
return ComparisonError(errors.New("operator must be not nil"))
}
if left == nil || !left.isNotNil() {
return ComparisonError(errors.New("left expression must be not nil"))
}
if right == nil || !left.isNotNil() {
return ComparisonError(errors.New("right expression must be not nil"))
}
comparison := Comparison{
left: nestedIfCondition(left),
operator: operator,
right: nestedIfCondition(right),
notNil: true,
}
comparison.key = getAddress(&comparison)
comparison.ConditionContainer = ConditionWrap(comparison)
return comparison
}
func ComparisonCreate1(operator Operator, expression Expression) Comparison {
if operator.GetError() != nil {
return ComparisonError(operator.GetError())
}
if expression != nil && expression.GetError() != nil {
return ComparisonError(expression.GetError())
}
if !operator.isUnary() {
return ComparisonError(errors.New("comparison: operator must be unary"))
}
if expression == nil || !expression.isNotNil() {
return ComparisonError(errors.New("comparison: expression must be not nil"))
}
var comparison Comparison
switch operator.operatorType {
case PREFIX:
comparison = Comparison{
left: nil,
operator: operator,
right: nestedIfCondition(expression),
}
case POSTFIX:
comparison = Comparison{
left: nestedIfCondition(expression),
operator: operator,
right: nil,
}
default:
return Comparison{}
}
comparison.key = getAddress(&comparison)
comparison.notNil = true
comparison.ConditionContainer = ConditionWrap(comparison)
return comparison
}
func ComparisonError(err error) Comparison {
return Comparison{
err: err,
}
}
func (c Comparison) GetError() error {
return c.err
}
func (c Comparison) isNotNil() bool {
return c.notNil
}
func (c Comparison) accept(visitor *CypherRenderer) {
visitor.enter(c)
if c.left != nil && c.left.isNotNil() {
NameOrExpression(c.left).accept(visitor)
}
c.operator.accept(visitor)
if c.right != nil && c.right.isNotNil() {
NameOrExpression(c.right).accept(visitor)
}
visitor.leave(c)
}
func (c Comparison) enter(renderer *CypherRenderer) {
}
func (c Comparison) leave(renderer *CypherRenderer) {
}
func (c Comparison) getKey() string {
return c.key
}
func (c Comparison) GetExpressionType() ExpressionType {
return CONDITION
}
func (c Comparison) getConditionType() string {
return "Comparison"
}
func nestedIfCondition(expression Expression) Expression {
_, isCondition := expression.(Condition)
if isCondition {
return NestedExpressionCreate(expression)
}
return expression
}