From 2d61f44442a3e338267967610954f678b0dea841 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Sun, 11 Sep 2022 13:31:42 -0400 Subject: [PATCH] slog: remove Attr.AppendValue Attr.AppendValue was originally intended to be a convenient and more efficient way of formatting Attr values. But there is no one clearly right way to format them, and the efficiency argument doesn't really hold water: AppendValue skips the redundant but cheap type check that occurs in switch a.Kind() { case IntKind: ... a.Int() .. but then formats Times and Durations as strings instead of integers. We keep it internally to help out in a couple of places, but overall it's better if Handler implementations do their own value formatting. For casual use, fmt.Sprint(a.Value()) is fine. Change-Id: Ie265f1ee5daf5f8002bfc2b70f4bf461cb07a10b Reviewed-on: https://go-review.googlesource.com/c/exp/+/430098 Run-TryBot: Jonathan Amsterdam Reviewed-by: Alan Donovan --- slog/attr.go | 4 ++-- slog/attr_safe.go | 2 +- slog/attr_unsafe.go | 2 +- slog/text_handler.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/slog/attr.go b/slog/attr.go index 5878165d4..9e783d5d4 100644 --- a/slog/attr.go +++ b/slog/attr.go @@ -279,9 +279,9 @@ func (a1 Attr) Equal(a2 Attr) bool { } } -// AppendValue appends a text representation of the Attr's value to dst. +// appendValue appends a text representation of the Attr's value to dst. // The value is formatted as with fmt.Sprint. -func (a Attr) AppendValue(dst []byte) []byte { +func (a Attr) appendValue(dst []byte) []byte { switch a.Kind() { case StringKind: return append(dst, a.str()...) diff --git a/slog/attr_safe.go b/slog/attr_safe.go index 6dc614924..37d997d90 100644 --- a/slog/attr_safe.go +++ b/slog/attr_safe.go @@ -58,5 +58,5 @@ func (a Attr) String() string { return a.str() } var buf []byte - return string(a.AppendValue(buf)) + return string(a.appendValue(buf)) } diff --git a/slog/attr_unsafe.go b/slog/attr_unsafe.go index 9e4d7eabf..1f46d6056 100644 --- a/slog/attr_unsafe.go +++ b/slog/attr_unsafe.go @@ -78,5 +78,5 @@ func (a Attr) String() string { return s } var buf []byte - return string(a.AppendValue(buf)) + return string(a.appendValue(buf)) } diff --git a/slog/text_handler.go b/slog/text_handler.go index 14d7a3e24..b67a65094 100644 --- a/slog/text_handler.go +++ b/slog/text_handler.go @@ -136,7 +136,7 @@ func (ap *textAppender) appendAttrValue(a Attr) error { } ap.appendString(fmt.Sprint(a.Value())) default: - *ap.buf() = a.AppendValue(*ap.buf()) + *ap.buf() = a.appendValue(*ap.buf()) } return nil }