-
Notifications
You must be signed in to change notification settings - Fork 351
fix(pagination): continue after empty cursor pages #893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package pagination | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net/http" | ||
| "reflect" | ||
|
|
||
|
|
@@ -328,10 +329,6 @@ func (r *NextCursorPage[T]) UnmarshalJSON(data []byte) error { | |
| // there is no next page, this function will return a 'nil' for the page value, but | ||
| // will not return an error | ||
| func (r *NextCursorPage[T]) GetNextPage() (res *NextCursorPage[T], err error) { | ||
| if len(r.Data) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if r.JSON.HasMore.Valid() && r.HasMore == false { | ||
| return nil, nil | ||
| } | ||
|
|
@@ -364,36 +361,49 @@ func (r *NextCursorPage[T]) SetPageConfig(cfg *requestconfig.RequestConfig, res | |
| } | ||
|
|
||
| type NextCursorPageAutoPager[T any] struct { | ||
| page *NextCursorPage[T] | ||
| cur T | ||
| idx int | ||
| run int | ||
| err error | ||
| page *NextCursorPage[T] | ||
| cur T | ||
| idx int | ||
| run int | ||
| err error | ||
| seenCursors map[string]struct{} | ||
| paramObj | ||
| } | ||
|
|
||
| func NewNextCursorPageAutoPager[T any](page *NextCursorPage[T], err error) *NextCursorPageAutoPager[T] { | ||
| return &NextCursorPageAutoPager[T]{ | ||
| page: page, | ||
| err: err, | ||
| page: page, | ||
| err: err, | ||
| seenCursors: make(map[string]struct{}), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When AGENTS.md reference: AGENTS.md:L71-L73 Useful? React with 👍 / 👎. |
||
| } | ||
| } | ||
|
|
||
| func (r *NextCursorPageAutoPager[T]) Next() bool { | ||
| if r.page == nil || len(r.page.Data) == 0 { | ||
| return false | ||
| } | ||
| if r.idx >= len(r.page.Data) { | ||
| for r.page != nil { | ||
| if r.idx < len(r.page.Data) { | ||
| r.cur = r.page.Data[r.idx] | ||
| r.run += 1 | ||
| r.idx += 1 | ||
| return true | ||
| } | ||
| if r.page.JSON.HasMore.Valid() && !r.page.HasMore { | ||
| r.page = nil | ||
| return false | ||
| } | ||
| if r.page.Next != "" { | ||
| if _, ok := r.seenCursors[r.page.Next]; ok { | ||
| r.err = errors.New("pagination cursor did not advance") | ||
| return false | ||
| } | ||
| r.seenCursors[r.page.Next] = struct{}{} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On a successful pagination with unique cursors, every exhausted page adds another string to AGENTS.md reference: AGENTS.md:L197-L198 Useful? React with 👍 / 👎. |
||
| } | ||
| r.idx = 0 | ||
| r.page, r.err = r.page.GetNextPage() | ||
| if r.err != nil || r.page == nil || len(r.page.Data) == 0 { | ||
| if r.err != nil { | ||
|
Comment on lines
400
to
+402
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an empty page returns AGENTS.md reference: AGENTS.md:L38-L40 Useful? React with 👍 / 👎. |
||
| return false | ||
| } | ||
| } | ||
| r.cur = r.page.Data[r.idx] | ||
| r.run += 1 | ||
| r.idx += 1 | ||
| return true | ||
| return false | ||
| } | ||
|
|
||
| func (r *NextCursorPageAutoPager[T]) Current() T { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes Castiron-owned shared pagination scaffolding, but the commit contains neither a generator change nor regenerated metadata, leaving the underlying template to keep emitting the faulty empty-page guard for future generations and sibling SDKs. Fix the shared Castiron template and regenerate this file rather than carrying only a repository-local patch.
AGENTS.md reference: AGENTS.md:L68-L70
Useful? React with 👍 / 👎.