Skip to content

Commit 1d04c54

Browse files
authored
refactor: change interface{} to any (#181)
1 parent d5a93ab commit 1d04c54

File tree

15 files changed

+127
-127
lines changed

15 files changed

+127
-127
lines changed

json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88
)
99

10-
func (l *Logger) jsonFormatter(keyvals ...interface{}) {
10+
func (l *Logger) jsonFormatter(keyvals ...any) {
1111
jw := &jsonWriter{w: &l.b}
1212
jw.start()
1313

json_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func TestJson(t *testing.T) {
1919
name string
2020
expected string
2121
msg string
22-
kvs []interface{}
23-
f func(msg interface{}, kvs ...interface{})
22+
kvs []any
23+
f func(msg any, kvs ...any)
2424
}{
2525
{
2626
name: "default logger info with timestamp",
@@ -54,70 +54,70 @@ func TestJson(t *testing.T) {
5454
name: "multiline kvs",
5555
expected: "{\"level\":\"error\",\"msg\":\"info\",\"multiline\":\"info\\ninfo\"}\n",
5656
msg: "info",
57-
kvs: []interface{}{"multiline", "info\ninfo"},
57+
kvs: []any{"multiline", "info\ninfo"},
5858
f: l.Error,
5959
},
6060
{
6161
name: "odd number of kvs",
6262
expected: "{\"level\":\"error\",\"msg\":\"info\",\"foo\":\"bar\",\"baz\":\"missing value\"}\n",
6363
msg: "info",
64-
kvs: []interface{}{"foo", "bar", "baz"},
64+
kvs: []any{"foo", "bar", "baz"},
6565
f: l.Error,
6666
},
6767
{
6868
name: "error field",
6969
expected: "{\"level\":\"error\",\"msg\":\"info\",\"error\":\"error message\"}\n",
7070
msg: "info",
71-
kvs: []interface{}{"error", errors.New("error message")},
71+
kvs: []any{"error", errors.New("error message")},
7272
f: l.Error,
7373
},
7474
{
7575
name: "struct field",
7676
expected: "{\"level\":\"info\",\"msg\":\"info\",\"struct\":{}}\n",
7777
msg: "info",
78-
kvs: []interface{}{"struct", struct{ foo string }{foo: "bar"}},
78+
kvs: []any{"struct", struct{ foo string }{foo: "bar"}},
7979
f: l.Info,
8080
},
8181
{
8282
name: "slice field",
8383
expected: "{\"level\":\"info\",\"msg\":\"info\",\"slice\":[1,2,3]}\n",
8484
msg: "info",
85-
kvs: []interface{}{"slice", []int{1, 2, 3}},
85+
kvs: []any{"slice", []int{1, 2, 3}},
8686
f: l.Info,
8787
},
8888
{
8989
name: "slice of structs",
9090
expected: "{\"level\":\"info\",\"msg\":\"info\",\"slice\":[{},{}]}\n",
9191
msg: "info",
92-
kvs: []interface{}{"slice", []struct{ foo string }{{foo: "bar"}, {foo: "baz"}}},
92+
kvs: []any{"slice", []struct{ foo string }{{foo: "bar"}, {foo: "baz"}}},
9393
f: l.Info,
9494
},
9595
{
9696
name: "slice of strings",
9797
expected: "{\"level\":\"info\",\"msg\":\"info\",\"slice\":[\"foo\",\"bar\"]}\n",
9898
msg: "info",
99-
kvs: []interface{}{"slice", []string{"foo", "bar"}},
99+
kvs: []any{"slice", []string{"foo", "bar"}},
100100
f: l.Info,
101101
},
102102
{
103103
name: "slice of errors",
104104
expected: "{\"level\":\"info\",\"msg\":\"info\",\"slice\":[{},{}]}\n",
105105
msg: "info",
106-
kvs: []interface{}{"slice", []error{errors.New("error message1"), errors.New("error message2")}},
106+
kvs: []any{"slice", []error{errors.New("error message1"), errors.New("error message2")}},
107107
f: l.Info,
108108
},
109109
{
110110
name: "map of strings",
111111
expected: "{\"level\":\"info\",\"msg\":\"info\",\"map\":{\"a\":\"b\",\"foo\":\"bar\"}}\n",
112112
msg: "info",
113-
kvs: []interface{}{"map", map[string]string{"a": "b", "foo": "bar"}},
113+
kvs: []any{"map", map[string]string{"a": "b", "foo": "bar"}},
114114
f: l.Info,
115115
},
116116
{
117117
name: "slog any value error type",
118118
expected: "{\"level\":\"info\",\"msg\":\"info\",\"error\":\"error message\"}\n",
119119
msg: "info",
120-
kvs: []interface{}{"error", slogAnyValue(fmt.Errorf("error message"))},
120+
kvs: []any{"error", slogAnyValue(fmt.Errorf("error message"))},
121121
f: l.Info,
122122
},
123123
}
@@ -141,9 +141,9 @@ func TestJsonCaller(t *testing.T) {
141141
name string
142142
expected string
143143
msg string
144-
kvs []interface{}
144+
kvs []any
145145

146-
f func(msg interface{}, kvs ...interface{})
146+
f func(msg any, kvs ...any)
147147
}{
148148
{
149149
name: "simple caller",
@@ -157,7 +157,7 @@ func TestJsonCaller(t *testing.T) {
157157
expected: fmt.Sprintf("{\"level\":\"info\",\"caller\":\"log/%s:%d\",\"msg\":\"info\"}\n", filepath.Base(file), line+30),
158158
msg: "info",
159159
kvs: nil,
160-
f: func(msg interface{}, kvs ...interface{}) {
160+
f: func(msg any, kvs ...any) {
161161
l.Helper()
162162
l.Info(msg, kvs...)
163163
},

logfmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/go-logfmt/logfmt"
99
)
1010

11-
func (l *Logger) logfmtFormatter(keyvals ...interface{}) {
11+
func (l *Logger) logfmtFormatter(keyvals ...any) {
1212
e := logfmt.NewEncoder(&l.b)
1313

1414
for i := 0; i < len(keyvals); i += 2 {

logfmt_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func TestLogfmt(t *testing.T) {
1616
name string
1717
expected string
1818
msg string
19-
kvs []interface{}
20-
f func(msg interface{}, kvs ...interface{})
19+
kvs []any
20+
f func(msg any, kvs ...any)
2121
}{
2222
{
2323
name: "simple",
@@ -37,14 +37,14 @@ func TestLogfmt(t *testing.T) {
3737
name: "message with keyvals",
3838
expected: "level=info msg=info foo=bar\n",
3939
msg: "info",
40-
kvs: []interface{}{"foo", "bar"},
40+
kvs: []any{"foo", "bar"},
4141
f: l.Info,
4242
},
4343
{
4444
name: "message with multiline keyvals",
4545
expected: "level=info msg=info foo=\"bar\\nbaz\"\n",
4646
msg: "info",
47-
kvs: []interface{}{"foo", "bar\nbaz"},
47+
kvs: []any{"foo", "bar\nbaz"},
4848
f: l.Info,
4949
},
5050
{
@@ -58,63 +58,63 @@ func TestLogfmt(t *testing.T) {
5858
name: "message with error",
5959
expected: "level=info msg=info err=\"foo: bar\"\n",
6060
msg: "info",
61-
kvs: []interface{}{"err", errors.New("foo: bar")},
61+
kvs: []any{"err", errors.New("foo: bar")},
6262
f: l.Info,
6363
},
6464
{
6565
name: "odd number of keyvals",
6666
expected: "level=info msg=info foo=bar baz=\"missing value\"\n",
6767
msg: "info",
68-
kvs: []interface{}{"foo", "bar", "baz"},
68+
kvs: []any{"foo", "bar", "baz"},
6969
f: l.Info,
7070
},
7171
{
7272
name: "struct field",
7373
expected: "level=info msg=info foo=\"{bar:foo bar}\"\n",
7474
msg: "info",
75-
kvs: []interface{}{"foo", struct{ bar string }{"foo bar"}},
75+
kvs: []any{"foo", struct{ bar string }{"foo bar"}},
7676
f: l.Info,
7777
},
7878
{
7979
name: "multiple struct fields",
8080
expected: "level=info msg=info foo={bar:baz} foo2={bar:baz}\n",
8181
msg: "info",
82-
kvs: []interface{}{"foo", struct{ bar string }{"baz"}, "foo2", struct{ bar string }{"baz"}},
82+
kvs: []any{"foo", struct{ bar string }{"baz"}, "foo2", struct{ bar string }{"baz"}},
8383
f: l.Info,
8484
},
8585
{
8686
name: "slice of structs",
8787
expected: "level=info msg=info foo=\"[{bar:baz} {bar:baz}]\"\n",
8888
msg: "info",
89-
kvs: []interface{}{"foo", []struct{ bar string }{{"baz"}, {"baz"}}},
89+
kvs: []any{"foo", []struct{ bar string }{{"baz"}, {"baz"}}},
9090
f: l.Info,
9191
},
9292
{
9393
name: "slice of strings",
9494
expected: "level=info msg=info foo=\"[bar baz]\"\n",
9595
msg: "info",
96-
kvs: []interface{}{"foo", []string{"bar", "baz"}},
96+
kvs: []any{"foo", []string{"bar", "baz"}},
9797
f: l.Info,
9898
},
9999
{
100100
name: "slice of errors",
101101
expected: "level=info msg=info foo=\"[error1 error2]\"\n",
102102
msg: "info",
103-
kvs: []interface{}{"foo", []error{errors.New("error1"), errors.New("error2")}},
103+
kvs: []any{"foo", []error{errors.New("error1"), errors.New("error2")}},
104104
f: l.Info,
105105
},
106106
{
107107
name: "map of strings",
108108
expected: "level=info msg=info foo=map[bar:baz]\n",
109109
msg: "info",
110-
kvs: []interface{}{"foo", map[string]string{"bar": "baz"}},
110+
kvs: []any{"foo", map[string]string{"bar": "baz"}},
111111
f: l.Info,
112112
},
113113
{
114114
name: "map with interface",
115115
expected: "level=info msg=info foo=map[bar:baz]\n",
116116
msg: "info",
117-
kvs: []interface{}{"foo", map[string]interface{}{"bar": "baz"}},
117+
kvs: []any{"foo", map[string]any{"bar": "baz"}},
118118
f: l.Info,
119119
},
120120
}

logger.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ type Logger struct {
4141
reportCaller bool
4242
reportTimestamp bool
4343

44-
fields []interface{}
44+
fields []any
4545

4646
helpers *sync.Map
4747
styles *Styles
4848
}
4949

5050
// Logf logs a message with formatting.
51-
func (l *Logger) Logf(level Level, format string, args ...interface{}) {
51+
func (l *Logger) Logf(level Level, format string, args ...any) {
5252
l.Log(level, fmt.Sprintf(format, args...))
5353
}
5454

5555
// Log logs the given message with the given keyvals for the given level.
56-
func (l *Logger) Log(level Level, msg interface{}, keyvals ...interface{}) {
56+
func (l *Logger) Log(level Level, msg any, keyvals ...any) {
5757
if atomic.LoadUint32(&l.isDiscard) != 0 {
5858
return
5959
}
@@ -81,8 +81,8 @@ func (l *Logger) Log(level Level, msg interface{}, keyvals ...interface{}) {
8181
l.handle(level, l.timeFunc(time.Now()), []runtime.Frame{frame}, msg, keyvals...)
8282
}
8383

84-
func (l *Logger) handle(level Level, ts time.Time, frames []runtime.Frame, msg interface{}, keyvals ...interface{}) {
85-
var kvs []interface{}
84+
func (l *Logger) handle(level Level, ts time.Time, frames []runtime.Frame, msg any, keyvals ...any) {
85+
var kvs []any
8686
if l.reportTimestamp && !ts.IsZero() {
8787
kvs = append(kvs, TimestampKey, ts)
8888
}
@@ -327,7 +327,7 @@ func (l *Logger) SetStyles(s *Styles) {
327327
}
328328

329329
// With returns a new logger with the given keyvals added.
330-
func (l *Logger) With(keyvals ...interface{}) *Logger {
330+
func (l *Logger) With(keyvals ...any) *Logger {
331331
var st Styles
332332
l.mu.Lock()
333333
sl := *l
@@ -336,7 +336,7 @@ func (l *Logger) With(keyvals ...interface{}) *Logger {
336336
sl.b = bytes.Buffer{}
337337
sl.mu = &sync.RWMutex{}
338338
sl.helpers = &sync.Map{}
339-
sl.fields = append(make([]interface{}, 0, len(l.fields)+len(keyvals)), l.fields...)
339+
sl.fields = append(make([]any, 0, len(l.fields)+len(keyvals)), l.fields...)
340340
sl.fields = append(sl.fields, keyvals...)
341341
sl.styles = &st
342342
return &sl
@@ -350,63 +350,63 @@ func (l *Logger) WithPrefix(prefix string) *Logger {
350350
}
351351

352352
// Debug prints a debug message.
353-
func (l *Logger) Debug(msg interface{}, keyvals ...interface{}) {
353+
func (l *Logger) Debug(msg any, keyvals ...any) {
354354
l.Log(DebugLevel, msg, keyvals...)
355355
}
356356

357357
// Info prints an info message.
358-
func (l *Logger) Info(msg interface{}, keyvals ...interface{}) {
358+
func (l *Logger) Info(msg any, keyvals ...any) {
359359
l.Log(InfoLevel, msg, keyvals...)
360360
}
361361

362362
// Warn prints a warning message.
363-
func (l *Logger) Warn(msg interface{}, keyvals ...interface{}) {
363+
func (l *Logger) Warn(msg any, keyvals ...any) {
364364
l.Log(WarnLevel, msg, keyvals...)
365365
}
366366

367367
// Error prints an error message.
368-
func (l *Logger) Error(msg interface{}, keyvals ...interface{}) {
368+
func (l *Logger) Error(msg any, keyvals ...any) {
369369
l.Log(ErrorLevel, msg, keyvals...)
370370
}
371371

372372
// Fatal prints a fatal message and exits.
373-
func (l *Logger) Fatal(msg interface{}, keyvals ...interface{}) {
373+
func (l *Logger) Fatal(msg any, keyvals ...any) {
374374
l.Log(FatalLevel, msg, keyvals...)
375375
os.Exit(1)
376376
}
377377

378378
// Print prints a message with no level.
379-
func (l *Logger) Print(msg interface{}, keyvals ...interface{}) {
379+
func (l *Logger) Print(msg any, keyvals ...any) {
380380
l.Log(noLevel, msg, keyvals...)
381381
}
382382

383383
// Debugf prints a debug message with formatting.
384-
func (l *Logger) Debugf(format string, args ...interface{}) {
384+
func (l *Logger) Debugf(format string, args ...any) {
385385
l.Log(DebugLevel, fmt.Sprintf(format, args...))
386386
}
387387

388388
// Infof prints an info message with formatting.
389-
func (l *Logger) Infof(format string, args ...interface{}) {
389+
func (l *Logger) Infof(format string, args ...any) {
390390
l.Log(InfoLevel, fmt.Sprintf(format, args...))
391391
}
392392

393393
// Warnf prints a warning message with formatting.
394-
func (l *Logger) Warnf(format string, args ...interface{}) {
394+
func (l *Logger) Warnf(format string, args ...any) {
395395
l.Log(WarnLevel, fmt.Sprintf(format, args...))
396396
}
397397

398398
// Errorf prints an error message with formatting.
399-
func (l *Logger) Errorf(format string, args ...interface{}) {
399+
func (l *Logger) Errorf(format string, args ...any) {
400400
l.Log(ErrorLevel, fmt.Sprintf(format, args...))
401401
}
402402

403403
// Fatalf prints a fatal message with formatting and exits.
404-
func (l *Logger) Fatalf(format string, args ...interface{}) {
404+
func (l *Logger) Fatalf(format string, args ...any) {
405405
l.Log(FatalLevel, fmt.Sprintf(format, args...))
406406
os.Exit(1)
407407
}
408408

409409
// Printf prints a message with no level and formatting.
410-
func (l *Logger) Printf(format string, args ...interface{}) {
410+
func (l *Logger) Printf(format string, args ...any) {
411411
l.Log(noLevel, fmt.Sprintf(format, args...))
412412
}

logger_121.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (l *Logger) Handle(ctx context.Context, record slog.Record) error {
3636
return nil
3737
}
3838

39-
fields := make([]interface{}, 0, record.NumAttrs()*2)
39+
fields := make([]any, 0, record.NumAttrs()*2)
4040
record.Attrs(func(a slog.Attr) bool {
4141
fields = append(fields, a.Key, a.Value)
4242
return true
@@ -52,7 +52,7 @@ func (l *Logger) Handle(ctx context.Context, record slog.Record) error {
5252
//
5353
// Implements slog.Handler.
5454
func (l *Logger) WithAttrs(attrs []slog.Attr) slog.Handler {
55-
fields := make([]interface{}, 0, len(attrs)*2)
55+
fields := make([]any, 0, len(attrs)*2)
5656
for _, attr := range attrs {
5757
fields = append(fields, attr.Key, attr.Value)
5858
}

logger_121_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func TestSlogAttr(t *testing.T) {
195195
cases := []struct {
196196
name string
197197
expected string
198-
kvs []interface{}
198+
kvs []any
199199
}{
200200
{
201201
name: "any",

0 commit comments

Comments
 (0)