Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/log/slog/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package slog

import (
"iter"
"runtime"
"slices"
"time"
Expand Down Expand Up @@ -92,6 +93,11 @@ func (r Record) Attrs(f func(Attr) bool) {
}
}

// AllAttrs returns an iterator over every attribute in the [Record].
func (r Record) AllAttrs() iter.Seq[Attr] {
return r.Attrs
}

// AddAttrs appends the given Attrs to the [Record]'s list of Attrs.
// It omits empty groups.
func (r *Record) AddAttrs(attrs ...Attr) {
Expand Down
16 changes: 16 additions & 0 deletions src/log/slog/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ func TestRecordAttrs(t *testing.T) {
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.AllAttrs() {
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) {
Expand Down