-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
73 lines (61 loc) · 1.35 KB
/
filter.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
package humus
import (
"errors"
"strings"
)
type logicalOp uint8
const (
logicalOr logicalOp = iota
logicalAnd
logicalNot
)
//Filter represents an object in the query that will be serialized as @filter (function...)
//It is just a wrapper over a function.
type Filter struct {
// op logicalOp
function
// node *Filter
ignoreHeader bool
}
func (f *Filter) canApply(mt modifierSource) bool {
return true
}
func (f *Filter) apply(root *GeneratedQuery, meta FieldMeta, mt modifierSource, sb *strings.Builder) error {
err := f.create(root, sb)
return err
}
func (f *Filter) priority() modifierType {
return modifierFilter
}
func (f *Filter) parenthesis() bool {
return false
}
func (f *Filter) stringify(q *GeneratedQuery, sb *strings.Builder) error {
if f != nil {
err := f.function.create(q, sb)
if err != nil {
return err
}
} else {
return errors.New("missing function in humus filter")
}
return nil
}
func (f *Filter) create(q *GeneratedQuery, sb *strings.Builder) error {
//No nil checks. Done during check.
if !f.ignoreHeader {
sb.WriteString(tokenFilter)
sb.WriteByte('(')
}
err := f.stringify(q, sb)
if !f.ignoreHeader {
sb.WriteByte(')')
}
return err
}
//Function related calls.
//Values is a wrapper to add a list of values to this filter.
func (f *Filter) Values(v ...interface{}) *Filter {
f.function.values(v)
return f
}