-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.go
342 lines (277 loc) · 6.94 KB
/
syntax.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package gqlparser
import (
"github.com/karupanerura/runetrie"
)
type Kind string
type ProjectID string
type Property string
type Cursor string
type Syntax interface {
isSyntax()
}
type Key struct {
ProjectID ProjectID
Namespace string
Path []*KeyPath
}
func (*Key) isSyntax() {}
type KeyPath struct {
Kind Kind
ID int64
Name string
}
type Query struct {
Properties []Property
Distinct bool
DistinctOn []Property
Kind Kind
Where Condition
OrderBy []OrderBy
Limit *Limit
Offset *Offset
}
func (*Query) isSyntax() {}
type OrderBy struct {
Descending bool
Property Property
}
func (*OrderBy) isSyntax() {}
type Limit struct {
Position int64
Cursor BindingVariable
}
func (*Limit) isSyntax() {}
type Offset struct {
Position int64
Cursor BindingVariable
}
func (*Offset) isSyntax() {}
type AggregationQuery struct {
Aggregations []Aggregation
Query
}
func (*AggregationQuery) isSyntax() {}
type Aggregation interface {
isAggregation()
}
type CountAggregation struct {
Alias string
}
func (*CountAggregation) isAggregation() {}
func (*CountAggregation) isSyntax() {}
type CountUpToAggregation struct {
Limit int64
Alias string
}
func (*CountUpToAggregation) isAggregation() {}
func (*CountUpToAggregation) isSyntax() {}
type SumAggregation struct {
Property string
Alias string
}
func (*SumAggregation) isAggregation() {}
func (*SumAggregation) isSyntax() {}
type AvgAggregation struct {
Property string
Alias string
}
func (*AvgAggregation) isAggregation() {}
func (*AvgAggregation) isSyntax() {}
type CompoundCondition interface {
Condition
isCompoundCondition()
}
type AndCompoundCondition struct {
Left Condition
Right Condition
}
func (*AndCompoundCondition) isCompoundCondition() {}
func (*AndCompoundCondition) isCondition() {}
func (*AndCompoundCondition) isSyntax() {}
func (c *AndCompoundCondition) Bind(br *BindingResolver) error {
if err := c.Left.Bind(br); err != nil {
return err
}
if err := c.Right.Bind(br); err != nil {
return err
}
return nil
}
func (c *AndCompoundCondition) Normalize() Condition {
return &AndCompoundCondition{
Left: c.Left.Normalize(),
Right: c.Right.Normalize(),
}
}
type OrCompoundCondition struct {
Left Condition
Right Condition
}
func (*OrCompoundCondition) isCompoundCondition() {}
func (*OrCompoundCondition) isCondition() {}
func (*OrCompoundCondition) isSyntax() {}
func (c *OrCompoundCondition) Bind(br *BindingResolver) error {
if err := c.Left.Bind(br); err != nil {
return err
}
if err := c.Right.Bind(br); err != nil {
return err
}
return nil
}
func (c *OrCompoundCondition) Normalize() Condition {
return &OrCompoundCondition{
Left: c.Left.Normalize(),
Right: c.Right.Normalize(),
}
}
type Condition interface {
isCondition()
Bind(*BindingResolver) error
Normalize() Condition
}
type IsNullCondition struct {
Property string
}
func (*IsNullCondition) isCondition() {}
func (*IsNullCondition) isSyntax() {}
func (*IsNullCondition) Bind(br *BindingResolver) error { return nil }
func (c *IsNullCondition) Normalize() Condition {
return &EitherComparatorCondition{
Comparator: EqualsEitherComparator,
Property: c.Property,
Value: nil,
}
}
type ForwardComparatorCondition struct {
Comparator ForwardComparator
Property string
Value any
}
func (*ForwardComparatorCondition) isCondition() {}
func (*ForwardComparatorCondition) isSyntax() {}
func (c *ForwardComparatorCondition) Bind(br *BindingResolver) error {
if bv, ok := c.Value.(BindingVariable); ok {
if v, err := br.Resolve(bv); err != nil {
return err
} else {
c.Value = v
}
}
return nil
}
func (c *ForwardComparatorCondition) Normalize() Condition {
switch c.Comparator {
case ContainsForwardComparator:
return &EitherComparatorCondition{
Comparator: EqualsEitherComparator,
Property: c.Property,
Value: c.Value,
}
default:
return c
}
}
type ForwardComparator string
const (
ContainsForwardComparator ForwardComparator = "CONTAINS"
HasAncestorForwardComparator ForwardComparator = "HAS ANCESTOR"
InForwardComparator ForwardComparator = "IN"
NotInForwardComparator ForwardComparator = "NOT IN"
)
var forwardComparatorTrie = runetrie.NewTrie(
ContainsForwardComparator,
HasAncestorForwardComparator,
InForwardComparator,
NotInForwardComparator,
)
func (c ForwardComparator) Valid() bool {
return forwardComparatorTrie.MatchAny(c)
}
type BackwardComparatorCondition struct {
Comparator BackwardComparator
Property string
Value any
}
func (*BackwardComparatorCondition) isCondition() {}
func (*BackwardComparatorCondition) isSyntax() {}
func (c *BackwardComparatorCondition) Bind(br *BindingResolver) error {
if bv, ok := c.Value.(BindingVariable); ok {
if v, err := br.Resolve(bv); err != nil {
return err
} else {
c.Value = v
}
}
return nil
}
func (c *BackwardComparatorCondition) Normalize() Condition {
switch c.Comparator {
case InBackwardComparator:
return &EitherComparatorCondition{
Comparator: EqualsEitherComparator,
Property: c.Property,
Value: c.Value,
}
case HasDescendantBackwardComparator:
return &ForwardComparatorCondition{
Comparator: HasAncestorForwardComparator,
Property: c.Property,
Value: c.Value,
}
default:
return c
}
}
type BackwardComparator string
const (
InBackwardComparator BackwardComparator = "IN"
HasDescendantBackwardComparator BackwardComparator = "HAS DESCENDANT"
)
var backwardComparatorTrie = runetrie.NewTrie(
InBackwardComparator,
HasDescendantBackwardComparator,
)
func (c BackwardComparator) Valid() bool {
return backwardComparatorTrie.MatchAny(c)
}
type EitherComparatorCondition struct {
Comparator EitherComparator
Property string
Value any
}
func (*EitherComparatorCondition) isCondition() {}
func (*EitherComparatorCondition) isSyntax() {}
func (c *EitherComparatorCondition) Bind(br *BindingResolver) error {
if bv, ok := c.Value.(BindingVariable); ok {
if v, err := br.Resolve(bv); err != nil {
return err
} else {
c.Value = v
}
}
return nil
}
func (c *EitherComparatorCondition) Normalize() Condition {
return c
}
type EitherComparator string
const (
EqualsEitherComparator EitherComparator = "="
NotEqualsEitherComparator EitherComparator = "!="
GreaterThanEitherComparator EitherComparator = ">"
GreaterThanOrEqualsThanEitherComparator EitherComparator = ">="
LesserThanEitherComparator EitherComparator = "<"
LesserThanOrEqualsEitherComparator EitherComparator = "<="
)
var eitherComparatorTrie = runetrie.NewTrie(
EqualsEitherComparator,
NotEqualsEitherComparator,
GreaterThanEitherComparator,
GreaterThanOrEqualsThanEitherComparator,
LesserThanEitherComparator,
LesserThanOrEqualsEitherComparator,
)
func (c EitherComparator) Valid() bool {
return eitherComparatorTrie.MatchAny(c)
}