slog: rename Kind enum values
Change from FooKind to KindFoo to match Level and most of the standard
library.
Change-Id: I5660ba35814bd78481bbeec6f0059d3c4f1c014a
Reviewed-on: https://go-review.googlesource.com/c/exp/+/462755
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/benchmarks/handlers.go b/slog/benchmarks/handlers.go
index 7362871..ec40e58 100644
--- a/slog/benchmarks/handlers.go
+++ b/slog/benchmarks/handlers.go
@@ -61,21 +61,21 @@
func (h *fastTextHandler) appendValue(buf *buffer.Buffer, v slog.Value) {
v = v.Resolve()
switch v.Kind() {
- case slog.StringKind:
+ case slog.KindString:
buf.WriteString(v.String())
- case slog.Int64Kind:
+ case slog.KindInt64:
*buf = strconv.AppendInt(*buf, v.Int64(), 10)
- case slog.Uint64Kind:
+ case slog.KindUint64:
*buf = strconv.AppendUint(*buf, v.Uint64(), 10)
- case slog.Float64Kind:
+ case slog.KindFloat64:
*buf = strconv.AppendFloat(*buf, v.Float64(), 'g', -1, 64)
- case slog.BoolKind:
+ case slog.KindBool:
*buf = strconv.AppendBool(*buf, v.Bool())
- case slog.DurationKind:
+ case slog.KindDuration:
*buf = strconv.AppendInt(*buf, v.Duration().Nanoseconds(), 10)
- case slog.TimeKind:
+ case slog.KindTime:
h.appendTime(buf, v.Time())
- case slog.AnyKind:
+ case slog.KindAny:
a := v.Any()
switch a := a.(type) {
case error:
diff --git a/slog/benchmarks/zap_benchmarks/zap_encoders_test.go b/slog/benchmarks/zap_benchmarks/zap_encoders_test.go
index 2e28cf1..9bcb9f3 100644
--- a/slog/benchmarks/zap_benchmarks/zap_encoders_test.go
+++ b/slog/benchmarks/zap_benchmarks/zap_encoders_test.go
@@ -41,15 +41,15 @@
k := a.Key
v := a.Value
switch v.Kind() {
- case slog.StringKind:
+ case slog.KindString:
f = zap.String(k, v.String())
- case slog.Int64Kind:
+ case slog.KindInt64:
f = zap.Int64(k, v.Int64())
- case slog.DurationKind:
+ case slog.KindDuration:
f = zap.Duration(k, v.Duration())
- case slog.TimeKind:
+ case slog.KindTime:
f = zap.Time(k, v.Time())
- case slog.AnyKind:
+ case slog.KindAny:
f = zap.Any(k, v)
default:
panic(fmt.Sprintf("unknown kind %d", v.Kind()))
diff --git a/slog/handler.go b/slog/handler.go
index 7fcf1da..b7b2a1d 100644
--- a/slog/handler.go
+++ b/slog/handler.go
@@ -424,7 +424,7 @@
return
}
v := a.Value.Resolve()
- if rep := s.h.opts.ReplaceAttr; rep != nil && v.Kind() != GroupKind {
+ if rep := s.h.opts.ReplaceAttr; rep != nil && v.Kind() != KindGroup {
var gs []string
if s.groups != nil {
gs = *s.groups
@@ -435,7 +435,7 @@
}
v = a.Value.Resolve()
}
- if v.Kind() == GroupKind {
+ if v.Kind() == KindGroup {
s.openGroup(a.Key)
for _, aa := range v.Group() {
s.appendAttr(aa)
diff --git a/slog/json_handler.go b/slog/json_handler.go
index 0475034..c373936 100644
--- a/slog/json_handler.go
+++ b/slog/json_handler.go
@@ -100,13 +100,13 @@
func appendJSONValue(s *handleState, v Value) error {
switch v.Kind() {
- case StringKind:
+ case KindString:
s.appendString(v.str())
- case Int64Kind:
+ case KindInt64:
*s.buf = strconv.AppendInt(*s.buf, v.Int64(), 10)
- case Uint64Kind:
+ case KindUint64:
*s.buf = strconv.AppendUint(*s.buf, v.Uint64(), 10)
- case Float64Kind:
+ case KindFloat64:
f := v.Float64()
// json.Marshal fails on special floats, so handle them here.
switch {
@@ -124,14 +124,14 @@
return err
}
}
- case BoolKind:
+ case KindBool:
*s.buf = strconv.AppendBool(*s.buf, v.Bool())
- case DurationKind:
+ case KindDuration:
// Do what json.Marshal does.
*s.buf = strconv.AppendInt(*s.buf, int64(v.Duration()), 10)
- case TimeKind:
+ case KindTime:
s.appendTime(v.Time())
- case AnyKind:
+ case KindAny:
a := v.Any()
if err, ok := a.(error); ok {
s.appendString(err.Error())
diff --git a/slog/json_handler_test.go b/slog/json_handler_test.go
index 635d534..045196f 100644
--- a/slog/json_handler_test.go
+++ b/slog/json_handler_test.go
@@ -131,7 +131,7 @@
{"time format", HandlerOptions{
ReplaceAttr: func(_ []string, a Attr) Attr {
v := a.Value
- if v.Kind() == TimeKind {
+ if v.Kind() == KindTime {
return String(a.Key, v.Time().Format(rfc3339Millis))
}
if a.Key == "level" {
@@ -143,7 +143,7 @@
{"time unix", HandlerOptions{
ReplaceAttr: func(_ []string, a Attr) Attr {
v := a.Value
- if v.Kind() == TimeKind {
+ if v.Kind() == KindTime {
return Int64(a.Key, v.Time().UnixNano())
}
if a.Key == "level" {
diff --git a/slog/text_handler.go b/slog/text_handler.go
index 191da44..a91e8ac 100644
--- a/slog/text_handler.go
+++ b/slog/text_handler.go
@@ -91,11 +91,11 @@
func appendTextValue(s *handleState, v Value) error {
switch v.Kind() {
- case StringKind:
+ case KindString:
s.appendString(v.str())
- case TimeKind:
+ case KindTime:
s.appendTime(v.time())
- case AnyKind:
+ case KindAny:
if tm, ok := v.any.(encoding.TextMarshaler); ok {
data, err := tm.MarshalText()
if err != nil {
diff --git a/slog/value.go b/slog/value.go
index 7408f4d..4e928b9 100644
--- a/slog/value.go
+++ b/slog/value.go
@@ -19,24 +19,20 @@
// Kind is the kind of a Value.
type Kind int
-// Unexported version of Kind, just so we can store Kinds in Values.
-// (No user-provided value has this type.)
-type kind Kind
-
// The following list is sorted alphabetically, but it's also important that
-// AnyKind is 0 so that a zero Value represents nil.
+// KindAny is 0 so that a zero Value represents nil.
const (
- AnyKind Kind = iota
- BoolKind
- DurationKind
- Float64Kind
- Int64Kind
- StringKind
- TimeKind
- Uint64Kind
- GroupKind
- LogValuerKind
+ KindAny Kind = iota
+ KindBool
+ KindDuration
+ KindFloat64
+ KindInt64
+ KindString
+ KindTime
+ KindUint64
+ KindGroup
+ KindLogValuer
)
var kindStrings = []string{
@@ -48,7 +44,7 @@
"String",
"Time",
"Uint64",
- "GroupKind",
+ "Group",
"LogValuer",
}
@@ -59,6 +55,10 @@
return "<unknown slog.Kind>"
}
+// Unexported version of Kind, just so we can store Kinds in Values.
+// (No user-provided value has this type.)
+type kind Kind
+
//////////////// Constructors
// IntValue returns a Value for an int.
@@ -68,17 +68,17 @@
// Int64Value returns a Value for an int64.
func Int64Value(v int64) Value {
- return Value{num: uint64(v), any: Int64Kind}
+ return Value{num: uint64(v), any: KindInt64}
}
// Uint64Value returns a Value for a uint64.
func Uint64Value(v uint64) Value {
- return Value{num: v, any: Uint64Kind}
+ return Value{num: v, any: KindUint64}
}
// Float64Value returns a Value for a floating-point number.
func Float64Value(v float64) Value {
- return Value{num: math.Float64bits(v), any: Float64Kind}
+ return Value{num: math.Float64bits(v), any: KindFloat64}
}
// BoolValue returns a Value for a bool.
@@ -87,7 +87,7 @@
if v {
u = 1
}
- return Value{num: u, any: BoolKind}
+ return Value{num: u, any: KindBool}
}
// Unexported version of *time.Location, just so we can store *time.Locations in
@@ -102,7 +102,7 @@
// DurationValue returns a Value for a time.Duration.
func DurationValue(v time.Duration) Value {
- return Value{num: uint64(v.Nanoseconds()), any: DurationKind}
+ return Value{num: uint64(v.Nanoseconds()), any: KindDuration}
}
// GroupValue returns a new Value for a list of Attrs.
@@ -122,10 +122,10 @@
// original numeric type is not preserved.
//
// Given a time.Time or time.Duration value, AnyValue returns a Value of kind
-// TimeKind or DurationKind. The monotonic time is not preserved.
+// KindTime or KindDuration. The monotonic time is not preserved.
//
// For nil, or values of all other types, including named types whose
-// underlying type is numeric, AnyValue returns a value of kind AnyKind.
+// underlying type is numeric, AnyValue returns a value of kind KindAny.
func AnyValue(v any) Value {
switch v := v.(type) {
case string:
@@ -178,24 +178,24 @@
// Any returns v's value as an any.
func (v Value) Any() any {
switch v.Kind() {
- case AnyKind, GroupKind, LogValuerKind:
+ case KindAny, KindGroup, KindLogValuer:
if k, ok := v.any.(kind); ok {
return Kind(k)
}
return v.any
- case Int64Kind:
+ case KindInt64:
return int64(v.num)
- case Uint64Kind:
+ case KindUint64:
return v.num
- case Float64Kind:
+ case KindFloat64:
return v.float()
- case StringKind:
+ case KindString:
return v.str()
- case BoolKind:
+ case KindBool:
return v.bool()
- case DurationKind:
+ case KindDuration:
return v.duration()
- case TimeKind:
+ case KindTime:
return v.time()
default:
panic(fmt.Sprintf("bad kind: %s", v.Kind()))
@@ -205,7 +205,7 @@
// Int64 returns v's value as an int64. It panics
// if v is not a signed integer.
func (v Value) Int64() int64 {
- if g, w := v.Kind(), Int64Kind; g != w {
+ if g, w := v.Kind(), KindInt64; g != w {
panic(fmt.Sprintf("Value kind is %s, not %s", g, w))
}
return int64(v.num)
@@ -214,7 +214,7 @@
// Uint64 returns v's value as a uint64. It panics
// if v is not an unsigned integer.
func (v Value) Uint64() uint64 {
- if g, w := v.Kind(), Uint64Kind; g != w {
+ if g, w := v.Kind(), KindUint64; g != w {
panic(fmt.Sprintf("Value kind is %s, not %s", g, w))
}
return v.num
@@ -223,7 +223,7 @@
// Bool returns v's value as a bool. It panics
// if v is not a bool.
func (v Value) Bool() bool {
- if g, w := v.Kind(), BoolKind; g != w {
+ if g, w := v.Kind(), KindBool; g != w {
panic(fmt.Sprintf("Value kind is %s, not %s", g, w))
}
return v.bool()
@@ -236,7 +236,7 @@
// Duration returns v's value as a time.Duration. It panics
// if v is not a time.Duration.
func (a Value) Duration() time.Duration {
- if g, w := a.Kind(), DurationKind; g != w {
+ if g, w := a.Kind(), KindDuration; g != w {
panic(fmt.Sprintf("Value kind is %s, not %s", g, w))
}
@@ -250,7 +250,7 @@
// Float64 returns v's value as a float64. It panics
// if v is not a float64.
func (v Value) Float64() float64 {
- if g, w := v.Kind(), Float64Kind; g != w {
+ if g, w := v.Kind(), KindFloat64; g != w {
panic(fmt.Sprintf("Value kind is %s, not %s", g, w))
}
@@ -264,7 +264,7 @@
// Time returns v's value as a time.Time. It panics
// if v is not a time.Time.
func (v Value) Time() time.Time {
- if g, w := v.Kind(), TimeKind; g != w {
+ if g, w := v.Kind(), KindTime; g != w {
panic(fmt.Sprintf("Value kind is %s, not %s", g, w))
}
return v.time()
@@ -281,7 +281,7 @@
}
// Group returns v's value as a []Attr.
-// It panics if v's Kind is not GroupKind.
+// It panics if v's Kind is not KindGroup.
func (v Value) Group() []Attr {
return v.group()
}
@@ -296,17 +296,17 @@
return false
}
switch k1 {
- case Int64Kind, Uint64Kind, BoolKind, DurationKind:
+ case KindInt64, KindUint64, KindBool, KindDuration:
return v.num == w.num
- case StringKind:
+ case KindString:
return v.str() == w.str()
- case Float64Kind:
+ case KindFloat64:
return v.float() == w.float()
- case TimeKind:
+ case KindTime:
return v.time().Equal(w.time())
- case AnyKind, LogValuerKind:
+ case KindAny, KindLogValuer:
return v.any == w.any // may panic if non-comparable
- case GroupKind:
+ case KindGroup:
return slices.EqualFunc(v.uncheckedGroup(), w.uncheckedGroup(), Attr.Equal)
default:
panic(fmt.Sprintf("bad kind: %s", k1))
@@ -317,21 +317,21 @@
// v is formatted as with fmt.Sprint.
func (v Value) append(dst []byte) []byte {
switch v.Kind() {
- case StringKind:
+ case KindString:
return append(dst, v.str()...)
- case Int64Kind:
+ case KindInt64:
return strconv.AppendInt(dst, int64(v.num), 10)
- case Uint64Kind:
+ case KindUint64:
return strconv.AppendUint(dst, v.num, 10)
- case Float64Kind:
+ case KindFloat64:
return strconv.AppendFloat(dst, v.float(), 'g', -1, 64)
- case BoolKind:
+ case KindBool:
return strconv.AppendBool(dst, v.bool())
- case DurationKind:
+ case KindDuration:
return append(dst, v.duration().String()...)
- case TimeKind:
+ case KindTime:
return append(dst, v.time().String()...)
- case AnyKind, GroupKind, LogValuerKind:
+ case KindAny, KindGroup, KindLogValuer:
return append(dst, fmt.Sprint(v.any)...)
default:
panic(fmt.Sprintf("bad kind: %s", v.Kind()))
@@ -352,11 +352,11 @@
// and returns the result.
// If the number of LogValue calls exceeds a threshold, a Value containing an
// error is returned.
-// Resolve's return value is guaranteed not to be of Kind LogValuerKind.
+// Resolve's return value is guaranteed not to be of Kind KindLogValuer.
func (v Value) Resolve() Value {
orig := v
for i := 0; i < maxLogValues; i++ {
- if v.Kind() != LogValuerKind {
+ if v.Kind() != KindLogValuer {
return v
}
v = v.LogValuer().LogValue()
diff --git a/slog/value_access_benchmark_test.go b/slog/value_access_benchmark_test.go
index f92d281..3bd7071 100644
--- a/slog/value_access_benchmark_test.go
+++ b/slog/value_access_benchmark_test.go
@@ -39,19 +39,19 @@
for i := 0; i < b.N; i++ {
for _, v := range vs {
switch v.Kind() {
- case StringKind:
+ case KindString:
s = v.String()
- case Int64Kind:
+ case KindInt64:
ii = v.Int64()
- case Uint64Kind:
+ case KindUint64:
u = v.Uint64()
- case Float64Kind:
+ case KindFloat64:
f = v.Float64()
- case BoolKind:
+ case KindBool:
bb = v.Bool()
- case DurationKind:
+ case KindDuration:
d = v.Duration()
- case AnyKind:
+ case KindAny:
a = v.Any()
default:
panic("bad kind")
@@ -132,49 +132,49 @@
// the kind again. See BenchmarkDispatch above.
func (a Value) AsString() (string, bool) {
- if a.Kind() == StringKind {
+ if a.Kind() == KindString {
return a.str(), true
}
return "", false
}
func (a Value) AsInt64() (int64, bool) {
- if a.Kind() == Int64Kind {
+ if a.Kind() == KindInt64 {
return int64(a.num), true
}
return 0, false
}
func (a Value) AsUint64() (uint64, bool) {
- if a.Kind() == Uint64Kind {
+ if a.Kind() == KindUint64 {
return a.num, true
}
return 0, false
}
func (a Value) AsFloat64() (float64, bool) {
- if a.Kind() == Float64Kind {
+ if a.Kind() == KindFloat64 {
return a.float(), true
}
return 0, false
}
func (a Value) AsBool() (bool, bool) {
- if a.Kind() == BoolKind {
+ if a.Kind() == KindBool {
return a.bool(), true
}
return false, false
}
func (a Value) AsDuration() (time.Duration, bool) {
- if a.Kind() == DurationKind {
+ if a.Kind() == KindDuration {
return a.duration(), true
}
return 0, false
}
func (a Value) AsAny() (any, bool) {
- if a.Kind() == AnyKind {
+ if a.Kind() == KindAny {
return a.any, true
}
return nil, false
@@ -195,19 +195,19 @@
func (a Value) Visit(v Visitor) {
switch a.Kind() {
- case StringKind:
+ case KindString:
v.String(a.str())
- case Int64Kind:
+ case KindInt64:
v.Int64(int64(a.num))
- case Uint64Kind:
+ case KindUint64:
v.Uint64(a.num)
- case BoolKind:
+ case KindBool:
v.Bool(a.bool())
- case Float64Kind:
+ case KindFloat64:
v.Float64(a.float())
- case DurationKind:
+ case KindDuration:
v.Duration(a.duration())
- case AnyKind:
+ case KindAny:
v.Any(a.any)
default:
panic("bad kind")
diff --git a/slog/value_safe.go b/slog/value_safe.go
index 38d5f97..7f50c1e 100644
--- a/slog/value_safe.go
+++ b/slog/value_safe.go
@@ -13,9 +13,9 @@
// The zero Value corresponds to nil.
type Value struct {
// num holds the value for Kinds Int64, Uint64, Float64, Bool and Duration,
- // and nanoseconds since the epoch for TimeKind.
+ // and nanoseconds since the epoch for KindTime.
num uint64
- // s holds the value for StringKind.
+ // s holds the value for KindString.
s string
// If any is of type Kind, then the value is in num or s as described above.
// If any is of type *time.Location, then the Kind is Time and time.Time
@@ -32,15 +32,15 @@
case Kind:
return k
case timeLocation:
- return TimeKind
+ return KindTime
case []Attr:
- return GroupKind
+ return KindGroup
case LogValuer:
- return LogValuerKind
+ return KindLogValuer
case kind: // a kind is just a wrapper for a Kind
- return AnyKind
+ return KindAny
default:
- return AnyKind
+ return KindAny
}
}
@@ -50,14 +50,14 @@
// String returns a new Value for a string.
func StringValue(value string) Value {
- return Value{s: value, any: StringKind}
+ return Value{s: value, any: KindString}
}
// String returns Value's value as a string, formatted like fmt.Sprint. Unlike
// the methods Int64, Float64, and so on, which panic if v is of the
// wrong kind, String never panics.
func (v Value) String() string {
- if v.Kind() == StringKind {
+ if v.Kind() == KindString {
return v.str()
}
var buf []byte
diff --git a/slog/value_test.go b/slog/value_test.go
index f3b0db3..31403ca 100644
--- a/slog/value_test.go
+++ b/slog/value_test.go
@@ -133,7 +133,7 @@
}
})
t.Run("Kind", func(t *testing.T) {
- want := BoolKind
+ want := KindBool
got := AnyValue(want).Any()
if got != want {
t.Errorf("got %v, want %v", got, want)
@@ -168,7 +168,7 @@
want := "replaced"
r := &replace{StringValue(want)}
v := AnyValue(r)
- if g, w := v.Kind(), LogValuerKind; g != w {
+ if g, w := v.Kind(), KindLogValuer; g != w {
t.Errorf("got %s, want %s", g, w)
}
got := v.LogValuer().LogValue().Any()
diff --git a/slog/value_unsafe.go b/slog/value_unsafe.go
index f8e50cc..8e23058 100644
--- a/slog/value_unsafe.go
+++ b/slog/value_unsafe.go
@@ -18,7 +18,7 @@
// The zero Value corresponds to nil.
type Value struct {
// num holds the value for Kinds Int64, Uint64, Float64, Bool and Duration,
- // the string length for StringKind, and nanoseconds since the epoch for TimeKind.
+ // the string length for KindString, and nanoseconds since the epoch for KindTime.
num uint64
// If any is of type Kind, then the value is in num as described above.
// If any is of type *time.Location, then the Kind is Time and time.Time value
@@ -43,17 +43,17 @@
case Kind:
return x
case stringptr:
- return StringKind
+ return KindString
case timeLocation:
- return TimeKind
+ return KindTime
case groupptr:
- return GroupKind
+ return KindGroup
case LogValuer:
- return LogValuerKind
+ return KindLogValuer
case kind: // a kind is just a wrapper for a Kind
- return AnyKind
+ return KindAny
default:
- return AnyKind
+ return KindAny
}
}
@@ -93,7 +93,7 @@
}
// Group returns the Value's value as a []Attr.
-// It panics if the Value's Kind is not GroupKind.
+// It panics if the Value's Kind is not KindGroup.
func (v Value) group() []Attr {
if sp, ok := v.any.(groupptr); ok {
return unsafe.Slice((*Attr)(sp), v.num)