forked from couchbaselabs/gojsonsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.go
257 lines (204 loc) · 4.5 KB
/
expression.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
// Copyright 2018 Couchbase, Inc. All rights reserved.
package gojsonsm
import (
"fmt"
"strings"
)
type VariableID int
func (id VariableID) String() string {
if id == 0 {
return "$doc"
}
return fmt.Sprintf("$%d", id)
}
type Expression interface {
String() string
}
type TrueExpr struct {
}
func (expr TrueExpr) String() string {
return "True"
}
type FalseExpr struct {
}
func (expr FalseExpr) String() string {
return "False"
}
type ValueExpr struct {
Value interface{}
}
func (expr ValueExpr) String() string {
return fmt.Sprintf("%v", expr.Value)
}
type TimeExpr struct {
Time interface{}
}
func (expr TimeExpr) String() string {
return fmt.Sprintf("%v", expr.Time)
}
type RegexExpr struct {
Regex interface{}
}
func (expr RegexExpr) String() string {
return fmt.Sprintf("/%v/", expr.Regex)
}
type PcreExpr struct {
Pcre interface{}
}
func (expr PcreExpr) String() string {
return fmt.Sprintf("/%v/", expr.Pcre)
}
type NotExpr struct {
SubExpr Expression
}
func (expr NotExpr) String() string {
return "NOT " + expr.SubExpr.String()
}
type AndExpr []Expression
func (expr AndExpr) String() string {
if len(expr) == 0 {
return "%%ERROR%%"
} else if len(expr) == 1 {
return expr[0].String()
} else {
value := reindentString(expr[0].String(), " ")
for i := 1; i < len(expr); i++ {
value += "\nAND\n"
value += reindentString(expr[i].String(), " ")
}
return value
}
}
type OrExpr []Expression
func (expr OrExpr) String() string {
if len(expr) == 0 {
return "%%ERROR%%"
} else if len(expr) == 1 {
return expr[0].String()
} else {
value := reindentString(expr[0].String(), " ")
for i := 1; i < len(expr); i++ {
value += "\nOR\n"
value += reindentString(expr[i].String(), " ")
}
return value
}
}
type FieldExpr struct {
Root VariableID
Path []string
}
func (expr FieldExpr) String() string {
rootStr := "$doc"
if expr.Root != 0 {
rootStr = fmt.Sprintf("$%d", expr.Root)
}
if len(expr.Path) > 0 {
return rootStr + "." + strings.Join(expr.Path, ".")
} else {
return rootStr
}
}
type FuncExpr struct {
FuncName string
Params []Expression
}
func (expr FuncExpr) String() string {
rootStr := fmt.Sprintf("func:%s(", expr.FuncName)
for i, param := range expr.Params {
if i > 0 {
rootStr += ","
}
rootStr += param.String()
}
rootStr += ")"
return rootStr
}
type AnyInExpr struct {
VarId VariableID
InExpr Expression
SubExpr Expression
}
func (expr AnyInExpr) String() string {
exprStr := reindentString(expr.SubExpr.String(), " ")
return fmt.Sprintf("any $%d in %s\n%s\nend", expr.VarId, expr.InExpr, exprStr)
}
type EveryInExpr struct {
VarId VariableID
InExpr Expression
SubExpr Expression
}
func (expr EveryInExpr) String() string {
exprStr := reindentString(expr.SubExpr.String(), " ")
return fmt.Sprintf("every $%d in %s\n%s\nend", expr.VarId, expr.InExpr, exprStr)
}
type AnyEveryInExpr struct {
VarId VariableID
InExpr Expression
SubExpr Expression
}
func (expr AnyEveryInExpr) String() string {
exprStr := reindentString(expr.SubExpr.String(), " ")
return fmt.Sprintf("any and every $%d in %s\n%s\nend", expr.VarId, expr.InExpr, exprStr)
}
type ExistsExpr struct {
SubExpr Expression
}
func (expr ExistsExpr) String() string {
return fmt.Sprintf("%s EXISTS", expr.SubExpr)
}
type NotExistsExpr struct {
SubExpr Expression
}
func (expr NotExistsExpr) String() string {
return fmt.Sprintf("%s IS MISSING", expr.SubExpr)
}
type EqualsExpr struct {
Lhs Expression
Rhs Expression
}
func (expr EqualsExpr) String() string {
return fmt.Sprintf("%s = %s", expr.Lhs, expr.Rhs)
}
type NotEqualsExpr struct {
Lhs Expression
Rhs Expression
}
func (expr NotEqualsExpr) String() string {
return fmt.Sprintf("%s != %s", expr.Lhs, expr.Rhs)
}
type LessThanExpr struct {
Lhs Expression
Rhs Expression
}
func (expr LessThanExpr) String() string {
return fmt.Sprintf("%s < %s", expr.Lhs, expr.Rhs)
}
type LessEqualsExpr struct {
Lhs Expression
Rhs Expression
}
func (expr LessEqualsExpr) String() string {
return fmt.Sprintf("%s <= %s", expr.Lhs, expr.Rhs)
}
type GreaterThanExpr struct {
Lhs Expression
Rhs Expression
}
func (expr GreaterThanExpr) String() string {
return fmt.Sprintf("%s > %s", expr.Lhs, expr.Rhs)
}
type GreaterEqualsExpr struct {
Lhs Expression
Rhs Expression
}
func (expr GreaterEqualsExpr) String() string {
return fmt.Sprintf("%s >= %s", expr.Lhs, expr.Rhs)
}
type LikeExpr struct {
Lhs Expression
Rhs Expression
}
func (expr LikeExpr) String() string {
return fmt.Sprintf("%s =~ %s", expr.Lhs, expr.Rhs)
}