Add limit iterator and context plumbing for --limit flag#4724
Open
simonfaltum wants to merge 3 commits intomainfrom
Open
Add limit iterator and context plumbing for --limit flag#4724simonfaltum wants to merge 3 commits intomainfrom
simonfaltum wants to merge 3 commits intomainfrom
Conversation
Collaborator
|
Commit: 1fbc876
16 interesting tests: 7 SKIP, 6 RECOVERED, 3 flaky
Top 20 slowest tests (at least 2 minutes):
|
3c4f97c to
8fb9c37
Compare
4b7a713 to
8c45e01
Compare
8c45e01 to
b9184da
Compare
d0812ba to
67aaf00
Compare
Contributor
shreyas-goenka
left a comment
There was a problem hiding this comment.
Note: This review was posted by Claude (AI assistant). Shreyas will do a separate, more thorough review pass.
Priority: LOW — Clean, well-structured PR
MEDIUM: Next() doesn't guard against remaining <= 0
limitIterator.Next() unconditionally delegates to inner.Next() without checking remaining > 0. If Next() is called without first checking HasNext(), the limit won't be enforced (remaining goes negative). Low severity since all callers use the HasNext()/Next() pattern, but a guard would be cheap:
func (i *limitIterator[T]) Next(ctx context.Context) (T, error) {
if i.remaining <= 0 {
var zero T
return zero, fmt.Errorf("iterator exhausted")
}
// ...
}What looks good
WithLimit/GetLimitfollows standard Go context value patternApplyLimit(ctx, iter)keeps call sites cleanlimitIteratoris unexported (correct encapsulation)- Limit of 0 means "no limit" (common CLI convention)
- Thorough table-driven tests covering edge cases
- Lazy evaluation preserves iterator semantics
Overall excellent PR. The Next() guard is the only actionable item.
Return listing.ErrNoMoreItems when Next() is called with remaining <= 0, so the limit is enforced even if the caller skips HasNext().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The codegen template generates a --limit flag on every paginated list command. That generated code calls cmdio.WithLimit(ctx, n) to store the limit in context. RenderIterator then applies a limit iterator to cap total results. This PR adds the runtime plumbing that the generated code depends on.
Changes
Before: no mechanism to cap total results from list commands.
Now: libs/cmdio provides a limit iterator (wraps a listing.Iterator and stops after N items), WithLimit/GetLimit (context key for the limit value), and an ApplyLimit helper. All three RenderIterator functions call ApplyLimit before rendering.
Implementation:
Test plan