-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
37 lines (29 loc) · 867 Bytes
/
context.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
package controllers
import "context"
type key int
const (
pageKey key = 1
tsnameKey key = 2
)
var (
ctx context.Context
cancel context.CancelFunc
)
// newContextPage returns a new Context with the page to skip to.
// Used when we want to display long lists of records
func newContextPage(ctx context.Context, p int) context.Context {
return context.WithValue(ctx, pageKey, p)
}
// fromContextPage retrieves the page to skip to when paginating, from a http Request Context
func fromContextPage(ctx context.Context) (string, int, bool) {
page, ok1 := ctx.Value(pageKey).(int)
tsname, ok2 := ctx.Value(tsnameKey).(string)
if !ok1 || !ok2 {
return tsname, page, false
}
return tsname, page, true
}
// newContextTSName
func newContextTSName(ctx context.Context, tsname string) context.Context {
return context.WithValue(ctx, tsnameKey, tsname)
}