-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilters.go
155 lines (124 loc) · 3.11 KB
/
filters.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
package directus
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
type Filter interface {
content() any
String() string
}
type filterOperator struct {
field string
op string
value any
}
func (f filterOperator) content() any {
return map[string]any{
f.field: map[string]any{
f.op: f.value,
},
}
}
func (f filterOperator) String() string {
return fmt.Sprintf("%s %s %v", f.field, f.op, f.value)
}
func Eq(field string, value any) Filter {
return filterOperator{field: field, op: "_eq", value: value}
}
func Neq(field string, value any) Filter {
return filterOperator{field: field, op: "_neq", value: value}
}
func Gt(field string, value any) Filter {
return filterOperator{field: field, op: "_gt", value: value}
}
func Gte(field string, value any) Filter {
return filterOperator{field: field, op: "_gte", value: value}
}
func Lt(field string, value any) Filter {
return filterOperator{field: field, op: "_lt", value: value}
}
func Lte(field string, value any) Filter {
return filterOperator{field: field, op: "_lte", value: value}
}
func Empty(field string) Filter {
return filterOperator{field: field, op: "_empty", value: nil}
}
func NotEmpty(field string) Filter {
return filterOperator{field: field, op: "_nempty", value: nil}
}
func In(field string, values ...any) Filter {
return filterOperator{field: field, op: "_in", value: values}
}
func Between(field string, from, to any) Filter {
return filterOperator{field: field, op: "_between", value: []any{from, to}}
}
func StartsWith(field string, value string) Filter {
return filterOperator{field: field, op: "_starts_with", value: value}
}
type filterLogical struct {
op string
values []Filter
}
func (f filterLogical) content() any {
var values []any
for _, v := range f.values {
values = append(values, v.content())
}
return map[string]any{
f.op: values,
}
}
func (f filterLogical) String() string {
vals := make([]string, len(f.values))
for i, v := range f.values {
vals[i] = v.String()
}
if f.op == "_and" {
return strings.Join(vals, " && ")
}
if f.op == "_or" {
return strings.Join(vals, " || ")
}
// Shouldn't reach here really.
return strings.Join(vals, " "+f.op+" ")
}
func And(filters ...Filter) Filter {
return filterLogical{op: "_and", values: filters}
}
func Or(filters ...Filter) Filter {
return filterLogical{op: "_or", values: filters}
}
type filterRelated struct {
field string
filter Filter
}
func (f filterRelated) content() any {
return map[string]any{
f.field: f.filter.content(),
}
}
func (f filterRelated) String() string {
return fmt.Sprintf("%s.%s", f.field, f.filter.String())
}
func Related(field string, filter Filter) Filter {
return filterRelated{field, filter}
}
type filterEmpty struct{}
func (f filterEmpty) String() string {
return ""
}
func (f filterEmpty) content() any {
return map[string]any{}
}
func Noop() Filter {
return filterEmpty{}
}
func FilterJSON(filter Filter) (string, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(filter.content()); err != nil {
return "", fmt.Errorf("directus: cannot encode filter: %v", err)
}
return buf.String(), nil
}