-
Notifications
You must be signed in to change notification settings - Fork 302
/
pagination.go
119 lines (105 loc) · 3.76 KB
/
pagination.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
// Copyright © 2022 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package store
import (
"context"
"fmt"
"strings"
)
type paginationOptionsKeyType struct{}
var paginationOptionsKey paginationOptionsKeyType
// PaginationOptions stores the pagination options that are propagated in the context.
type PaginationOptions struct {
limit uint32
offset uint32
total *uint64
}
// WithPagination instructs the store to paginate the results, and set the total
// number of results into total.
func WithPagination(ctx context.Context, limit, page uint32, total *uint64) context.Context {
if page == 0 {
page = 1
}
return context.WithValue(ctx, paginationOptionsKey, PaginationOptions{
limit: limit,
offset: (page - 1) * limit,
total: total,
})
}
// SetTotal sets the total number of results into the destination set by
// SetTotalCount if not already set.
func SetTotal(ctx context.Context, total uint64) {
if opts, ok := ctx.Value(paginationOptionsKey).(PaginationOptions); ok && opts.total != nil && *opts.total == 0 {
*opts.total = total
}
}
// LimitAndOffsetFromContext gets the limit and offset from the context.
func LimitAndOffsetFromContext(ctx context.Context) (limit, offset uint32) {
if opts, ok := ctx.Value(paginationOptionsKey).(PaginationOptions); ok {
return opts.limit, opts.offset
}
return 0, 0
}
// WithOrder instructs the store to sort the results by the given field.
// If the field is prefixed with a minus, the order is reversed.
func WithOrder(ctx context.Context, spec string) context.Context {
if spec == "" {
return ctx
}
field := spec
direction := "ASC"
if strings.HasPrefix(spec, "-") {
field = strings.TrimPrefix(spec, "-")
direction = "DESC"
}
return context.WithValue(ctx, orderOptionsKey, OrderOptions{
Field: field,
Direction: direction,
})
}
type orderOptionsKeyType struct{}
var orderOptionsKey orderOptionsKeyType
// OrderOptions stores the ordering options that are propagated in the context.
type OrderOptions struct {
Field string
Direction string
}
// OrderOptionsFromContext returns the ordering options for the query.
func OrderOptionsFromContext(ctx context.Context) OrderOptions {
if opts, ok := ctx.Value(orderOptionsKey).(OrderOptions); ok {
return opts
}
return OrderOptions{}
}
// OrderFromContext returns the ordering string (field and direction) for the query.
// If the context contains ordering options, those are used. Otherwise, the default
// field and order are used.
func OrderFromContext(ctx context.Context, table, defaultTableField, defaultDirection string) string {
if opts, ok := ctx.Value(orderOptionsKey).(OrderOptions); ok && opts.Field != "" {
direction := opts.Direction
if direction == "" {
direction = "ASC"
}
if (table == "organizations" && opts.Field == "organization_id") || (table == "users" && opts.Field == "user_id") {
table = "accounts"
opts.Field = "uid"
}
tableField := fmt.Sprintf(`"%s"."%s"`, table, opts.Field)
if tableField != defaultTableField && opts.Field != defaultTableField {
return fmt.Sprintf(`%s %s, %s %s`, tableField, direction, defaultTableField, direction)
}
return fmt.Sprintf(`%s %s`, tableField, direction)
}
return fmt.Sprintf("%s %s", defaultTableField, defaultDirection)
}