-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
88 lines (75 loc) · 2.32 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package tpuf
import (
"encoding/json"
)
// Supported operators for filtering.
// See https://turbopuffer.com/docs/query#full-list-of-operators
type Operator string
const (
OpEq Operator = "Eq"
OpNotEq Operator = "NotEq"
OpIn Operator = "In"
OpNotIn Operator = "NotIn"
OpLt Operator = "Lt"
OpLte Operator = "Lte"
OpGt Operator = "Gt"
OpGte Operator = "Gte"
OpGlob Operator = "Glob"
OpNotGlob Operator = "NotGlob"
OpIGlob Operator = "IGlob"
OpNotIGlob Operator = "NotIGlob"
)
// Filter represents a Turbopuffer filter.
// This may be a simple filter, such as a single attribute with an operator and value,
// or a more complex filter, such as an "And" or "Or" filter with multiple sub-filters.
// See https://turbopuffer.com/docs/query#filtering-parameters
type Filter interface {
tpuf_SerializeFilter() interface{}
json.Marshaler
}
// BaseFilter represents a simple filter with an attribute, operator, and value.
type BaseFilter struct {
Attribute string
Operator Operator
Value interface{}
}
func (bf *BaseFilter) tpuf_SerializeFilter() interface{} {
return []interface{}{bf.Attribute, bf.Operator, bf.Value}
}
func (f *BaseFilter) MarshalJSON() ([]byte, error) {
return json.Marshal(f.tpuf_SerializeFilter())
}
// AndFilter represents a filter that requires all of its sub-filters to be true.
type AndFilter struct {
Filters []Filter
}
func (af *AndFilter) tpuf_SerializeFilter() interface{} {
serialized := make([]interface{}, 2)
serialized[0] = "And"
subFilters := make([]interface{}, len(af.Filters))
for i, filter := range af.Filters {
subFilters[i] = filter.tpuf_SerializeFilter()
}
serialized[1] = subFilters
return serialized
}
func (f *AndFilter) MarshalJSON() ([]byte, error) {
return json.Marshal(f.tpuf_SerializeFilter())
}
// OrFilter represents a filter that requires at least one of its sub-filters to be true.
type OrFilter struct {
Filters []Filter
}
func (of *OrFilter) tpuf_SerializeFilter() interface{} {
serialized := make([]interface{}, 2)
serialized[0] = "Or"
subFilters := make([]interface{}, len(of.Filters))
for i, filter := range of.Filters {
subFilters[i] = filter.tpuf_SerializeFilter()
}
serialized[1] = subFilters
return serialized
}
func (f *OrFilter) MarshalJSON() ([]byte, error) {
return json.Marshal(f.tpuf_SerializeFilter())
}