-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
116 lines (96 loc) · 2.44 KB
/
options.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
package sqlstore
import (
"github.com/SKF/go-eventsource/v2/eventsource"
)
type column string
const (
columnAggregateID column = "aggregate_id"
columnSequenceID column = "sequence_id"
columnCreatedAt column = "created_at"
columnUserID column = "user_id"
columnType column = "type"
columnData column = "data"
)
type whereOperator string
const (
whereOperatorEquals = "="
whereOperatorGreaterThan = ">"
)
type whereOpt struct {
value interface{}
operator whereOperator
}
type options struct {
limit *int
offset *int
descending bool
where map[column]whereOpt
}
// WithLimit will limit the result.
func WithLimit(limit int) eventsource.QueryOption {
return func(i interface{}) {
if o, ok := i.(*options); ok {
o.limit = &limit
}
}
}
// WithOffset will offset the result.
func WithOffset(offset int) eventsource.QueryOption {
return func(i interface{}) {
if o, ok := i.(*options); ok {
o.offset = &offset
}
}
}
// WithDescending will set the sorting order to descending.
func WithDescending() eventsource.QueryOption {
return func(i interface{}) {
if o, ok := i.(*options); ok {
o.descending = true
}
}
}
// WithAscending will set the sorting order to ascending.
func WithAscending() eventsource.QueryOption {
return func(i interface{}) {
if o, ok := i.(*options); ok {
o.descending = false
}
}
}
func where(operator whereOperator, key column, value interface{}) eventsource.QueryOption {
return func(i interface{}) {
if o, ok := i.(*options); ok {
o.where[key] = whereOpt{
value: value,
operator: operator,
}
}
}
}
func equals(key column, value interface{}) eventsource.QueryOption {
return where(whereOperatorEquals, key, value)
}
func greaterThan(key column, value interface{}) eventsource.QueryOption {
return where(whereOperatorGreaterThan, key, value)
}
func BySequenceID(value string) eventsource.QueryOption {
return greaterThan(columnSequenceID, value)
}
func ByTimestamp(value int64) eventsource.QueryOption {
return greaterThan(columnCreatedAt, value)
}
func ByType(value string) eventsource.QueryOption {
return equals(columnType, value)
}
// evaluate a list of options by extending the default options.
func evaluateQueryOptions(queryOpts []eventsource.QueryOption) *options {
opts := &options{ // nolint:exhaustivestruct
descending: false,
where: make(map[column]whereOpt),
}
for _, opt := range queryOpts {
opt(opts)
}
return opts
}