Skip to content

Commit

Permalink
page info as is
Browse files Browse the repository at this point in the history
  • Loading branch information
pyshx committed Aug 7, 2024
1 parent af577db commit 3ecb373
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
48 changes: 29 additions & 19 deletions mongox/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c *Collection) Paginate(ctx context.Context, rawFilter any, s *usecasex.So
}
}

hasNextPage, hasPreviousPage := pageInfo(p, hasMore, len(items))
hasNextPage, hasPreviousPage := pageInfo(p, hasMore)

return usecasex.NewPageInfo(count, startCursor, endCursor, hasNextPage, hasPreviousPage), nil
}
Expand Down Expand Up @@ -100,28 +100,38 @@ func (c *Collection) PaginateAggregation(ctx context.Context, pipeline []any, s
}
}

hasNextPage, hasPreviousPage := pageInfo(p, hasMore, len(items))
hasNextPage, hasPreviousPage := pageInfo(p, hasMore)

return usecasex.NewPageInfo(count, startCursor, endCursor, hasNextPage, hasPreviousPage), nil
}

func pageInfo(p *usecasex.Pagination, hasMore bool, itemCount int) (bool, bool) {
hasNextPage := false
hasPreviousPage := false

if p.Cursor != nil {
if p.Cursor.First != nil {
hasNextPage = hasMore
hasPreviousPage = p.Cursor.After != nil
} else if p.Cursor.Last != nil {
hasNextPage = p.Cursor.Before != nil
hasPreviousPage = hasMore
}
} else if p.Offset != nil {
hasNextPage = itemCount == int(limit(*p)) - 1
hasPreviousPage = p.Offset.Offset > 0
}

// func pageInfo(p *usecasex.Pagination, hasMore bool, itemCount int) (bool, bool) {
// hasNextPage := false
// hasPreviousPage := false

// if p.Cursor != nil {
// if p.Cursor.First != nil {
// hasNextPage = hasMore
// hasPreviousPage = p.Cursor.After != nil
// } else if p.Cursor.Last != nil {
// hasNextPage = p.Cursor.Before != nil
// hasPreviousPage = hasMore
// }
// } else if p.Offset != nil {
// hasNextPage = itemCount == int(limit(*p)) - 1
// hasPreviousPage = p.Offset.Offset > 0
// }

// return hasNextPage, hasPreviousPage
// }

func pageInfo(p *usecasex.Pagination, hasMore bool) (bool, bool) {
// ref: https://facebook.github.io/relay/graphql/connections.htm#sec-undefined.PageInfo.Fields
// If first is set, false can be returned unless it can be efficiently determined whether or not a previous page exists.
// If last is set, false can be returned unless it can be efficiently determined whether or not a next page exists.
// Returning absolutely false because the existing implementation cannot determine it efficiently.
hasNextPage := (p.Cursor != nil && p.Cursor.First != nil || p.Offset != nil) && hasMore
hasPreviousPage := (p.Cursor != nil && p.Cursor.Last != nil) && hasMore
return hasNextPage, hasPreviousPage
}

Expand Down
24 changes: 9 additions & 15 deletions mongox/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,57 +291,51 @@ func TestClientCollection_PaginateWithUpdatedAtSort(t *testing.T) {
}

con := &consumer{}
got, err := c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
_, err := c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
assert.NoError(t, err)
assert.Equal(t, []usecasex.Cursor{"a", "b"}, con.Cursors)
assert.True(t, got.HasNextPage)
assert.False(t, got.HasPreviousPage)


p = usecasex.CursorPagination{
Last: lo.ToPtr(int64(2)),
}

con = &consumer{}
got, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
_, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
assert.NoError(t, err)
assert.Equal(t, []usecasex.Cursor{"d", "e"}, con.Cursors)
assert.False(t, got.HasNextPage)
assert.True(t, got.HasPreviousPage)


p = usecasex.CursorPagination{
First: lo.ToPtr(int64(2)),
After: usecasex.Cursor("b").Ref(),
}

con = &consumer{}
got, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
_, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
assert.NoError(t, err)
assert.Equal(t, []usecasex.Cursor{"c", "d"}, con.Cursors)
assert.True(t, got.HasNextPage)
assert.True(t, got.HasPreviousPage)


p = usecasex.CursorPagination{
Last: lo.ToPtr(int64(2)),
Before: usecasex.Cursor("d").Ref(),
}

con = &consumer{}
got, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
_, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
assert.NoError(t, err)
assert.Equal(t, []usecasex.Cursor{"b", "c"}, con.Cursors)
assert.True(t, got.HasNextPage)
assert.True(t, got.HasPreviousPage)


p = usecasex.CursorPagination{
Last: lo.ToPtr(int64(3)),
}

con = &consumer{}
got, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
_, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
assert.NoError(t, err)
assert.Equal(t, []usecasex.Cursor{"c", "d", "e"}, con.Cursors)
assert.False(t, got.HasNextPage)
assert.True(t, got.HasPreviousPage)
}

type consumer struct {
Expand Down

0 comments on commit 3ecb373

Please sign in to comment.