Skip to content

Commit

Permalink
slog: remove Attr.AppendValue
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Alan Donovan <[email protected]>
  • Loading branch information
jba committed Sep 15, 2022
1 parent d6a8ba4 commit 2d61f44
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions slog/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()...)
Expand Down
2 changes: 1 addition & 1 deletion slog/attr_safe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion slog/attr_unsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ func (a Attr) String() string {
return s
}
var buf []byte
return string(a.AppendValue(buf))
return string(a.appendValue(buf))
}
2 changes: 1 addition & 1 deletion slog/text_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 2d61f44

Please sign in to comment.