Skip to content

Commit 3d006fe

Browse files
alexandearsagikazarmark
authored andcommitted
refactor: replace interface{} with any
1 parent 8a6dc5d commit 3d006fe

28 files changed

+322
-324
lines changed

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -544,19 +544,19 @@ go func(){
544544
In Viper, there are a few ways to get a value depending on the value’s type.
545545
The following functions and methods exist:
546546

547-
* `Get(key string) : interface{}`
547+
* `Get(key string) : any`
548548
* `GetBool(key string) : bool`
549549
* `GetFloat64(key string) : float64`
550550
* `GetInt(key string) : int`
551551
* `GetIntSlice(key string) : []int`
552552
* `GetString(key string) : string`
553-
* `GetStringMap(key string) : map[string]interface{}`
553+
* `GetStringMap(key string) : map[string]any`
554554
* `GetStringMapString(key string) : map[string]string`
555555
* `GetStringSlice(key string) : []string`
556556
* `GetTime(key string) : time.Time`
557557
* `GetDuration(key string) : time.Duration`
558558
* `IsSet(key string) : bool`
559-
* `AllSettings() : map[string]interface{}`
559+
* `AllSettings() : map[string]any`
560560

561561
One important thing to recognize is that each Get function will return a zero
562562
value if it’s not found. To check if a given key exists, the `IsSet()` method
@@ -719,8 +719,8 @@ etc.
719719

720720
There are two methods to do this:
721721

722-
* `Unmarshal(rawVal interface{}) : error`
723-
* `UnmarshalKey(key string, rawVal interface{}) : error`
722+
* `Unmarshal(rawVal any) : error`
723+
* `UnmarshalKey(key string, rawVal any) : error`
724724

725725
Example:
726726

@@ -745,9 +745,9 @@ you have to change the delimiter:
745745
```go
746746
v := viper.NewWithOptions(viper.KeyDelimiter("::"))
747747

748-
v.SetDefault("chart::values", map[string]interface{}{
749-
"ingress": map[string]interface{}{
750-
"annotations": map[string]interface{}{
748+
v.SetDefault("chart::values", map[string]any{
749+
"ingress": map[string]any{
750+
"annotations": map[string]any{
751751
"traefik.frontend.rule.type": "PathPrefix",
752752
"traefik.ingress.kubernetes.io/ssl-redirect": "true",
753753
},
@@ -756,7 +756,7 @@ v.SetDefault("chart::values", map[string]interface{}{
756756

757757
type config struct {
758758
Chart struct{
759-
Values map[string]interface{}
759+
Values map[string]any
760760
}
761761
}
762762

internal/encoding/decoder.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
)
66

77
// Decoder decodes the contents of b into v.
8-
// It's primarily used for decoding contents of a file into a map[string]interface{}.
8+
// It's primarily used for decoding contents of a file into a map[string]any.
99
type Decoder interface {
10-
Decode(b []byte, v map[string]interface{}) error
10+
Decode(b []byte, v map[string]any) error
1111
}
1212

1313
const (
@@ -48,7 +48,7 @@ func (e *DecoderRegistry) RegisterDecoder(format string, enc Decoder) error {
4848
}
4949

5050
// Decode calls the underlying Decoder based on the format.
51-
func (e *DecoderRegistry) Decode(format string, b []byte, v map[string]interface{}) error {
51+
func (e *DecoderRegistry) Decode(format string, b []byte, v map[string]any) error {
5252
e.mu.RLock()
5353
decoder, ok := e.decoders[format]
5454
e.mu.RUnlock()

internal/encoding/decoder_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
)
77

88
type decoder struct {
9-
v map[string]interface{}
9+
v map[string]any
1010
}
1111

12-
func (d decoder) Decode(_ []byte, v map[string]interface{}) error {
12+
func (d decoder) Decode(_ []byte, v map[string]any) error {
1313
for key, value := range d.v {
1414
v[key] = value
1515
}
@@ -46,7 +46,7 @@ func TestDecoderRegistry_Decode(t *testing.T) {
4646
t.Run("OK", func(t *testing.T) {
4747
registry := NewDecoderRegistry()
4848
decoder := decoder{
49-
v: map[string]interface{}{
49+
v: map[string]any{
5050
"key": "value",
5151
},
5252
}
@@ -56,7 +56,7 @@ func TestDecoderRegistry_Decode(t *testing.T) {
5656
t.Fatal(err)
5757
}
5858

59-
v := map[string]interface{}{}
59+
v := map[string]any{}
6060

6161
err = registry.Decode("myformat", []byte("key: value"), v)
6262
if err != nil {
@@ -71,7 +71,7 @@ func TestDecoderRegistry_Decode(t *testing.T) {
7171
t.Run("DecoderNotFound", func(t *testing.T) {
7272
registry := NewDecoderRegistry()
7373

74-
v := map[string]interface{}{}
74+
v := map[string]any{}
7575

7676
err := registry.Decode("myformat", nil, v)
7777
if err != ErrDecoderNotFound {

internal/encoding/dotenv/codec.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const keyDelimiter = "_"
1515
// (commonly called as dotenv format).
1616
type Codec struct{}
1717

18-
func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
19-
flattened := map[string]interface{}{}
18+
func (Codec) Encode(v map[string]any) ([]byte, error) {
19+
flattened := map[string]any{}
2020

2121
flattened = flattenAndMergeMap(flattened, v, "", keyDelimiter)
2222

@@ -40,7 +40,7 @@ func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
4040
return buf.Bytes(), nil
4141
}
4242

43-
func (Codec) Decode(b []byte, v map[string]interface{}) error {
43+
func (Codec) Decode(b []byte, v map[string]any) error {
4444
var buf bytes.Buffer
4545

4646
_, err := buf.Write(b)

internal/encoding/dotenv/codec_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const encoded = `KEY=value
1515
`
1616

1717
// Viper's internal representation
18-
var data = map[string]interface{}{
18+
var data = map[string]any{
1919
"KEY": "value",
2020
}
2121

@@ -36,7 +36,7 @@ func TestCodec_Decode(t *testing.T) {
3636
t.Run("OK", func(t *testing.T) {
3737
codec := Codec{}
3838

39-
v := map[string]interface{}{}
39+
v := map[string]any{}
4040

4141
err := codec.Decode([]byte(original), v)
4242
if err != nil {
@@ -51,7 +51,7 @@ func TestCodec_Decode(t *testing.T) {
5151
t.Run("InvalidData", func(t *testing.T) {
5252
codec := Codec{}
5353

54-
v := map[string]interface{}{}
54+
v := map[string]any{}
5555

5656
err := codec.Decode([]byte(`invalid data`), v)
5757
if err == nil {

internal/encoding/dotenv/map_utils.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ import (
99
// flattenAndMergeMap recursively flattens the given map into a new map
1010
// Code is based on the function with the same name in the main package.
1111
// TODO: move it to a common place
12-
func flattenAndMergeMap(shadow map[string]interface{}, m map[string]interface{}, prefix string, delimiter string) map[string]interface{} {
12+
func flattenAndMergeMap(shadow map[string]any, m map[string]any, prefix string, delimiter string) map[string]any {
1313
if shadow != nil && prefix != "" && shadow[prefix] != nil {
1414
// prefix is shadowed => nothing more to flatten
1515
return shadow
1616
}
1717
if shadow == nil {
18-
shadow = make(map[string]interface{})
18+
shadow = make(map[string]any)
1919
}
2020

21-
var m2 map[string]interface{}
21+
var m2 map[string]any
2222
if prefix != "" {
2323
prefix += delimiter
2424
}
2525
for k, val := range m {
2626
fullKey := prefix + k
2727
switch val := val.(type) {
28-
case map[string]interface{}:
28+
case map[string]any:
2929
m2 = val
30-
case map[interface{}]interface{}:
30+
case map[any]any:
3131
m2 = cast.ToStringMap(val)
3232
default:
3333
// immediate value

internal/encoding/encoder.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
)
66

77
// Encoder encodes the contents of v into a byte representation.
8-
// It's primarily used for encoding a map[string]interface{} into a file format.
8+
// It's primarily used for encoding a map[string]any into a file format.
99
type Encoder interface {
10-
Encode(v map[string]interface{}) ([]byte, error)
10+
Encode(v map[string]any) ([]byte, error)
1111
}
1212

1313
const (
@@ -47,7 +47,7 @@ func (e *EncoderRegistry) RegisterEncoder(format string, enc Encoder) error {
4747
return nil
4848
}
4949

50-
func (e *EncoderRegistry) Encode(format string, v map[string]interface{}) ([]byte, error) {
50+
func (e *EncoderRegistry) Encode(format string, v map[string]any) ([]byte, error) {
5151
e.mu.RLock()
5252
encoder, ok := e.encoders[format]
5353
e.mu.RUnlock()

internal/encoding/encoder_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type encoder struct {
88
b []byte
99
}
1010

11-
func (e encoder) Encode(_ map[string]interface{}) ([]byte, error) {
11+
func (e encoder) Encode(_ map[string]any) ([]byte, error) {
1212
return e.b, nil
1313
}
1414

@@ -49,7 +49,7 @@ func TestEncoderRegistry_Decode(t *testing.T) {
4949
t.Fatal(err)
5050
}
5151

52-
b, err := registry.Encode("myformat", map[string]interface{}{"key": "value"})
52+
b, err := registry.Encode("myformat", map[string]any{"key": "value"})
5353
if err != nil {
5454
t.Fatal(err)
5555
}
@@ -62,7 +62,7 @@ func TestEncoderRegistry_Decode(t *testing.T) {
6262
t.Run("EncoderNotFound", func(t *testing.T) {
6363
registry := NewEncoderRegistry()
6464

65-
_, err := registry.Encode("myformat", map[string]interface{}{"key": "value"})
65+
_, err := registry.Encode("myformat", map[string]any{"key": "value"})
6666
if err != ErrEncoderNotFound {
6767
t.Fatalf("expected ErrEncoderNotFound, got: %v", err)
6868
}

internal/encoding/hcl/codec.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// TODO: add printer config to the codec?
1313
type Codec struct{}
1414

15-
func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
15+
func (Codec) Encode(v map[string]any) ([]byte, error) {
1616
b, err := json.Marshal(v)
1717
if err != nil {
1818
return nil, err
@@ -35,6 +35,6 @@ func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
3535
return buf.Bytes(), nil
3636
}
3737

38-
func (Codec) Decode(b []byte, v map[string]interface{}) error {
38+
func (Codec) Decode(b []byte, v map[string]any) error {
3939
return hcl.Unmarshal(b, &v)
4040
}

internal/encoding/hcl/codec_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,24 @@ const encoded = `"key" = "value"
4545
//
4646
// in case of HCL it's slightly different from Viper's internal representation
4747
// (eg. map is decoded into a list of maps)
48-
var decoded = map[string]interface{}{
48+
var decoded = map[string]any{
4949
"key": "value",
50-
"list": []interface{}{
50+
"list": []any{
5151
"item1",
5252
"item2",
5353
"item3",
5454
},
55-
"map": []map[string]interface{}{
55+
"map": []map[string]any{
5656
{
5757
"key": "value",
5858
},
5959
},
60-
"nested_map": []map[string]interface{}{
60+
"nested_map": []map[string]any{
6161
{
62-
"map": []map[string]interface{}{
62+
"map": []map[string]any{
6363
{
6464
"key": "value",
65-
"list": []interface{}{
65+
"list": []any{
6666
"item1",
6767
"item2",
6868
"item3",
@@ -74,20 +74,20 @@ var decoded = map[string]interface{}{
7474
}
7575

7676
// Viper's internal representation
77-
var data = map[string]interface{}{
77+
var data = map[string]any{
7878
"key": "value",
79-
"list": []interface{}{
79+
"list": []any{
8080
"item1",
8181
"item2",
8282
"item3",
8383
},
84-
"map": map[string]interface{}{
84+
"map": map[string]any{
8585
"key": "value",
8686
},
87-
"nested_map": map[string]interface{}{
88-
"map": map[string]interface{}{
87+
"nested_map": map[string]any{
88+
"map": map[string]any{
8989
"key": "value",
90-
"list": []interface{}{
90+
"list": []any{
9191
"item1",
9292
"item2",
9393
"item3",
@@ -113,7 +113,7 @@ func TestCodec_Decode(t *testing.T) {
113113
t.Run("OK", func(t *testing.T) {
114114
codec := Codec{}
115115

116-
v := map[string]interface{}{}
116+
v := map[string]any{}
117117

118118
err := codec.Decode([]byte(original), v)
119119
if err != nil {
@@ -128,7 +128,7 @@ func TestCodec_Decode(t *testing.T) {
128128
t.Run("InvalidData", func(t *testing.T) {
129129
codec := Codec{}
130130

131-
v := map[string]interface{}{}
131+
v := map[string]any{}
132132

133133
err := codec.Decode([]byte(`invalid data`), v)
134134
if err == nil {

internal/encoding/ini/codec.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ type Codec struct {
1919
LoadOptions LoadOptions
2020
}
2121

22-
func (c Codec) Encode(v map[string]interface{}) ([]byte, error) {
22+
func (c Codec) Encode(v map[string]any) ([]byte, error) {
2323
cfg := ini.Empty()
2424
ini.PrettyFormat = false
2525

26-
flattened := map[string]interface{}{}
26+
flattened := map[string]any{}
2727

2828
flattened = flattenAndMergeMap(flattened, v, "", c.keyDelimiter())
2929

@@ -62,7 +62,7 @@ func (c Codec) Encode(v map[string]interface{}) ([]byte, error) {
6262
return buf.Bytes(), nil
6363
}
6464

65-
func (c Codec) Decode(b []byte, v map[string]interface{}) error {
65+
func (c Codec) Decode(b []byte, v map[string]any) error {
6666
cfg := ini.Empty(c.LoadOptions)
6767

6868
err := cfg.Append(b)

0 commit comments

Comments
 (0)