Skip to content

Commit

Permalink
feat: faster attribute processing
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Jan 3, 2025
1 parent 67b1ddd commit ae3e45b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 20 deletions.
22 changes: 3 additions & 19 deletions attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"reflect"
"runtime"
"slices"
"strconv"
"strings"

"github.com/samber/lo"
Expand All @@ -20,9 +19,8 @@ func AppendRecordAttrsToAttrs(attrs []slog.Attr, groups []string, record *slog.R
output := make([]slog.Attr, 0, len(attrs)+record.NumAttrs())
output = append(output, attrs...)

slices.Reverse(groups)
record.Attrs(func(attr slog.Attr) bool {
for i := range groups {
for i := len(groups) - 1; i >= 0; i-- {
attr = slog.Group(groups[i], attr)
}
output = append(output, attr)
Expand Down Expand Up @@ -146,24 +144,10 @@ func AttrsToString(attrs ...slog.Attr) map[string]string {

func ValueToString(v slog.Value) string {
switch v.Kind() {
case slog.KindAny:
return AnyValueToString(v)
case slog.KindLogValuer:
case slog.KindAny, slog.KindLogValuer, slog.KindGroup:
return AnyValueToString(v)
case slog.KindGroup:
return AnyValueToString(v)
case slog.KindInt64:
return fmt.Sprintf("%d", v.Int64())
case slog.KindUint64:
return fmt.Sprintf("%d", v.Uint64())
case slog.KindFloat64:
return fmt.Sprintf("%f", v.Float64())
case slog.KindString:
case slog.KindInt64, slog.KindUint64, slog.KindFloat64, slog.KindString, slog.KindBool, slog.KindDuration:
return v.String()
case slog.KindBool:
return strconv.FormatBool(v.Bool())
case slog.KindDuration:
return v.Duration().String()
case slog.KindTime:
return v.Time().UTC().String()
default:
Expand Down
2 changes: 1 addition & 1 deletion attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ func TestValueToString(t *testing.T) {
},
"KindFloat64": {
input: slog.Float64("key", 3.14),
expected: "3.140000",
expected: "3.14",
},
"KindString": {
input: slog.String("key", "test"),
Expand Down

0 comments on commit ae3e45b

Please # to comment.