Skip to content

Commit 6c6f92a

Browse files
authored
feat: replace interface{} with any (#136)
Signed-off-by: Manuel Alonso <[email protected]>
1 parent c54895a commit 6c6f92a

File tree

15 files changed

+114
-114
lines changed

15 files changed

+114
-114
lines changed

config/decode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type StringDecoder interface {
4444
//
4545
// Most of the heavy lifting is handled by the mapstructure library. A custom decoder is used to handle
4646
// decoding string values to the supported primitives.
47-
func Decode(input interface{}, output interface{}) error {
47+
func Decode(input any, output any) error {
4848
decoder, err := mapstructure.NewDecoder(
4949
&mapstructure.DecoderConfig{ //nolint: exhaustruct
5050
Result: output,

config/decode_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ func (u *Decoded) DecodeString(text string) error {
9292

9393
func TestDecode(t *testing.T) {
9494
timeVal := getTimeVal()
95-
tests := map[string]interface{}{
96-
"primitive values": map[string]interface{}{
95+
tests := map[string]any{
96+
"primitive values": map[string]any{
9797
"int": -9999,
9898
"intPtr": ptr.Of(-9999),
9999
"int64": -1234,
@@ -128,16 +128,16 @@ func TestDecode(t *testing.T) {
128128
"stringPtr": ptr.Of("1234"),
129129
"decoded": "unlimited",
130130
"decodedPtr": "unlimited",
131-
"nested": map[string]interface{}{
131+
"nested": map[string]any{
132132
"integer": 1234,
133133
"string": 5678,
134134
},
135-
"nestedPtr": map[string]interface{}{
135+
"nestedPtr": map[string]any{
136136
"integer": 1234,
137137
"string": 5678,
138138
},
139139
},
140-
"string values": map[string]interface{}{
140+
"string values": map[string]any{
141141
"int": "-9999",
142142
"intPtr": "-9999",
143143
"int64": "-1234",
@@ -196,7 +196,7 @@ func TestDecode(t *testing.T) {
196196

197197
func TestDecodeErrors(t *testing.T) {
198198
var actual testConfig
199-
err := config.Decode(map[string]interface{}{
199+
err := config.Decode(map[string]any{
200200
"int": "-badval",
201201
"intPtr": "-badval",
202202
"int64": "-badval",

config/normalize.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import (
2121
// for JSON and usage in component initialization.
2222
//
2323
//nolint:cyclop
24-
func Normalize(i interface{}) (interface{}, error) {
24+
func Normalize(i any) (any, error) {
2525
var err error
2626
switch x := i.(type) {
27-
case map[interface{}]interface{}:
28-
m2 := map[string]interface{}{}
27+
case map[any]any:
28+
m2 := map[string]any{}
2929
for k, v := range x {
3030
if strKey, ok := k.(string); ok {
3131
if m2[strKey], err = Normalize(v); err != nil {
@@ -37,16 +37,16 @@ func Normalize(i interface{}) (interface{}, error) {
3737
}
3838

3939
return m2, nil
40-
case map[string]interface{}:
41-
m2 := map[string]interface{}{}
40+
case map[string]any:
41+
m2 := map[string]any{}
4242
for k, v := range x {
4343
if m2[k], err = Normalize(v); err != nil {
4444
return nil, err
4545
}
4646
}
4747

4848
return m2, nil
49-
case []interface{}:
49+
case []any:
5050
for i, v := range x {
5151
if x[i], err = Normalize(v); err != nil {
5252
return nil, err

config/normalize_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,67 +24,67 @@ import (
2424

2525
func TestNormalize(t *testing.T) {
2626
tests := map[string]struct {
27-
input interface{}
28-
expected interface{}
27+
input any
28+
expected any
2929
err string
3030
}{
3131
"simple": {input: "test", expected: "test"},
3232
"map of string to interface{}": {
33-
input: map[string]interface{}{
33+
input: map[string]any{
3434
"test": "1234",
35-
"nested": map[string]interface{}{
35+
"nested": map[string]any{
3636
"value": "5678",
3737
},
38-
}, expected: map[string]interface{}{
38+
}, expected: map[string]any{
3939
"test": "1234",
40-
"nested": map[string]interface{}{
40+
"nested": map[string]any{
4141
"value": "5678",
4242
},
4343
},
4444
},
4545
"map of string to interface{} with error": {
46-
input: map[string]interface{}{
46+
input: map[string]any{
4747
"test": "1234",
48-
"nested": map[interface{}]interface{}{
48+
"nested": map[any]any{
4949
5678: "5678",
5050
},
5151
}, err: "error parsing config field: 5678",
5252
},
5353
"map of interface{} to interface{}": {
54-
input: map[string]interface{}{
54+
input: map[string]any{
5555
"test": "1234",
56-
"nested": map[interface{}]interface{}{
56+
"nested": map[any]any{
5757
"value": "5678",
5858
},
59-
}, expected: map[string]interface{}{
59+
}, expected: map[string]any{
6060
"test": "1234",
61-
"nested": map[string]interface{}{
61+
"nested": map[string]any{
6262
"value": "5678",
6363
},
6464
},
6565
},
6666
"map of interface{} to interface{} with error": {
67-
input: map[interface{}]interface{}{
67+
input: map[any]any{
6868
"test": "1234",
69-
"nested": map[interface{}]interface{}{
69+
"nested": map[any]any{
7070
5678: "5678",
7171
},
7272
}, err: "error parsing config field: 5678",
7373
},
7474
"slice of interface{}": {
75-
input: []interface{}{
76-
map[interface{}]interface{}{
75+
input: []any{
76+
map[any]any{
7777
"value": "5678",
7878
},
79-
}, expected: []interface{}{
80-
map[string]interface{}{
79+
}, expected: []any{
80+
map[string]any{
8181
"value": "5678",
8282
},
8383
},
8484
},
8585
"slice of interface{} with error": {
86-
input: []interface{}{
87-
map[interface{}]interface{}{
86+
input: []any{
87+
map[any]any{
8888
1234: "1234",
8989
},
9090
}, err: "error parsing config field: 1234",

config/prefix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"unicode"
1919
)
2020

21-
func PrefixedBy(input interface{}, prefix string) (interface{}, error) {
21+
func PrefixedBy(input any, prefix string) (any, error) {
2222
normalized, err := Normalize(input)
2323
if err != nil {
2424
// The only error that can come from normalize is if
@@ -28,8 +28,8 @@ func PrefixedBy(input interface{}, prefix string) (interface{}, error) {
2828
}
2929
input = normalized
3030

31-
if inputMap, ok := input.(map[string]interface{}); ok {
32-
converted := make(map[string]interface{}, len(inputMap))
31+
if inputMap, ok := input.(map[string]any); ok {
32+
converted := make(map[string]any, len(inputMap))
3333
for k, v := range inputMap {
3434
if strings.HasPrefix(k, prefix) {
3535
key := uncapitalize(strings.TrimPrefix(k, prefix))

config/prefix_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
func TestPrefixedBy(t *testing.T) {
2626
tests := map[string]struct {
2727
prefix string
28-
input interface{}
29-
expected interface{}
28+
input any
29+
expected any
3030
err string
3131
}{
3232
"map of string to string": {
@@ -44,13 +44,13 @@ func TestPrefixedBy(t *testing.T) {
4444
},
4545
"map of string to interface{}": {
4646
prefix: "test",
47-
input: map[string]interface{}{
47+
input: map[string]any{
4848
"": "",
4949
"ignore": "don't include me",
5050
"testOne": "include me",
5151
"testTwo": "and me",
5252
},
53-
expected: map[string]interface{}{
53+
expected: map[string]any{
5454
"one": "include me",
5555
"two": "and me",
5656
},

cron/logger.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ You can check the original license at:
1818
package cron
1919

2020
import (
21-
"io/ioutil"
21+
"io"
2222
"log"
2323
"os"
2424
"strings"
@@ -29,48 +29,48 @@ import (
2929
var DefaultLogger Logger = PrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))
3030

3131
// DiscardLogger can be used by callers to discard all log messages.
32-
var DiscardLogger Logger = PrintfLogger(log.New(ioutil.Discard, "", 0))
32+
var DiscardLogger Logger = PrintfLogger(log.New(io.Discard, "", 0))
3333

3434
// Logger is the interface used in this package for logging, so that any backend
3535
// can be plugged in. It is a subset of the github.com/go-logr/logr interface.
3636
type Logger interface {
3737
// Info logs routine messages about cron's operation.
38-
Info(msg string, keysAndValues ...interface{})
38+
Info(msg string, keysAndValues ...any)
3939
// Error logs an error condition.
40-
Error(err error, msg string, keysAndValues ...interface{})
40+
Error(err error, msg string, keysAndValues ...any)
4141
}
4242

4343
// PrintfLogger wraps a Printf-based logger (such as the standard library "log")
4444
// into an implementation of the Logger interface which logs errors only.
45-
func PrintfLogger(l interface{ Printf(string, ...interface{}) }) Logger {
45+
func PrintfLogger(l interface{ Printf(string, ...any) }) Logger {
4646
return printfLogger{l, false}
4747
}
4848

4949
// VerbosePrintfLogger wraps a Printf-based logger (such as the standard library
5050
// "log") into an implementation of the Logger interface which logs everything.
51-
func VerbosePrintfLogger(l interface{ Printf(string, ...interface{}) }) Logger {
51+
func VerbosePrintfLogger(l interface{ Printf(string, ...any) }) Logger {
5252
return printfLogger{l, true}
5353
}
5454

5555
type printfLogger struct {
56-
logger interface{ Printf(string, ...interface{}) }
56+
logger interface{ Printf(string, ...any) }
5757
logInfo bool
5858
}
5959

60-
func (pl printfLogger) Info(msg string, keysAndValues ...interface{}) {
60+
func (pl printfLogger) Info(msg string, keysAndValues ...any) {
6161
if pl.logInfo {
6262
keysAndValues = formatTimes(keysAndValues)
6363
pl.logger.Printf(
6464
formatString(len(keysAndValues)),
65-
append([]interface{}{msg}, keysAndValues...)...)
65+
append([]any{msg}, keysAndValues...)...)
6666
}
6767
}
6868

69-
func (pl printfLogger) Error(err error, msg string, keysAndValues ...interface{}) {
69+
func (pl printfLogger) Error(err error, msg string, keysAndValues ...any) {
7070
keysAndValues = formatTimes(keysAndValues)
7171
pl.logger.Printf(
7272
formatString(len(keysAndValues)+2),
73-
append([]interface{}{msg, "error", err}, keysAndValues...)...)
73+
append([]any{msg, "error", err}, keysAndValues...)...)
7474
}
7575

7676
// formatString returns a logfmt-like format string for the number of
@@ -91,8 +91,8 @@ func formatString(numKeysAndValues int) string {
9191
}
9292

9393
// formatTimes formats any time.Time values as RFC3339.
94-
func formatTimes(keysAndValues []interface{}) []interface{} {
95-
var formattedArgs []interface{}
94+
func formatTimes(keysAndValues []any) []any {
95+
var formattedArgs []any
9696
for _, arg := range keysAndValues {
9797
if t, ok := arg.(time.Time); ok {
9898
arg = t.Format(time.RFC3339)

0 commit comments

Comments
 (0)