-
Notifications
You must be signed in to change notification settings - Fork 19
/
pagination.go
118 lines (98 loc) · 4.03 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
// Copyright 2022 Fraunhofer AISEC
//
// 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.
//
// $$\ $$\ $$\ $$\
// $$ | $$ |\__| $$ |
// $$$$$$$\ $$ | $$$$$$\ $$\ $$\ $$$$$$$ |$$\ $$$$$$\ $$$$$$\ $$$$$$\
// $$ _____|$$ |$$ __$$\ $$ | $$ |$$ __$$ |$$ |\_$$ _| $$ __$$\ $$ __$$\
// $$ / $$ |$$ / $$ |$$ | $$ |$$ / $$ |$$ | $$ | $$ / $$ |$$ | \__|
// $$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$\ $$ | $$ |$$ |
// \$$$$$$\ $$ |\$$$$$ |\$$$$$ |\$$$$$$ |$$ | \$$$ |\$$$$$ |$$ |
// \_______|\__| \______/ \______/ \_______|\__| \____/ \______/ \__|
//
// This file is part of Clouditor Community Edition.
package api
import (
"context"
"encoding/base64"
"fmt"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
// PageTokenField is the protobuf field that contains our page token.
const PageTokenField = "page_token"
// PaginatedRequest contains the typical parameters for a paginated request, usually a request for a List gRPC call.
type PaginatedRequest interface {
GetPageToken() string
GetPageSize() int32
proto.Message
}
// PaginatedResponse contains the typical parameters for a paginated response, usually a response for a List gRPC call.
type PaginatedResponse interface {
GetNextPageToken() string
}
// Encode encodes this page token into a base64 URL encoded string.
func (t *PageToken) Encode() (b64token string, err error) {
var b []byte
b, err = proto.Marshal(t)
if err != nil {
return "", fmt.Errorf("error while marshaling protobuf message: %w", err)
}
b64token = base64.URLEncoding.EncodeToString(b)
return
}
// DecodePageToken decodes a PageToken out of a base 64 URL encoded string.
func DecodePageToken(b64token string) (t *PageToken, err error) {
var b []byte
b, err = base64.URLEncoding.DecodeString(b64token)
if err != nil {
return nil, fmt.Errorf("error while decoding base64 token: %w", err)
}
t = new(PageToken)
err = proto.Unmarshal(b, t)
if err != nil {
return nil, fmt.Errorf("error while unmarshalling protobuf message: %w", err)
}
return
}
// ListAllPaginated invokes a List gRPC function that supports pagination, fetches all pages using individual calls and
// finally combines all results of all pages into a single slice. It executes the function specified in list using the
// req of RequestType. Afterwards, the function getter is executed to transform the response of the list calls into the
// results slice.
func ListAllPaginated[ResponseType PaginatedResponse, RequestType PaginatedRequest, ResultType any](req RequestType, list func(context.Context, RequestType, ...grpc.CallOption) (ResponseType, error), getter func(res ResponseType) []ResultType) (results []ResultType, err error) {
var (
res ResponseType
pageToken string = ""
)
for {
// Modify the request to include our page token using protoreflect. This will be empty for the first page
m := req.ProtoReflect()
m.Set(m.Descriptor().Fields().ByName(PageTokenField), protoreflect.ValueOf(pageToken))
// Call the list function to fetch the next page
res, err = list(context.Background(), req)
if err != nil {
// Transparently return the error of the list function without any wrapping
return nil, err
}
// Append results and retrieve our next page token
results = append(results, getter(res)...)
pageToken = res.GetNextPageToken()
// If the page token is empty, there are no more pages left to fetch
if pageToken == "" {
break
}
}
return
}