Skip to content

Commit

Permalink
List: get rid of ugly math and casting, use explicit switch
Browse files Browse the repository at this point in the history
Code review comments

Signed-off-by: Boris Glimcher <[email protected]>
  • Loading branch information
glimchb committed Apr 12, 2023
1 parent f379366 commit d2789cf
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions pkg/server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"crypto/rand"
"fmt"
"log"
"math"
"math/big"
"net"
"os"
Expand All @@ -27,23 +26,23 @@ func ExtractPagination(pageSize int32, pageToken string, pagination map[string]i
maxPageSize = 250
defaultPageSize = 50
)
if pageSize < 0 {
err := status.Error(codes.InvalidArgument, "negative PageSize is not allowed")
return -1, -1, err
}
// pick reasonable default and max sizes
size = defaultPageSize
if pageSize > 0 {
size = int(math.Min(float64(pageSize), maxPageSize))
switch {
case pageSize < 0:
return -1, -1, status.Error(codes.InvalidArgument, "negative PageSize is not allowed")
case pageSize == 0:
size = defaultPageSize
case pageSize > maxPageSize:
size = maxPageSize
default:
size = int(pageSize)
}
// fetch offset from the database using opaque token
offset = 0
if pageToken != "" {
var ok bool
offset, ok = pagination[pageToken]
if !ok {
err := status.Errorf(codes.NotFound, "unable to find pagination token %s", pageToken)
return -1, -1, err
return -1, -1, status.Errorf(codes.NotFound, "unable to find pagination token %s", pageToken)
}
log.Printf("Found offset %d from pagination token: %s", offset, pageToken)
}
Expand Down

0 comments on commit d2789cf

Please sign in to comment.