Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace 'interface{}' with 'any' #1646

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,19 +544,19 @@ go func(){
In Viper, there are a few ways to get a value depending on the value’s type.
The following functions and methods exist:

* `Get(key string) : interface{}`
* `Get(key string) : any`
* `GetBool(key string) : bool`
* `GetFloat64(key string) : float64`
* `GetInt(key string) : int`
* `GetIntSlice(key string) : []int`
* `GetString(key string) : string`
* `GetStringMap(key string) : map[string]interface{}`
* `GetStringMap(key string) : map[string]any`
* `GetStringMapString(key string) : map[string]string`
* `GetStringSlice(key string) : []string`
* `GetTime(key string) : time.Time`
* `GetDuration(key string) : time.Duration`
* `IsSet(key string) : bool`
* `AllSettings() : map[string]interface{}`
* `AllSettings() : map[string]any`

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

There are two methods to do this:

* `Unmarshal(rawVal interface{}) : error`
* `UnmarshalKey(key string, rawVal interface{}) : error`
* `Unmarshal(rawVal any) : error`
* `UnmarshalKey(key string, rawVal any) : error`

Example:

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

v.SetDefault("chart::values", map[string]interface{}{
"ingress": map[string]interface{}{
"annotations": map[string]interface{}{
v.SetDefault("chart::values", map[string]any{
"ingress": map[string]any{
"annotations": map[string]any{
"traefik.frontend.rule.type": "PathPrefix",
"traefik.ingress.kubernetes.io/ssl-redirect": "true",
},
Expand All @@ -756,7 +756,7 @@ v.SetDefault("chart::values", map[string]interface{}{

type config struct {
Chart struct{
Values map[string]interface{}
Values map[string]any
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/encoding/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
)

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

const (
Expand Down Expand Up @@ -48,7 +48,7 @@ func (e *DecoderRegistry) RegisterDecoder(format string, enc Decoder) error {
}

// Decode calls the underlying Decoder based on the format.
func (e *DecoderRegistry) Decode(format string, b []byte, v map[string]interface{}) error {
func (e *DecoderRegistry) Decode(format string, b []byte, v map[string]any) error {
e.mu.RLock()
decoder, ok := e.decoders[format]
e.mu.RUnlock()
Expand Down
10 changes: 5 additions & 5 deletions internal/encoding/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

type decoder struct {
v map[string]interface{}
v map[string]any
}

func (d decoder) Decode(_ []byte, v map[string]interface{}) error {
func (d decoder) Decode(_ []byte, v map[string]any) error {
for key, value := range d.v {
v[key] = value
}
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestDecoderRegistry_Decode(t *testing.T) {
t.Run("OK", func(t *testing.T) {
registry := NewDecoderRegistry()
decoder := decoder{
v: map[string]interface{}{
v: map[string]any{
"key": "value",
},
}
Expand All @@ -56,7 +56,7 @@ func TestDecoderRegistry_Decode(t *testing.T) {
t.Fatal(err)
}

v := map[string]interface{}{}
v := map[string]any{}

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

v := map[string]interface{}{}
v := map[string]any{}

err := registry.Decode("myformat", nil, v)
if err != ErrDecoderNotFound {
Expand Down
6 changes: 3 additions & 3 deletions internal/encoding/dotenv/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const keyDelimiter = "_"
// (commonly called as dotenv format).
type Codec struct{}

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

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

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

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

_, err := buf.Write(b)
Expand Down
6 changes: 3 additions & 3 deletions internal/encoding/dotenv/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const encoded = `KEY=value
`

// Viper's internal representation
var data = map[string]interface{}{
var data = map[string]any{
"KEY": "value",
}

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

v := map[string]interface{}{}
v := map[string]any{}

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

v := map[string]interface{}{}
v := map[string]any{}

err := codec.Decode([]byte(`invalid data`), v)
if err == nil {
Expand Down
10 changes: 5 additions & 5 deletions internal/encoding/dotenv/map_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ import (
// flattenAndMergeMap recursively flattens the given map into a new map
// Code is based on the function with the same name in the main package.
// TODO: move it to a common place
func flattenAndMergeMap(shadow map[string]interface{}, m map[string]interface{}, prefix string, delimiter string) map[string]interface{} {
func flattenAndMergeMap(shadow map[string]any, m map[string]any, prefix string, delimiter string) map[string]any {
if shadow != nil && prefix != "" && shadow[prefix] != nil {
// prefix is shadowed => nothing more to flatten
return shadow
}
if shadow == nil {
shadow = make(map[string]interface{})
shadow = make(map[string]any)
}

var m2 map[string]interface{}
var m2 map[string]any
if prefix != "" {
prefix += delimiter
}
for k, val := range m {
fullKey := prefix + k
switch val := val.(type) {
case map[string]interface{}:
case map[string]any:
m2 = val
case map[interface{}]interface{}:
case map[any]any:
m2 = cast.ToStringMap(val)
default:
// immediate value
Expand Down
6 changes: 3 additions & 3 deletions internal/encoding/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
)

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

const (
Expand Down Expand Up @@ -47,7 +47,7 @@ func (e *EncoderRegistry) RegisterEncoder(format string, enc Encoder) error {
return nil
}

func (e *EncoderRegistry) Encode(format string, v map[string]interface{}) ([]byte, error) {
func (e *EncoderRegistry) Encode(format string, v map[string]any) ([]byte, error) {
e.mu.RLock()
encoder, ok := e.encoders[format]
e.mu.RUnlock()
Expand Down
6 changes: 3 additions & 3 deletions internal/encoding/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type encoder struct {
b []byte
}

func (e encoder) Encode(_ map[string]interface{}) ([]byte, error) {
func (e encoder) Encode(_ map[string]any) ([]byte, error) {
return e.b, nil
}

Expand Down Expand Up @@ -49,7 +49,7 @@ func TestEncoderRegistry_Decode(t *testing.T) {
t.Fatal(err)
}

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

_, err := registry.Encode("myformat", map[string]interface{}{"key": "value"})
_, err := registry.Encode("myformat", map[string]any{"key": "value"})
if err != ErrEncoderNotFound {
t.Fatalf("expected ErrEncoderNotFound, got: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/encoding/hcl/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// TODO: add printer config to the codec?
type Codec struct{}

func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
func (Codec) Encode(v map[string]any) ([]byte, error) {
b, err := json.Marshal(v)
if err != nil {
return nil, err
Expand All @@ -35,6 +35,6 @@ func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
return buf.Bytes(), nil
}

func (Codec) Decode(b []byte, v map[string]interface{}) error {
func (Codec) Decode(b []byte, v map[string]any) error {
return hcl.Unmarshal(b, &v)
}
28 changes: 14 additions & 14 deletions internal/encoding/hcl/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@ const encoded = `"key" = "value"
//
// in case of HCL it's slightly different from Viper's internal representation
// (eg. map is decoded into a list of maps)
var decoded = map[string]interface{}{
var decoded = map[string]any{
"key": "value",
"list": []interface{}{
"list": []any{
"item1",
"item2",
"item3",
},
"map": []map[string]interface{}{
"map": []map[string]any{
{
"key": "value",
},
},
"nested_map": []map[string]interface{}{
"nested_map": []map[string]any{
{
"map": []map[string]interface{}{
"map": []map[string]any{
{
"key": "value",
"list": []interface{}{
"list": []any{
"item1",
"item2",
"item3",
Expand All @@ -74,20 +74,20 @@ var decoded = map[string]interface{}{
}

// Viper's internal representation
var data = map[string]interface{}{
var data = map[string]any{
"key": "value",
"list": []interface{}{
"list": []any{
"item1",
"item2",
"item3",
},
"map": map[string]interface{}{
"map": map[string]any{
"key": "value",
},
"nested_map": map[string]interface{}{
"map": map[string]interface{}{
"nested_map": map[string]any{
"map": map[string]any{
"key": "value",
"list": []interface{}{
"list": []any{
"item1",
"item2",
"item3",
Expand All @@ -113,7 +113,7 @@ func TestCodec_Decode(t *testing.T) {
t.Run("OK", func(t *testing.T) {
codec := Codec{}

v := map[string]interface{}{}
v := map[string]any{}

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

v := map[string]interface{}{}
v := map[string]any{}

err := codec.Decode([]byte(`invalid data`), v)
if err == nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/encoding/ini/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ type Codec struct {
LoadOptions LoadOptions
}

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

flattened := map[string]interface{}{}
flattened := map[string]any{}

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

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

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

err := cfg.Append(b)
Expand Down