-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifier.go
383 lines (338 loc) · 8.31 KB
/
modifier.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package humus
import (
"errors"
"sort"
"strings"
)
type modifierSource int
/*
Begin simple mods
*/
type mapElement struct {
m modifierList
f facet
g groupBy
q *GeneratedQuery
}
type facetCreator mapElement
type groupCreator mapElement
type modifierCreator mapElement
const (
modifierField modifierSource = iota
modifierFunction
)
/*
Operation is a closure callback given a mod. Any operations called on this
applies the given operation at the path.
*/
type Operation func(m Mod)
/*
Mod is the core for applying operations at certain predicate levels.
There are two kind of 'mods', those which exist at root/edge level and those
at field level. Paginate, Filter, Sort exists at root/edge level with the remaining
existing at field level. This is an important distinction
For Paginate, a path of "" applies the pagination at the top level (root) and given a single
predicate P it applies it on the edge.
For Variable, a path of "" applies the variable at the root field level, at the top level node.
*/
type Mod interface {
/*
Paginate creates a pagination at this level given the pagination type
and the value.
*/
Paginate(t PaginationType, value int) bool
/*
Filter creates a filter at this level given a function type and a list of variables with the
same syntax as a function.
*/
Filter(t FunctionType, variables ...interface{}) bool
/*
Sort applies a sorting at this level.
*/
Sort(t OrderType, p Predicate) bool
/*
Aggregate sets an aggregation at this level.
*/
Aggregate(t AggregateType, v string, alias string) bool
/*
Count sets a count variable at this level, e.g.
result : count(uid) given a "uid" as predicate and
"result" as alias.
*/
Count(p Predicate, alias string) bool
/*
Variable sets a variable at this level.
It either generates a value variable or an alias variable.
If name is omitted so is the prefix for the variable. This
can be useful for setting facet variables where name is omitted.
*/
Variable(name string, value string, isAlias bool) bool
}
type modifierType uint8
const (
modifierVariable modifierType = 1 << iota
modifierAggregate
//All above are field generating modifiers.
modifierFilter
modifierPagination
modifierOrder
modifierGroupBy
modifierFacet
)
type modifier interface {
canApply(mt modifierSource) bool
//While io.Writer is more generic, the utility of
//multiple different write methods is unbeatable here.
apply(root *GeneratedQuery, meta FieldMeta, mt modifierSource, sb *strings.Builder) error
priority() modifierType
parenthesis() bool
}
type modifierList []modifier
func (m modifierList) sort() {
if len(m) > 1 {
if len(m) == 2 {
if m[0].priority() > m[1].priority() {
m.Swap(0, 1)
}
} else {
sort.Stable(m)
}
}
}
func (m modifierList) runNormal(q *GeneratedQuery, meta FieldMeta, where modifierSource, sb *strings.Builder) error {
var curType modifierType
for k, v := range m {
newType := v.priority()
if newType <= modifierAggregate {
continue
}
if v.canApply(where) {
if newType > curType && v.parenthesis() {
sb.WriteByte('(')
}
err := v.apply(q, meta, where, sb)
if k != len(m)-1 && v.parenthesis() {
if p := m[k+1].priority(); p == newType {
sb.WriteByte(',')
} else if p != newType {
sb.WriteByte(')')
}
}
if err != nil {
return err
}
}
if k == len(m)-1 && v.parenthesis() {
sb.WriteByte(')')
}
curType = newType
}
return nil
}
func (m modifierList) runTopLevel(q *GeneratedQuery, meta FieldMeta, where modifierSource, sb *strings.Builder) error {
for _, v := range m {
if v.priority() > modifierFilter && v.canApply(modifierFunction) {
sb.WriteByte(',')
err := v.apply(q, 0, modifierFunction, sb)
if err != nil {
return err
}
}
}
sb.WriteByte(')')
for _, v := range m {
if v.priority() == modifierFilter && v.canApply(modifierFunction) {
err := v.apply(q, 0, modifierFunction, sb)
if err != nil {
return err
}
}
}
return nil
}
func (m modifierList) runVariables(q *GeneratedQuery, meta FieldMeta, where modifierSource, sb *strings.Builder) error {
for _, v := range m {
if v.priority() > modifierAggregate {
break
}
err := v.apply(q, meta, where, sb)
if err != nil {
return err
}
}
return nil
}
//assume sorted
func (m modifierList) runFacet(q *GeneratedQuery, meta FieldMeta, sb *strings.Builder) error {
sb.WriteString("@facets(")
for k, v := range m {
err := v.apply(q, meta, modifierField, sb)
if k != len(m)-1 {
sb.WriteByte(',')
}
if err != nil {
return err
}
}
sb.WriteByte(')')
return nil
}
func (m *modifierCreator) Paginate(t PaginationType, value int) bool {
m.m = append(m.m, pagination{Type: t, Value: value})
return true
}
func (m *modifierCreator) Variable(name string, value string, isAlias bool) bool {
m.m = append(m.m, variable{
name: name,
value: value,
alias: isAlias,
})
return true
}
func (m *modifierCreator) Filter(t FunctionType, variables ...interface{}) bool {
var filter Filter
filter.typ = t
filter.variables = make([]graphVariable, len(variables))
for k, v := range variables {
val, typ := processInterface(v)
filter.variables[k] = graphVariable{
Value: val,
Type: typ,
}
}
filter.mapVariables(m.q)
m.m = append(m.m, &filter)
return true
}
func (m *modifierCreator) Sort(t OrderType, p Predicate) bool {
m.m = append(m.m, Ordering{
Type: t,
Predicate: p,
})
return true
}
func (m *modifierCreator) Aggregate(t AggregateType, v string, alias string) bool {
m.m = append(m.m, aggregateValues{
Type: t,
Alias: alias,
Variable: v,
})
return true
}
func (m *modifierCreator) Count(p Predicate, alias string) bool {
m.m = append(m.m, variable{
name: alias,
value: "count(" + string(p) + ")",
alias: true,
})
return true
}
func (m modifierList) Len() int {
return len(m)
}
func (m modifierList) hasModifier(mt modifierType) bool {
for _, v := range m {
if v.priority() == mt {
return true
}
}
return false
}
func (m modifierList) Less(i, j int) bool {
return m[i].priority() < m[j].priority()
}
func (m modifierList) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
//aggregateValues represents a modifier with a type(sum),
//an alias for changing json key as well as what variable or predicate it acts on.
type aggregateValues struct {
Type AggregateType
Alias string
Variable string
}
func (a aggregateValues) canApply(mt modifierSource) bool {
return true
}
func (a aggregateValues) apply(root *GeneratedQuery, meta FieldMeta, mt modifierSource, sb *strings.Builder) error {
sb.WriteByte(' ')
if a.Variable == "" {
return errors.New("missing predicate in aggregateValues")
}
if a.Alias != "" {
sb.WriteString(a.Alias)
sb.WriteString(" : ")
}
sb.WriteString(string(a.Type))
isCount := a.Type == "count"
sb.WriteByte('(')
if !isCount {
sb.WriteString("val(")
}
sb.WriteString(a.Variable)
if !isCount {
sb.WriteByte(')')
}
sb.WriteByte(')')
sb.WriteByte(' ')
return nil
}
func (a aggregateValues) priority() modifierType {
return modifierAggregate
}
func (a aggregateValues) parenthesis() bool {
return true
}
type groupBy struct {
m modifierList
p Predicate
}
func (g *groupCreator) Paginate(t PaginationType, value int) bool {
return false
}
func (g *groupCreator) Variable(name string, value string, isAlias bool) bool {
g.g.m = append(g.g.m, variable{
name: name,
value: value,
alias: isAlias,
})
return true
}
func (g *groupCreator) Filter(t FunctionType, variables ...interface{}) bool {
return false
}
func (g *groupCreator) Sort(t OrderType, p Predicate) bool {
return false
}
func (g *groupCreator) Aggregate(t AggregateType, v string, alias string) bool {
g.g.m = append(g.g.m, aggregateValues{
Type: t,
Alias: alias,
Variable: v,
})
return true
}
func (g *groupCreator) Count(p Predicate, alias string) bool {
return false
}
func (g groupBy) canApply(mt modifierSource) bool {
return mt == modifierField
}
func (g groupBy) apply(root *GeneratedQuery, meta FieldMeta, mt modifierSource, sb *strings.Builder) error {
if g.p == "" {
return errors.New("missing predicate type in groupBy")
}
sb.WriteString("@groupby(")
sb.WriteString(string(g.p))
sb.WriteByte(')')
sb.WriteByte('{')
g.m.runVariables(root, 0, mt, sb)
sb.WriteByte('}')
return nil
}
func (g groupBy) priority() modifierType {
return modifierGroupBy
}
func (g groupBy) parenthesis() bool {
return false
}