-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquery.go
121 lines (104 loc) · 2.21 KB
/
query.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
package query
import (
"errors"
"time"
)
var (
ErrQueryNotSupported = errors.New("the requested query option is not supported")
)
type SupportedOptions byte
const (
CanLimitResults SupportedOptions = 0x01
CanSortBy SupportedOptions = 0x01 << 1
CanBucketBy SupportedOptions = 0x01 << 2
CanQueryByCursor SupportedOptions = 0x01 << 3
CanQueryByStartTime SupportedOptions = 0x01 << 4
CanQueryByEndTime SupportedOptions = 0x01 << 5
CanFilterBy SupportedOptions = 0x01 << 6
)
type QueryOptions struct {
Supported SupportedOptions
Start time.Time
End time.Time
Interval Interval
SortBy Ordering
Limit uint64
Cursor Cursor
FilterBy Filter
}
type Option func(*QueryOptions) error
func (qo *QueryOptions) check(cap SupportedOptions) bool {
return qo.Supported&cap != cap
}
func (qo *QueryOptions) Apply(opts ...Option) error {
for _, o := range opts {
err := o(qo)
if err != nil {
return err
}
}
return nil
}
func WithInterval(val Interval) Option {
return func(qo *QueryOptions) error {
if qo.check(CanBucketBy) {
return ErrQueryNotSupported
}
qo.Interval = val
return nil
}
}
func WithFilter(val Filter) Option {
return func(qo *QueryOptions) error {
if qo.check(CanFilterBy) {
return ErrQueryNotSupported
}
qo.FilterBy = val
return nil
}
}
func WithDirection(val Ordering) Option {
return func(qo *QueryOptions) error {
if qo.check(CanSortBy) {
return ErrQueryNotSupported
}
qo.SortBy = val
return nil
}
}
func WithLimit(val uint64) Option {
return func(qo *QueryOptions) error {
if qo.check(CanLimitResults) {
return ErrQueryNotSupported
}
qo.Limit = val
return nil
}
}
func WithCursor(val []byte) Option {
return func(qo *QueryOptions) error {
if qo.check(CanQueryByCursor) {
return ErrQueryNotSupported
}
qo.Cursor = val
return nil
}
}
func WithStartTime(val time.Time) Option {
return func(qo *QueryOptions) error {
if qo.check(CanQueryByStartTime) {
return ErrQueryNotSupported
}
qo.Start = val
return nil
}
}
func WithEndTime(val time.Time) Option {
return func(qo *QueryOptions) error {
if qo.check(CanQueryByEndTime) {
return ErrQueryNotSupported
}
qo.End = val
return nil
}
}