slog: export Attr fields
Export the Key and Value fields of Attr.
Remove unnecessary methods.
Change-Id: Icfbe149d7ebab072102d58fe367c2d121db5a2d6
Reviewed-on: https://go-review.googlesource.com/c/exp/+/437957
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/slog/attr.go b/slog/attr.go
index 6236db6..32d862d 100644
--- a/slog/attr.go
+++ b/slog/attr.go
@@ -10,19 +10,11 @@
)
// An Attr is a key-value pair.
-// It can represent most small values without an allocation.
-// The zero Attr has a key of "" and a value of nil.
type Attr struct {
- key string
- value Value
+ Key string
+ Value Value
}
-// Key returns the Attr's key.
-func (a Attr) Key() string { return a.key }
-
-// Value returns the Attr's Value.
-func (a Attr) Value() Value { return a.value }
-
// String returns an Attr for a string value.
func String(key, value string) Attr {
return Attr{key, StringValue(value)}
@@ -71,17 +63,11 @@
return Attr{key, AnyValue(value)}
}
-// WithKey returns an attr with the given key and the receiver's value.
-func (a Attr) WithKey(key string) Attr {
- a.key = key
- return a
-}
-
// Equal reports whether two Attrs have equal keys and values.
func (a1 Attr) Equal(a2 Attr) bool {
- return a1.key == a2.key && a1.value.Equal(a2.value)
+ return a1.Key == a2.Key && a1.Value.Equal(a2.Value)
}
func (a Attr) String() string {
- return fmt.Sprintf("%s=%s", a.key, a.value)
+ return fmt.Sprintf("%s=%s", a.Key, a.Value)
}
diff --git a/slog/attr_test.go b/slog/attr_test.go
index 1e8bde4..6b1f19a 100644
--- a/slog/attr_test.go
+++ b/slog/attr_test.go
@@ -22,13 +22,13 @@
d time.Duration
)
a := int(testing.AllocsPerRun(5, func() {
- i = Int64("key", 1).Value().Int64()
- u = Uint64("key", 1).Value().Uint64()
- f = Float64("key", 1).Value().Float64()
- b = Bool("key", true).Value().Bool()
- s = String("key", "foo").Value().String()
- d = Duration("key", d).Value().Duration()
- x = Any("key", p).Value().Any()
+ i = Int64("key", 1).Value.Int64()
+ u = Uint64("key", 1).Value.Uint64()
+ f = Float64("key", 1).Value.Float64()
+ b = Bool("key", true).Value.Bool()
+ s = String("key", "foo").Value.String()
+ d = Duration("key", d).Value.Duration()
+ x = Any("key", p).Value.Any()
}))
if a != 0 {
t.Errorf("got %d allocs, want zero", a)
diff --git a/slog/benchmarks/handlers.go b/slog/benchmarks/handlers.go
index fce0bc5..05bc1d9 100644
--- a/slog/benchmarks/handlers.go
+++ b/slog/benchmarks/handlers.go
@@ -49,9 +49,9 @@
buf.WriteString(r.Message())
r.Attrs(func(a slog.Attr) {
buf.WriteByte(' ')
- buf.WriteString(a.Key())
+ buf.WriteString(a.Key)
buf.WriteByte('=')
- h.appendValue(buf, a.Value())
+ h.appendValue(buf, a.Value)
})
buf.WriteByte('\n')
_, err := h.w.Write(*buf)
diff --git a/slog/benchmarks/zapbenchmarks/zap_encoders_test.go b/slog/benchmarks/zapbenchmarks/zap_encoders_test.go
index 6ae762c..0058d9a 100644
--- a/slog/benchmarks/zapbenchmarks/zap_encoders_test.go
+++ b/slog/benchmarks/zapbenchmarks/zap_encoders_test.go
@@ -38,8 +38,8 @@
var fields []zap.Field
for _, a := range slogbench.TestAttrs {
var f zap.Field
- k := a.Key()
- v := a.Value()
+ k := a.Key
+ v := a.Value
switch v.Kind() {
case slog.StringKind:
f = zap.String(k, v.String())
diff --git a/slog/handler.go b/slog/handler.go
index 3894dde..aeead7b 100644
--- a/slog/handler.go
+++ b/slog/handler.go
@@ -221,11 +221,11 @@
if rep := s.h.opts.ReplaceAttr; rep != nil {
a = rep(a)
}
- if a.Key() == "" {
+ if a.Key == "" {
return
}
- s.appendKey(a.Key())
- s.appendValue(a.Value())
+ s.appendKey(a.Key)
+ s.appendValue(a.Value)
}
func (s *handleState) appendError(err error) {
diff --git a/slog/handler_test.go b/slog/handler_test.go
index fcf7dc5..93c00ac 100644
--- a/slog/handler_test.go
+++ b/slog/handler_test.go
@@ -121,7 +121,8 @@
}
func upperCaseKey(a Attr) Attr {
- return a.WithKey(strings.ToUpper(a.Key()))
+ a.Key = strings.ToUpper(a.Key)
+ return a
}
func TestHandlerEnabled(t *testing.T) {
diff --git a/slog/json_handler_test.go b/slog/json_handler_test.go
index 819efb9..df0628b 100644
--- a/slog/json_handler_test.go
+++ b/slog/json_handler_test.go
@@ -130,24 +130,24 @@
{"defaults", HandlerOptions{}},
{"time format", HandlerOptions{
ReplaceAttr: func(a Attr) Attr {
- v := a.Value()
+ v := a.Value
if v.Kind() == TimeKind {
- return String(a.Key(), v.Time().Format(rfc3339Millis))
+ return String(a.Key, v.Time().Format(rfc3339Millis))
}
- if a.Key() == "level" {
- return a.WithKey("severity")
+ if a.Key == "level" {
+ return Attr{"severity", a.Value}
}
return a
},
}},
{"time unix", HandlerOptions{
ReplaceAttr: func(a Attr) Attr {
- v := a.Value()
+ v := a.Value
if v.Kind() == TimeKind {
- return Int64(a.Key(), v.Time().UnixNano())
+ return Int64(a.Key, v.Time().UnixNano())
}
- if a.Key() == "level" {
- return a.WithKey("severity")
+ if a.Key == "level" {
+ return Attr{"severity", a.Value}
}
return a
},