internal/mcp: change paginateList to a generic helper
This CL simplifies the paginateList function in server.go to use a generic helper for tools, resources, and prompts. Change-Id: Ide0d2a90d715374280067e094d8870882e6bddfa Reviewed-on: https://go-review.googlesource.com/c/tools/+/678055 Auto-Submit: Sam Thanawalla <samthanawalla@google.com> Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
12761ffcb6
commit
30af200d5e
1 changed files with 96 additions and 81 deletions
|
|
@ -391,23 +391,13 @@ func (cs *ClientSession) Prompts(ctx context.Context, params *ListPromptsParams)
|
|||
})
|
||||
}
|
||||
|
||||
type ListParams interface {
|
||||
// Returns a pointer to the param's Cursor field.
|
||||
cursorPtr() *string
|
||||
}
|
||||
|
||||
type ListResult[T any] interface {
|
||||
// Returns a pointer to the param's NextCursor field.
|
||||
nextCursorPtr() *string
|
||||
}
|
||||
|
||||
// paginate is a generic helper function to provide a paginated iterator.
|
||||
func paginate[P ListParams, R ListResult[E], E any](ctx context.Context, params P, listFunc func(context.Context, P) (R, error), items func(R) []*E) iter.Seq2[E, error] {
|
||||
return func(yield func(E, error) bool) {
|
||||
func paginate[P listParams, R listResult[T], T any](ctx context.Context, params P, listFunc func(context.Context, P) (R, error), items func(R) []*T) iter.Seq2[T, error] {
|
||||
return func(yield func(T, error) bool) {
|
||||
for {
|
||||
res, err := listFunc(ctx, params)
|
||||
if err != nil {
|
||||
var zero E
|
||||
var zero T
|
||||
yield(zero, err)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue