log/slog: add Record.AttrsIter
Fixes #61934
diff --git a/src/log/slog/record.go b/src/log/slog/record.go
index 3b4e68c..d7a3b54 100644
--- a/src/log/slog/record.go
+++ b/src/log/slog/record.go
@@ -5,6 +5,7 @@
package slog
import (
+ "iter"
"runtime"
"slices"
"time"
@@ -92,6 +93,13 @@
}
}
+// AttrsIter returns an iterator over every attribute in the [Record].
+func (r Record) AttrsIter() iter.Seq[Attr] {
+ return func(yield func(Attr) bool) {
+ r.Attrs(yield)
+ }
+}
+
// AddAttrs appends the given Attrs to the [Record]'s list of Attrs.
// It omits empty groups.
func (r *Record) AddAttrs(attrs ...Attr) {
diff --git a/src/log/slog/record_test.go b/src/log/slog/record_test.go
index 939dc34..a81477f 100644
--- a/src/log/slog/record_test.go
+++ b/src/log/slog/record_test.go
@@ -36,6 +36,22 @@
t.Errorf("got %v, want %v", got, want)
}
}
+
+ // Early return with iterator.
+ // Hit both loops in Record.Attrs: front and back.
+ for _, stop := range []int{2, 6} {
+ var got []Attr
+ for a := range r.AttrsIter() {
+ got = append(got, a)
+ if len(got) < stop {
+ break
+ }
+ }
+ want := as[:stop]
+ if !attrsEqual(got, want) {
+ t.Errorf("got %v, want %v", got, want)
+ }
+ }
}
func TestRecordSource(t *testing.T) {
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
I spotted some possible problems with your PR:
1. The commit message body is very brief. That can be OK if the change is trivial like correcting spelling or fixing a broken link, but usually the description should provide context for the change and explain what it does in complete sentences.
Please address any problems by updating the GitHub PR.
When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.
To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.
For more details, see:
(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Congratulations on opening your first change. Thank you for your contribution!
Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.
Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.
During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Hold | +1 |
proposal not yet accepted.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
return func(yield func(Attr) bool) {Could you just return `r.Attrs`?
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Hold | +1 |
Please change the commit message.
Could you just return `r.Attrs`?
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |