-
Notifications
You must be signed in to change notification settings - Fork 111
/
page_token.go
43 lines (35 loc) · 936 Bytes
/
page_token.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
package server
import (
"encoding/base64"
"fmt"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"google.golang.org/protobuf/proto"
)
const _defaultPageSize = 20
func unmarshalPageToken(reqToken string) (*adminv1.StringPageToken, error) {
token := &adminv1.StringPageToken{}
if reqToken != "" {
in, err := base64.URLEncoding.DecodeString(reqToken)
if err != nil {
return nil, fmt.Errorf("Failed to parse request token: %w", err)
}
if err := proto.Unmarshal(in, token); err != nil {
return nil, fmt.Errorf("Failed to parse request token: %w", err)
}
}
return token, nil
}
func marshalPageToken(val string) string {
token := &adminv1.StringPageToken{Val: val}
bytes, err := proto.Marshal(token)
if err != nil {
panic(err)
}
return base64.URLEncoding.EncodeToString(bytes)
}
func validPageSize(pageSize uint32) int {
if pageSize == 0 {
return _defaultPageSize
}
return int(pageSize)
}