]> Cypherpunks.ru repositories - gostls13.git/commitdiff
log/slog: function argument to Record.Attrs returns bool
authorJonathan Amsterdam <jba@google.com>
Wed, 12 Apr 2023 13:11:59 +0000 (09:11 -0400)
committerJonathan Amsterdam <jba@google.com>
Wed, 12 Apr 2023 20:33:37 +0000 (20:33 +0000)
Record.Attrs stops as soon as its argument function returns false.

Fixes #59060.

Change-Id: I578d64635e0e52b0fcdbc57f6d5a27a6efac8c70
Reviewed-on: https://go-review.googlesource.com/c/go/+/484096
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>

api/next/59060.txt [new file with mode: 0644]
src/log/slog/handler.go
src/log/slog/internal/benchmarks/handlers.go
src/log/slog/internal/benchmarks/handlers_test.go
src/log/slog/record.go
src/log/slog/record_test.go

diff --git a/api/next/59060.txt b/api/next/59060.txt
new file mode 100644 (file)
index 0000000..e362452
--- /dev/null
@@ -0,0 +1,2 @@
+pkg log/slog, method (Record) Attrs(func(Attr) bool) #59060
+
index 1fd0e76459c7a5570d05435a9334c90f0df17d09..d2f919800a268208635d0b4ef0f8a4aeb6da277b 100644 (file)
@@ -330,8 +330,9 @@ func (s *handleState) appendNonBuiltIns(r Record) {
        defer s.prefix.Free()
        s.prefix.WriteString(s.h.groupPrefix)
        s.openGroups()
-       r.Attrs(func(a Attr) {
+       r.Attrs(func(a Attr) bool {
                s.appendAttr(a)
+               return true
        })
        if s.h.json {
                // Close all open groups.
index 4f9213fb5cd2ae22cf9d3199cf5024cd153d786d..091e2ddcca0ac1af58f143d952b9d12b558fe963 100644 (file)
@@ -47,11 +47,12 @@ func (h *fastTextHandler) Handle(_ context.Context, r slog.Record) error {
        buf.WriteByte(' ')
        buf.WriteString("msg=")
        buf.WriteString(r.Message)
-       r.Attrs(func(a slog.Attr) {
+       r.Attrs(func(a slog.Attr) bool {
                buf.WriteByte(' ')
                buf.WriteString(a.Key)
                buf.WriteByte('=')
                h.appendValue(buf, a.Value)
+               return true
        })
        buf.WriteByte('\n')
        _, err := h.w.Write(*buf)
index 1777fde3683ad45f6bb96881e654371fbb0c55da..6c00c80286871b40445f6c7db02cfd4b2e609fac 100644 (file)
@@ -37,6 +37,6 @@ func TestHandlers(t *testing.T) {
 
 func attrSlice(r slog.Record) []slog.Attr {
        var as []slog.Attr
-       r.Attrs(func(a slog.Attr) { as = append(as, a) })
+       r.Attrs(func(a slog.Attr) bool { as = append(as, a); return true })
        return as
 }
index 0ee2a27f0ea6cae59ba9a0c3c6b455cfe31fc81d..4a5d9161197653ed11a457aa1fc1ddf15f842159 100644 (file)
@@ -86,13 +86,18 @@ func (r Record) NumAttrs() int {
 }
 
 // Attrs calls f on each Attr in the Record.
+// Iteration stops if f returns false.
 // The Attrs are already resolved.
-func (r Record) Attrs(f func(Attr)) {
+func (r Record) Attrs(f func(Attr) bool) {
        for i := 0; i < r.nFront; i++ {
-               f(r.front[i])
+               if !f(r.front[i]) {
+                       return
+               }
        }
        for _, a := range r.back {
-               f(a)
+               if !f(a) {
+                       return
+               }
        }
 }
 
index b1410f51abe3ace29ae149e069946db2843eb522..c40c6183fad8d804cf3fb5c908a0259468e2e3b9 100644 (file)
@@ -23,6 +23,17 @@ func TestRecordAttrs(t *testing.T) {
        if got := attrsSlice(r); !attrsEqual(got, as) {
                t.Errorf("got %v, want %v", got, as)
        }
+
+       // Early return.
+       var got []Attr
+       r.Attrs(func(a Attr) bool {
+               got = append(got, a)
+               return len(got) < 2
+       })
+       want := as[:2]
+       if !attrsEqual(got, want) {
+               t.Errorf("got %v, want %v", got, want)
+       }
 }
 
 func TestRecordSourceLine(t *testing.T) {
@@ -102,7 +113,7 @@ func newRecordWithAttrs(as []Attr) Record {
 
 func attrsSlice(r Record) []Attr {
        s := make([]Attr, 0, r.NumAttrs())
-       r.Attrs(func(a Attr) { s = append(s, a) })
+       r.Attrs(func(a Attr) bool { s = append(s, a); return true })
        return s
 }
 
@@ -157,7 +168,7 @@ func BenchmarkRecord(b *testing.B) {
                for j := 0; j < nAttrs; j++ {
                        r.AddAttrs(Int("k", j))
                }
-               r.Attrs(func(b Attr) { a = b })
+               r.Attrs(func(b Attr) bool { a = b; return true })
        }
        _ = a
 }