This repository has been archived by the owner on Jun 13, 2022. It is now read-only.
forked from rgburke/grv
/
filter_generator.go
373 lines (308 loc) · 11.6 KB
/
filter_generator.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
package main
import (
"fmt"
"regexp"
"strings"
"time"
log "github.com/Sirupsen/logrus"
glob "github.com/gobwas/glob"
)
// Filter is a function which returns true if the argument matches the filter and false otherwise
type Filter func(interface{}) bool
// FieldDescriptor describes field data for an object type
type FieldDescriptor interface {
FieldTypeDescriptor
FieldValue(inputValue interface{}, fieldName string) interface{}
}
// CreateFilter constructs a filter instance from the provided query and field information
func CreateFilter(query string, fieldDescriptor FieldDescriptor) (filter Filter, errors []error) {
queryParser := NewQueryParser(strings.NewReader(query))
expression, eof, err := queryParser.Parse()
if err != nil {
log.Debugf("Errors encountered when parsing query")
errors = append(errors, err)
return
} else if eof {
return
}
log.Debugf("Received query: %v", expression)
expressionProcessor := NewExpressionProcessor(expression, fieldDescriptor)
if expression, errors = expressionProcessor.Process(); len(errors) > 0 {
log.Debugf("Errors encountered when processing query")
return
}
log.Infof("Creating filter for processed expression: %v", expression)
filterGenerator := NewFilterGenerator(expression, fieldDescriptor)
filter, err = filterGenerator.GenerateFilter()
if err != nil {
log.Debugf("Errors encountered when generating filter from expression")
errors = append(errors, err)
return
}
return
}
// FilterGenerator generates a filter from a parsed and processed expression
type FilterGenerator struct {
expression Expression
fieldDescriptor FieldDescriptor
}
// NewFilterGenerator creates a FilterGenerator instance
func NewFilterGenerator(expression Expression, fieldDescriptor FieldDescriptor) *FilterGenerator {
return &FilterGenerator{
expression: expression,
fieldDescriptor: fieldDescriptor,
}
}
// GenerateFilter generates a filter from the provided expression and field information
func (filterGenerator *FilterGenerator) GenerateFilter() (filter Filter, err error) {
if filterGeneratorExpression, ok := filterGenerator.expression.(filterGeneratorExpression); ok {
filter = filterGeneratorExpression.generateFilter(filterGenerator.fieldDescriptor)
} else {
err = fmt.Errorf("Expected filter generator expression but received expression of type %T", filterGenerator.expression)
}
return
}
type filterGeneratorExpression interface {
generateFilter(FieldDescriptor) Filter
}
func (parenExpression *ParenExpression) generateFilter(fieldDescriptor FieldDescriptor) Filter {
filterGeneratorExpression := parenExpression.expression.(filterGeneratorExpression)
return filterGeneratorExpression.generateFilter(fieldDescriptor)
}
func (unaryExpression *UnaryExpression) generateFilter(fieldDescriptor FieldDescriptor) Filter {
filterGeneratorExpression := unaryExpression.expression.(filterGeneratorExpression)
filter := filterGeneratorExpression.generateFilter(fieldDescriptor)
switch unaryExpression.operator.operator.tokenType {
case QtkNot:
return func(inputValue interface{}) bool {
return !filter(inputValue)
}
}
panic(fmt.Sprintf("Encountered invalid operator: %v", unaryExpression.operator.operator.value))
}
func (binaryExpression *BinaryExpression) generateFilter(fieldDescriptor FieldDescriptor) Filter {
if !binaryExpression.IsComparison() {
lhs := binaryExpression.lhs.(filterGeneratorExpression).generateFilter(fieldDescriptor)
rhs := binaryExpression.rhs.(filterGeneratorExpression).generateFilter(fieldDescriptor)
switch binaryExpression.operator.operator.tokenType {
case QtkAnd:
return func(inputValue interface{}) bool {
return lhs(inputValue) && rhs(inputValue)
}
case QtkOr:
return func(inputValue interface{}) bool {
return lhs(inputValue) || rhs(inputValue)
}
default:
panic(fmt.Sprintf("Encountered invalid operator: %v", binaryExpression.operator.operator.value))
}
}
lhs := binaryExpression.lhs.(valueType)
rhs := binaryExpression.rhs.(valueType)
var comparator fieldComparator
switch binaryExpression.operator.operator.tokenType {
case QtkCmpGlob:
comparator = globComparator
case QtkCmpRegexp:
comparator = regexpComparator
default:
comparator = basicFieldComparators[binaryExpression.operator.operator.tokenType][lhs.FieldType(fieldDescriptor)]
}
return func(inputValue interface{}) bool {
return comparator(lhs.getValue(inputValue, fieldDescriptor), rhs.getValue(inputValue, fieldDescriptor))
}
}
type valueType interface {
TypeDescriptor
getValue(inputValue interface{}, fieldDescriptor FieldDescriptor) interface{}
}
func (stringLiteral *StringLiteral) getValue(inputValue interface{}, fieldDescriptor FieldDescriptor) interface{} {
return stringLiteral.value.value
}
func (numberLiteral *NumberLiteral) getValue(inputValue interface{}, fieldDescriptor FieldDescriptor) interface{} {
return numberLiteral.number
}
func (dateLiteral *DateLiteral) getValue(inputValue interface{}, fieldDescriptor FieldDescriptor) interface{} {
return dateLiteral.dateTime
}
func (globLiteral *GlobLiteral) getValue(inputValue interface{}, fieldDescriptor FieldDescriptor) interface{} {
return globLiteral.glob
}
func (regexLiteral *RegexLiteral) getValue(inputValue interface{}, fieldDescriptor FieldDescriptor) interface{} {
return regexLiteral.regex
}
func (identifier *Identifier) getValue(inputValue interface{}, fieldDescriptor FieldDescriptor) interface{} {
return fieldDescriptor.FieldValue(inputValue, identifier.identifier.value)
}
type fieldComparator func(interface{}, interface{}) bool
var basicFieldComparators = map[QueryTokenType]map[FieldType]fieldComparator{
QtkCmpEq: {
FtNumber: func(value1 interface{}, value2 interface{}) bool {
num1 := value1.(float64)
num2 := value2.(float64)
return num1 == num2
},
FtString: func(value1 interface{}, value2 interface{}) bool {
str1 := value1.(string)
str2 := value2.(string)
return str1 == str2
},
FtDate: func(value1 interface{}, value2 interface{}) bool {
time1 := value1.(time.Time)
time2 := value2.(time.Time)
return time1.Equal(time2)
},
},
QtkCmpNe: {
FtNumber: func(value1 interface{}, value2 interface{}) bool {
num1 := value1.(float64)
num2 := value2.(float64)
return num1 != num2
},
FtString: func(value1 interface{}, value2 interface{}) bool {
str1 := value1.(string)
str2 := value2.(string)
return str1 != str2
},
FtDate: func(value1 interface{}, value2 interface{}) bool {
time1 := value1.(time.Time)
time2 := value2.(time.Time)
return !time1.Equal(time2)
},
},
QtkCmpGt: {
FtNumber: func(value1 interface{}, value2 interface{}) bool {
num1 := value1.(float64)
num2 := value2.(float64)
return num1 > num2
},
FtString: func(value1 interface{}, value2 interface{}) bool {
str1 := value1.(string)
str2 := value2.(string)
return str1 > str2
},
FtDate: func(value1 interface{}, value2 interface{}) bool {
time1 := value1.(time.Time)
time2 := value2.(time.Time)
return time1.After(time2)
},
},
QtkCmpGe: {
FtNumber: func(value1 interface{}, value2 interface{}) bool {
num1 := value1.(float64)
num2 := value2.(float64)
return num1 >= num2
},
FtString: func(value1 interface{}, value2 interface{}) bool {
str1 := value1.(string)
str2 := value2.(string)
return str1 >= str2
},
FtDate: func(value1 interface{}, value2 interface{}) bool {
time1 := value1.(time.Time)
time2 := value2.(time.Time)
return time1.After(time2) || time1.Equal(time2)
},
},
QtkCmpLt: {
FtNumber: func(value1 interface{}, value2 interface{}) bool {
num1 := value1.(float64)
num2 := value2.(float64)
return num1 < num2
},
FtString: func(value1 interface{}, value2 interface{}) bool {
str1 := value1.(string)
str2 := value2.(string)
return str1 < str2
},
FtDate: func(value1 interface{}, value2 interface{}) bool {
time1 := value1.(time.Time)
time2 := value2.(time.Time)
return time1.Before(time2)
},
},
QtkCmpLe: {
FtNumber: func(value1 interface{}, value2 interface{}) bool {
num1 := value1.(float64)
num2 := value2.(float64)
return num1 <= num2
},
FtString: func(value1 interface{}, value2 interface{}) bool {
str1 := value1.(string)
str2 := value2.(string)
return str1 <= str2
},
FtDate: func(value1 interface{}, value2 interface{}) bool {
time1 := value1.(time.Time)
time2 := value2.(time.Time)
return time1.Before(time2) || time1.Equal(time2)
},
},
}
func globComparator(value1 interface{}, value2 interface{}) bool {
input := value1.(string)
glob := value2.(glob.Glob)
return glob.Match(input)
}
func regexpComparator(value1 interface{}, value2 interface{}) bool {
input := value1.(string)
regex := value2.(*regexp.Regexp)
return regex.MatchString(input)
}
// GenerateFilterQueryLanguageHelpSections generates help documentation for the Filter Query Language
func GenerateFilterQueryLanguageHelpSections(config Config) (helpSections []*HelpSection) {
description := []HelpSectionText{
{text: "GRV has a built in query language which can be used to filter the content of the Ref and Commit views."},
{text: "All queries resolve to boolean values which are tested against each item listed in the view."},
{text: "A query is composed of at least one comparison:"},
{},
{text: "field CMP value", themeComponentID: CmpHelpViewSectionCodeBlock},
{},
{text: "CMP can be any of the following comparison operators, which are case-insensitive:"},
{},
{text: "=, !=, >, >=, <, <=, GLOB, REGEXP", themeComponentID: CmpHelpViewSectionCodeBlock},
{},
{text: "Value is one of the following types:"},
{},
{text: "string (e.g. \"test\")", themeComponentID: CmpHelpViewSectionCodeBlock},
{text: "number (e.g. 123 or 123.0)", themeComponentID: CmpHelpViewSectionCodeBlock},
{text: "date (e.g. \"2017-09-05 10:05:25\" or \"2017-09-05\")", themeComponentID: CmpHelpViewSectionCodeBlock},
{},
{text: "Field is specific to the view that is being filtered."},
{text: "For example, to filter commits to those whose commit messages start with \"Bug Fix:\":"},
{},
{text: "summary GLOB \"Bug Fix:*\"", themeComponentID: CmpHelpViewSectionCodeBlock},
{},
{text: "Or equivalently:"},
{},
{text: "summary REGEXP \"^Bug Fix:.*\"", themeComponentID: CmpHelpViewSectionCodeBlock},
{},
{text: "For more inforation about the supported GLOB syntax see: https://github.com/gobwas/glob"},
{},
{text: "For more information about the supported regex syntax see: https://golang.org/s/re2syntax"},
{},
{text: "Comparisons can be composed together using the following logical operators, which are case-insensitive:"},
{},
{text: "AND, OR, NOT", themeComponentID: CmpHelpViewSectionCodeBlock},
{},
{text: "For example, to filter commits to those authored by John Smith or Jane Roe in September 2017, ignoring merge commits:"},
{},
{text: `authordate >= "2017-09-01" AND authordate < "2017-10-01" AND (authorname = "John Smith" OR authorname = "Jane Roe") AND parentcount < 2`, themeComponentID: CmpHelpViewSectionCodeBlock},
{},
{text: "As shown above, expressions can be grouped using parentheses."},
{},
{text: "The list of (case-insensitive) fields that can be used in the Commit View is:"},
}
helpSections = append(helpSections, &HelpSection{
title: HelpSectionText{text: "Filter Query Language"},
description: description,
})
helpSections = append(helpSections, GenerateCommitFieldHelpSection(config))
helpSections = append(helpSections, &HelpSection{
description: []HelpSectionText{
{text: "The list of (case-insensitive) fields that can be used in the Ref View is:"},
},
})
helpSections = append(helpSections, GenerateRefFieldHelpSection(config))
return
}