From a8a1c613a4edddeeb0543a7bfe98276509cfd2f0 Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Mon, 16 Jun 2025 12:04:52 +0200 Subject: [PATCH] fix(pagination): handle empty page return case Adds a check for empty pages in the pagination logic to return a nil slice and empty PageInfo when there are no items to display. Updates tests to verify the behavior when no more items are available, ensuring robustness in pagination functionality. --- pagination.go | 3 +++ pagination_test.go | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/pagination.go b/pagination.go index 06ec629..4fc6b0a 100644 --- a/pagination.go +++ b/pagination.go @@ -88,6 +88,9 @@ func GetPage[T any](items []T, first *int, after *string, last *int, before *str eIx = idx } page := items[sIx:min(eIx, len(items))] + if len(page) == 0 { + return nil, PageInfo{} + } return page, PageInfo{ StartCursor: ptr(EncodeCursor(fn(page[0]))), HasNextPage: eIx < len(items), diff --git a/pagination_test.go b/pagination_test.go index 7db8da6..0f30590 100644 --- a/pagination_test.go +++ b/pagination_test.go @@ -45,6 +45,12 @@ func TestGetPage(t *testing.T) { EndCursor: ptr("Mg=="), }, }, + { + name: "no more items", + args: args[string]{items: []string{"1", "2", "3"}, first: ptr(2), after: ptr("Mw=="), max: 10, fn: func(s string) string { return s }}, + wantItems: nil, + wantPageInfo: PageInfo{}, + }, { name: "first 10", args: args[string]{items: []string{"1", "2", "3"}, first: ptr(10), max: 10, fn: func(s string) string { return s }},