Skip to content

Commit

Permalink
Update paging package to be version agnostic (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenh authored Oct 25, 2023
1 parent 553a19e commit 2ef8edd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 41 deletions.
4 changes: 2 additions & 2 deletions cache/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func testPagination[T Named, S PagingSlice[T]](t *testing.T, slice S) {
current++
}

if result.PageInfo.NextToken != "" {
page.Token = result.PageInfo.NextToken
if result.NextToken != "" {
page.Token = result.NextToken
} else {
break
}
Expand Down
27 changes: 6 additions & 21 deletions cache/paging.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,21 @@ type RelationTypeSlice []*dsc2.RelationType
type PermissionSlice []*dsc2.Permission

func (s ObjectTypeSlice) Paginate(page *dsc2.PaginationRequest) (*paging.Result[*dsc2.ObjectType], error) {
if paging.IsCountOnly(page) {
return &paging.Result[*dsc2.ObjectType]{
PageInfo: &dsc2.PaginationResponse{ResultSize: int32(len(s))},
}, nil
}

return paging.PaginateSlice(
s,
page,
page.Size,
page.Token,
1,
func(keys []string, ot *dsc2.ObjectType) bool { return keys[0] == ot.Name },
func(ot *dsc2.ObjectType) []string { return []string{ot.Name} },
)
}

func (s RelationTypeSlice) Paginate(page *dsc2.PaginationRequest) (*paging.Result[*dsc2.RelationType], error) {
if paging.IsCountOnly(page) {
return &paging.Result[*dsc2.RelationType]{
PageInfo: &dsc2.PaginationResponse{ResultSize: int32(len(s))},
}, nil
}

return paging.PaginateSlice(
s,
page,
page.Size,
page.Token,
2,
func(keys []string, relType *dsc2.RelationType) bool {
return keys[0] == relType.ObjectType && keys[1] == relType.Name
Expand All @@ -44,15 +34,10 @@ func (s RelationTypeSlice) Paginate(page *dsc2.PaginationRequest) (*paging.Resul
}

func (s PermissionSlice) Paginate(page *dsc2.PaginationRequest) (*paging.Result[*dsc2.Permission], error) {
if paging.IsCountOnly(page) {
return &paging.Result[*dsc2.Permission]{
PageInfo: &dsc2.PaginationResponse{ResultSize: int32(len(s))},
}, nil
}

return paging.PaginateSlice(
s,
page,
page.Size,
page.Token,
1,
func(keys []string, p *dsc2.Permission) bool { return keys[0] == p.Name },
func(p *dsc2.Permission) []string { return []string{p.Name} },
Expand Down
8 changes: 0 additions & 8 deletions paging/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/gob"

dsc2 "github.com/aserto-dev/go-directory/aserto/directory/common/v2"
"github.com/aserto-dev/go-directory/pkg/derr"
)

Expand Down Expand Up @@ -72,10 +71,3 @@ func (c *Cursor) Encode() (string, error) {

return base64.RawStdEncoding.EncodeToString(buf.Bytes()), nil
}

func IsCountOnly(p *dsc2.PaginationRequest) bool {
if p == nil {
return false
}
return p.Size == TotalsOnlyResultSet
}
18 changes: 8 additions & 10 deletions paging/slice.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package paging

import (
dsc2 "github.com/aserto-dev/go-directory/aserto/directory/common/v2"
"github.com/aserto-dev/go-directory/pkg/derr"
"github.com/pkg/errors"
"github.com/samber/lo"
Expand All @@ -11,22 +10,23 @@ type KeyComparer[T any] func([]string, T) bool
type KeyMapper[T any] func(T) []string

type Result[T any] struct {
Items []T
PageInfo *dsc2.PaginationResponse
Items []T
NextToken string
}

func PaginateSlice[T any](
s []T,
page *dsc2.PaginationRequest,
size int32,
token string,
keyCount int,
cmp KeyComparer[T],
mapper KeyMapper[T],
) (*Result[T], error) {
result := &Result[T]{}

start := 0
if page != nil && page.Token != "" {
cursor, err := DecodeCursor(page.Token)
if token != "" {
cursor, err := DecodeCursor(token)
if err != nil {
return result, err
}
Expand All @@ -44,7 +44,7 @@ func PaginateSlice[T any](
}
}

pageSize := lo.Min([]int32{page.Size, int32(len(s) - start)})
pageSize := lo.Min([]int32{size, int32(len(s) - start)})
end := start + int(pageSize)

var next *string
Expand All @@ -58,9 +58,7 @@ func PaginateSlice[T any](
}

result.Items = s[start:end]
result.PageInfo = &dsc2.PaginationResponse{
NextToken: lo.FromPtr(next),
}
result.NextToken = lo.FromPtr(next)

return result, nil
}

0 comments on commit 2ef8edd

Please sign in to comment.