-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
66 lines (57 loc) · 1.41 KB
/
option.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
package mongodb
import "github.com/theharshon/gocommon/paging"
type Option interface {
apply(*option)
}
type option struct {
filter interface{}
sorter interface{}
pagination *paging.Paging
page int
limit int
hint interface{}
}
type optionFn func(*option)
func (f optionFn) apply(opt *option) {
f(opt)
}
func WithPaging(pagination *paging.Paging) Option {
return optionFn(func(opt *option) {
opt.page = int(pagination.CurrentPage)
opt.limit = int(pagination.Limit)
opt.pagination = pagination
})
}
// WithFilter - default not filter
//
// # Use bson object to filter document
//
// see more: https://docs.mongodb.com/manual/reference/operator/query/
func WithFilter(filter interface{}) Option {
return optionFn(func(opt *option) {
opt.filter = filter
})
}
// WithSorter - default sort descending by `_id` field
//
// { $sort: { <field1>: <sort order>, <field2>: <sort order> ... }}
//
// $sort takes a document that specifies the field(s) to sort by and the
// respective sort order. <sort order> can have one of the following values:
//
// 1 Sort ascending
//
// -1 Sort descending
//
// { $meta: "textScore" } Sort by the computed textScore metadata in descending
// order
func WithSorter(sorter interface{}) Option {
return optionFn(func(opt *option) {
opt.sorter = sorter
})
}
func WithHint(hint interface{}) Option {
return optionFn(func(opt *option) {
opt.hint = hint
})
}