chore(tools): centralize pagination cap helper
This commit is contained in:
@@ -76,9 +76,7 @@ func (t *CodeSearch) Call(ctx context.Context, raw json.RawMessage) (json.RawMes
|
||||
if args.Page < 1 {
|
||||
args.Page = 1
|
||||
}
|
||||
if args.Limit < 1 || args.Limit > 50 {
|
||||
args.Limit = 30
|
||||
}
|
||||
args.Limit = capLimit(args.Limit, 30)
|
||||
|
||||
if args.Repo != "" {
|
||||
return t.singleRepo(ctx, args)
|
||||
|
||||
@@ -48,10 +48,8 @@ func (t *RepoList) Call(ctx context.Context, raw json.RawMessage) (json.RawMessa
|
||||
if err := t.a.Check(args.Owner); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if args.Limit == 0 || args.Limit > 50 {
|
||||
args.Limit = 30
|
||||
}
|
||||
if args.Page == 0 {
|
||||
args.Limit = capLimit(args.Limit, 30)
|
||||
if args.Page < 1 {
|
||||
args.Page = 1
|
||||
}
|
||||
repos, err := t.c.ListRepos(ctx, args.Owner, args.Page, args.Limit)
|
||||
|
||||
@@ -60,9 +60,7 @@ func (t *RepoSearch) Call(ctx context.Context, raw json.RawMessage) (json.RawMes
|
||||
if args.Page < 1 {
|
||||
args.Page = 1
|
||||
}
|
||||
if args.Limit < 1 || args.Limit > 50 {
|
||||
args.Limit = 30
|
||||
}
|
||||
args.Limit = capLimit(args.Limit, 30)
|
||||
|
||||
repos, err := t.c.SearchRepos(ctx, args.Q, args.Owner, args.Page, args.Limit)
|
||||
if err != nil {
|
||||
|
||||
@@ -22,3 +22,14 @@ func parseArgs(raw json.RawMessage, dst any) error {
|
||||
}
|
||||
|
||||
func _ctx(ctx context.Context) context.Context { return ctx } // stub for future hooks
|
||||
|
||||
// capLimit returns a sane page size: 0 or negative → def, > 50 → 50.
|
||||
func capLimit(in, def int) int {
|
||||
if in <= 0 {
|
||||
return def
|
||||
}
|
||||
if in > 50 {
|
||||
return 50
|
||||
}
|
||||
return in
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user