mirror of
https://github.com/hanzo-fi/go-libs.git
synced 2026-10-30 20:53:44 +00:00
Migrate all internal usages of github.com/pkg/errors to the Go standard library: errors.New/Is/As map directly to the stdlib equivalents, while errors.Wrap/Wrapf become fmt.Errorf(..., %w, err). github.com/pkg/errors is no longer a direct dependency (remains indirect via watermill). Constraint: errors.Wrap returns nil on nil input; fmt.Errorf does not, so run.go's "executing migration" wrap was rewritten as an explicit nil check Confidence: high Scope-risk: narrow
35 lines
627 B
Go
35 lines
627 B
Go
package paginate
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
)
|
|
|
|
func Iterate[T any, Q any](ctx context.Context, q Q, iterator func(ctx context.Context, q Q) (*Cursor[T], error), cb func(cursor *Cursor[T]) error) error {
|
|
|
|
for {
|
|
cursor, err := iterator(ctx, q)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := cb(cursor); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !cursor.HasMore {
|
|
break
|
|
}
|
|
|
|
newQuery := reflect.New(reflect.TypeOf(q))
|
|
if err := UnmarshalCursor(cursor.Next, newQuery.Interface()); err != nil {
|
|
return fmt.Errorf("paginating next request: %w", err)
|
|
}
|
|
|
|
q = newQuery.Elem().Interface().(Q)
|
|
}
|
|
|
|
return nil
|
|
}
|