diff --git a/cache/metadata_test.go b/cache/metadata_test.go index 1ad1d26..8260bee 100644 --- a/cache/metadata_test.go +++ b/cache/metadata_test.go @@ -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 } diff --git a/cache/paging.go b/cache/paging.go index d331c25..bd797dd 100644 --- a/cache/paging.go +++ b/cache/paging.go @@ -10,15 +10,10 @@ 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} }, @@ -26,15 +21,10 @@ func (s ObjectTypeSlice) Paginate(page *dsc2.PaginationRequest) (*paging.Result[ } 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 @@ -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} }, diff --git a/paging/cursor.go b/paging/cursor.go index ef6bed0..558d344 100644 --- a/paging/cursor.go +++ b/paging/cursor.go @@ -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" ) @@ -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 -} diff --git a/paging/slice.go b/paging/slice.go index 6001291..81af7f0 100644 --- a/paging/slice.go +++ b/paging/slice.go @@ -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" @@ -11,13 +10,14 @@ 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], @@ -25,8 +25,8 @@ func PaginateSlice[T any]( 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 } @@ -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 @@ -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 }