From 4db965e19f768bca1c741e5dd51a204ad5bc7b88 Mon Sep 17 00:00:00 2001 From: James Elliott Date: Sat, 15 Apr 2023 22:35:44 +1000 Subject: [PATCH] refactor: interfaces (#5252) Use any alias instead of empty interfaces. Signed-off-by: James Elliott --- internal/configuration/decode_hooks.go | 14 +++++------ .../koanf_provider_filtered_file.go | 2 +- internal/configuration/koanf_util.go | 24 +++++++++---------- internal/logging/printf.go | 4 ++-- .../session/encrypting_serializer_test.go | 4 ++-- internal/utils/crypto_test.go | 6 ++--- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/internal/configuration/decode_hooks.go b/internal/configuration/decode_hooks.go index 5f059e8be..f0cb8468f 100644 --- a/internal/configuration/decode_hooks.go +++ b/internal/configuration/decode_hooks.go @@ -260,7 +260,7 @@ func StringToAddressHookFunc() mapstructure.DecodeHookFuncType { // StringToX509CertificateHookFunc decodes strings to x509.Certificate's. func StringToX509CertificateHookFunc() mapstructure.DecodeHookFuncType { - return func(f reflect.Type, t reflect.Type, data any) (value interface{}, err error) { + return func(f reflect.Type, t reflect.Type, data any) (value any, err error) { if f.Kind() != reflect.String { return data, nil } @@ -283,7 +283,7 @@ func StringToX509CertificateHookFunc() mapstructure.DecodeHookFuncType { return result, nil } - var i interface{} + var i any if i, err = utils.ParseX509FromPEM([]byte(dataStr)); err != nil { return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseBasic, "*", expectedType, err) @@ -300,7 +300,7 @@ func StringToX509CertificateHookFunc() mapstructure.DecodeHookFuncType { // StringToX509CertificateChainHookFunc decodes strings to schema.X509CertificateChain's. func StringToX509CertificateChainHookFunc() mapstructure.DecodeHookFuncType { - return func(f reflect.Type, t reflect.Type, data interface{}) (value interface{}, err error) { + return func(f reflect.Type, t reflect.Type, data any) (value any, err error) { var ptr bool if f.Kind() != reflect.String { @@ -348,7 +348,7 @@ func StringToX509CertificateChainHookFunc() mapstructure.DecodeHookFuncType { // StringToTLSVersionHookFunc decodes strings to schema.TLSVersion's. func StringToTLSVersionHookFunc() mapstructure.DecodeHookFuncType { - return func(f reflect.Type, t reflect.Type, data interface{}) (value interface{}, err error) { + return func(f reflect.Type, t reflect.Type, data any) (value any, err error) { var ptr bool if f.Kind() != reflect.String { @@ -388,7 +388,7 @@ func StringToTLSVersionHookFunc() mapstructure.DecodeHookFuncType { // StringToCryptoPrivateKeyHookFunc decodes strings to schema.CryptographicPrivateKey's. func StringToCryptoPrivateKeyHookFunc() mapstructure.DecodeHookFuncType { - return func(f reflect.Type, t reflect.Type, data interface{}) (value interface{}, err error) { + return func(f reflect.Type, t reflect.Type, data any) (value any, err error) { if f.Kind() != reflect.String { return data, nil } @@ -418,7 +418,7 @@ func StringToCryptoPrivateKeyHookFunc() mapstructure.DecodeHookFuncType { // StringToPrivateKeyHookFunc decodes strings to rsa.PrivateKey's. func StringToPrivateKeyHookFunc() mapstructure.DecodeHookFuncType { - return func(f reflect.Type, t reflect.Type, data interface{}) (value interface{}, err error) { + return func(f reflect.Type, t reflect.Type, data any) (value any, err error) { if f.Kind() != reflect.String { return data, nil } @@ -487,7 +487,7 @@ func StringToPrivateKeyHookFunc() mapstructure.DecodeHookFuncType { // StringToPasswordDigestHookFunc decodes a string into a crypt.Digest. func StringToPasswordDigestHookFunc() mapstructure.DecodeHookFuncType { - return func(f reflect.Type, t reflect.Type, data interface{}) (value interface{}, err error) { + return func(f reflect.Type, t reflect.Type, data any) (value any, err error) { var ptr bool if f.Kind() != reflect.String { diff --git a/internal/configuration/koanf_provider_filtered_file.go b/internal/configuration/koanf_provider_filtered_file.go index 17bfc5b1b..b59c5adf5 100644 --- a/internal/configuration/koanf_provider_filtered_file.go +++ b/internal/configuration/koanf_provider_filtered_file.go @@ -50,7 +50,7 @@ func (f *FilteredFile) ReadBytes() (data []byte, err error) { } // Read is not supported by the filtered file koanf.Provider. -func (f *FilteredFile) Read() (map[string]interface{}, error) { +func (f *FilteredFile) Read() (map[string]any, error) { return nil, errors.New("filtered file provider does not support this method") } diff --git a/internal/configuration/koanf_util.go b/internal/configuration/koanf_util.go index 351048937..b08320e41 100644 --- a/internal/configuration/koanf_util.go +++ b/internal/configuration/koanf_util.go @@ -53,15 +53,15 @@ func koanfRemapKeys(val *schema.StructValidator, ko *koanf.Koanf, ds map[string] return final, nil } -func koanfRemapKeysStandard(keys map[string]any, val *schema.StructValidator, ds map[string]Deprecation) (keysFinal map[string]interface{}) { +func koanfRemapKeysStandard(keys map[string]any, val *schema.StructValidator, ds map[string]Deprecation) (keysFinal map[string]any) { var ( ok bool d Deprecation key string - value interface{} + value any ) - keysFinal = make(map[string]interface{}) + keysFinal = make(map[string]any) for key, value = range keys { if d, ok = ds[key]; ok { @@ -93,35 +93,35 @@ func koanfRemapKeysStandard(keys map[string]any, val *schema.StructValidator, ds return keysFinal } -func koanfRemapKeysMapped(keys map[string]interface{}, val *schema.StructValidator, ds map[string]Deprecation) (keysFinal map[string]interface{}) { +func koanfRemapKeysMapped(keys map[string]any, val *schema.StructValidator, ds map[string]Deprecation) (keysFinal map[string]any) { var ( key string - value interface{} - slc, slcFinal []interface{} + value any + slc, slcFinal []any ok bool - m map[string]interface{} + m map[string]any d Deprecation ) - keysFinal = make(map[string]interface{}) + keysFinal = make(map[string]any) for key, value = range keys { - if slc, ok = value.([]interface{}); !ok { + if slc, ok = value.([]any); !ok { keysFinal[key] = value continue } - slcFinal = make([]interface{}, len(slc)) + slcFinal = make([]any, len(slc)) for i, item := range slc { - if m, ok = item.(map[string]interface{}); !ok { + if m, ok = item.(map[string]any); !ok { slcFinal[i] = item continue } - itemFinal := make(map[string]interface{}) + itemFinal := make(map[string]any) for subkey, element := range m { prefix := fmt.Sprintf("%s[].", key) diff --git a/internal/logging/printf.go b/internal/logging/printf.go index 8f6cb7f98..9e5224b2c 100644 --- a/internal/logging/printf.go +++ b/internal/logging/printf.go @@ -13,7 +13,7 @@ type PrintfLogger struct { } // Printf is the implementation of the interface. -func (l *PrintfLogger) Printf(format string, args ...interface{}) { +func (l *PrintfLogger) Printf(format string, args ...any) { l.logrus.Logf(l.level, format, args...) } @@ -24,6 +24,6 @@ type CtxPrintfLogger struct { } // Printf is the implementation of the interface. -func (l *CtxPrintfLogger) Printf(_ context.Context, format string, args ...interface{}) { +func (l *CtxPrintfLogger) Printf(_ context.Context, format string, args ...any) { l.logrus.Logf(l.level, format, args...) } diff --git a/internal/session/encrypting_serializer_test.go b/internal/session/encrypting_serializer_test.go index fac909889..9aa8de1cd 100644 --- a/internal/session/encrypting_serializer_test.go +++ b/internal/session/encrypting_serializer_test.go @@ -9,7 +9,7 @@ import ( ) func TestShouldEncryptAndDecrypt(t *testing.T) { - payload := session.Dict{KV: map[string]interface{}{"key": "value"}} + payload := session.Dict{KV: map[string]any{"key": "value"}} dst, err := payload.MarshalMsg(nil) require.NoError(t, err) @@ -28,7 +28,7 @@ func TestShouldEncryptAndDecrypt(t *testing.T) { } func TestShouldNotSupportUnencryptedSessionForBackwardCompatibility(t *testing.T) { - payload := session.Dict{KV: map[string]interface{}{"key": "value"}} + payload := session.Dict{KV: map[string]any{"key": "value"}} dst, err := payload.MarshalMsg(nil) require.NoError(t, err) diff --git a/internal/utils/crypto_test.go b/internal/utils/crypto_test.go index 99ab52cf8..ea12592c4 100644 --- a/internal/utils/crypto_test.go +++ b/internal/utils/crypto_test.go @@ -325,7 +325,7 @@ func TestShouldParseCurves(t *testing.T) { } } -func testMustBuildPrivateKey(b PrivateKeyBuilder) interface{} { +func testMustBuildPrivateKey(b PrivateKeyBuilder) any { k, err := b.Build() if err != nil { panic(err) @@ -337,8 +337,8 @@ func testMustBuildPrivateKey(b PrivateKeyBuilder) interface{} { func TestPublicKeyFromPrivateKey(t *testing.T) { testCases := []struct { Name string - PrivateKey interface{} - Expected interface{} + PrivateKey any + Expected any }{ { Name: "RSA2048",