rename interface{} to any

Signed-off-by: Terry Howe <terrylhowe@gmail.com>
pull/31342/head
Terry Howe 11 months ago
parent bee9c1a108
commit ffb3940011
No known key found for this signature in database

@ -21,17 +21,17 @@ import (
"reflect" "reflect"
) )
// Copy performs a deep copy of the given interface{}. // Copy performs a deep copy of the given src.
// This implementation handles the specific use cases needed by Helm. // This implementation handles the specific use cases needed by Helm.
func Copy(src interface{}) (interface{}, error) { func Copy(src any) (any, error) {
if src == nil { if src == nil {
return make(map[string]interface{}), nil return make(map[string]any), nil
} }
return copyValue(reflect.ValueOf(src)) return copyValue(reflect.ValueOf(src))
} }
// copyValue handles copying using reflection for non-map types // copyValue handles copying using reflection for non-map types
func copyValue(original reflect.Value) (interface{}, error) { func copyValue(original reflect.Value) (any, error) {
switch original.Kind() { switch original.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
@ -52,7 +52,7 @@ func copyValue(original reflect.Value) (interface{}, error) {
copied := reflect.MakeMap(original.Type()) copied := reflect.MakeMap(original.Type())
var err error var err error
var child interface{} var child any
iter := original.MapRange() iter := original.MapRange()
for iter.Next() { for iter.Next() {
key := iter.Key() key := iter.Key()

@ -26,13 +26,13 @@ import (
func TestCopy_Nil(t *testing.T) { func TestCopy_Nil(t *testing.T) {
result, err := Copy(nil) result, err := Copy(nil)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, map[string]interface{}{}, result) assert.Equal(t, map[string]any{}, result)
} }
func TestCopy_PrimitiveTypes(t *testing.T) { func TestCopy_PrimitiveTypes(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input interface{} input any
}{ }{
{"bool", true}, {"bool", true},
{"int", 42}, {"int", 42},
@ -98,14 +98,14 @@ func TestCopy_Slice(t *testing.T) {
}) })
t.Run("slice of maps", func(t *testing.T) { t.Run("slice of maps", func(t *testing.T) {
input := []map[string]interface{}{ input := []map[string]any{
{"key1": "value1"}, {"key1": "value1"},
{"key2": "value2"}, {"key2": "value2"},
} }
result, err := Copy(input) result, err := Copy(input)
require.NoError(t, err) require.NoError(t, err)
resultSlice, ok := result.([]map[string]interface{}) resultSlice, ok := result.([]map[string]any)
require.True(t, ok) require.True(t, ok)
assert.Equal(t, input, resultSlice) assert.Equal(t, input, resultSlice)
@ -116,12 +116,12 @@ func TestCopy_Slice(t *testing.T) {
} }
func TestCopy_Map(t *testing.T) { func TestCopy_Map(t *testing.T) {
t.Run("map[string]interface{}", func(t *testing.T) { t.Run("map[string]any", func(t *testing.T) {
input := map[string]interface{}{ input := map[string]any{
"string": "value", "string": "value",
"int": 42, "int": 42,
"bool": true, "bool": true,
"nested": map[string]interface{}{ "nested": map[string]any{
"inner": "value", "inner": "value",
}, },
} }
@ -129,7 +129,7 @@ func TestCopy_Map(t *testing.T) {
result, err := Copy(input) result, err := Copy(input)
require.NoError(t, err) require.NoError(t, err)
resultMap, ok := result.(map[string]interface{}) resultMap, ok := result.(map[string]any)
require.True(t, ok) require.True(t, ok)
assert.Equal(t, input, resultMap) assert.Equal(t, input, resultMap)
@ -137,8 +137,8 @@ func TestCopy_Map(t *testing.T) {
input["string"] = "modified" input["string"] = "modified"
assert.Equal(t, "value", resultMap["string"]) assert.Equal(t, "value", resultMap["string"])
nestedInput := input["nested"].(map[string]interface{}) nestedInput := input["nested"].(map[string]any)
nestedResult := resultMap["nested"].(map[string]interface{}) nestedResult := resultMap["nested"].(map[string]any)
nestedInput["inner"] = "modified" nestedInput["inner"] = "modified"
assert.Equal(t, "value", nestedResult["inner"]) assert.Equal(t, "value", nestedResult["inner"])
}) })
@ -155,14 +155,14 @@ func TestCopy_Map(t *testing.T) {
}) })
t.Run("nil map", func(t *testing.T) { t.Run("nil map", func(t *testing.T) {
var input map[string]interface{} var input map[string]any
result, err := Copy(input) result, err := Copy(input)
require.NoError(t, err) require.NoError(t, err)
assert.Nil(t, result) assert.Nil(t, result)
}) })
t.Run("map with nil values", func(t *testing.T) { t.Run("map with nil values", func(t *testing.T) {
input := map[string]interface{}{ input := map[string]any{
"key1": "value1", "key1": "value1",
"key2": nil, "key2": nil,
} }
@ -170,7 +170,7 @@ func TestCopy_Map(t *testing.T) {
result, err := Copy(input) result, err := Copy(input)
require.NoError(t, err) require.NoError(t, err)
resultMap, ok := result.(map[string]interface{}) resultMap, ok := result.(map[string]any)
require.True(t, ok) require.True(t, ok)
assert.Equal(t, input, resultMap) assert.Equal(t, input, resultMap)
assert.Nil(t, resultMap["key2"]) assert.Nil(t, resultMap["key2"])
@ -183,7 +183,7 @@ func TestCopy_Struct(t *testing.T) {
Age int Age int
Active bool Active bool
Scores []int Scores []int
Metadata map[string]interface{} Metadata map[string]any
} }
input := TestStruct{ input := TestStruct{
@ -191,7 +191,7 @@ func TestCopy_Struct(t *testing.T) {
Age: 30, Age: 30,
Active: true, Active: true,
Scores: []int{95, 87, 92}, Scores: []int{95, 87, 92},
Metadata: map[string]interface{}{ Metadata: map[string]any{
"level": "advanced", "level": "advanced",
"tags": []string{"go", "programming"}, "tags": []string{"go", "programming"},
}, },
@ -257,25 +257,25 @@ func TestCopy_Pointer(t *testing.T) {
} }
func TestCopy_Interface(t *testing.T) { func TestCopy_Interface(t *testing.T) {
t.Run("interface{} with value", func(t *testing.T) { t.Run("any with value", func(t *testing.T) {
var input interface{} = "hello" var input any = "hello"
result, err := Copy(input) result, err := Copy(input)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, input, result) assert.Equal(t, input, result)
}) })
t.Run("nil interface{}", func(t *testing.T) { t.Run("nil any", func(t *testing.T) {
var input interface{} var input any
result, err := Copy(input) result, err := Copy(input)
require.NoError(t, err) require.NoError(t, err)
// Copy(nil) returns an empty map according to the implementation // Copy(nil) returns an empty map according to the implementation
assert.Equal(t, map[string]interface{}{}, result) assert.Equal(t, map[string]any{}, result)
}) })
t.Run("interface{} with complex value", func(t *testing.T) { t.Run("any with complex value", func(t *testing.T) {
var input interface{} = map[string]interface{}{ var input any = map[string]any{
"key": "value", "key": "value",
"nested": map[string]interface{}{ "nested": map[string]any{
"inner": 42, "inner": 42,
}, },
} }
@ -287,12 +287,12 @@ func TestCopy_Interface(t *testing.T) {
} }
func TestCopy_ComplexNested(t *testing.T) { func TestCopy_ComplexNested(t *testing.T) {
input := map[string]interface{}{ input := map[string]any{
"users": []map[string]interface{}{ "users": []map[string]any{
{ {
"name": "Alice", "name": "Alice",
"age": 30, "age": 30,
"addresses": []map[string]interface{}{ "addresses": []map[string]any{
{"type": "home", "city": "NYC"}, {"type": "home", "city": "NYC"},
{"type": "work", "city": "SF"}, {"type": "work", "city": "SF"},
}, },
@ -300,12 +300,12 @@ func TestCopy_ComplexNested(t *testing.T) {
{ {
"name": "Bob", "name": "Bob",
"age": 25, "age": 25,
"addresses": []map[string]interface{}{ "addresses": []map[string]any{
{"type": "home", "city": "LA"}, {"type": "home", "city": "LA"},
}, },
}, },
}, },
"metadata": map[string]interface{}{ "metadata": map[string]any{
"version": "1.0", "version": "1.0",
"flags": []bool{true, false, true}, "flags": []bool{true, false, true},
}, },
@ -314,17 +314,17 @@ func TestCopy_ComplexNested(t *testing.T) {
result, err := Copy(input) result, err := Copy(input)
require.NoError(t, err) require.NoError(t, err)
resultMap, ok := result.(map[string]interface{}) resultMap, ok := result.(map[string]any)
require.True(t, ok) require.True(t, ok)
assert.Equal(t, input, resultMap) assert.Equal(t, input, resultMap)
// Verify deep copy by modifying nested values // Verify deep copy by modifying nested values
users := input["users"].([]map[string]interface{}) users := input["users"].([]map[string]any)
addresses := users[0]["addresses"].([]map[string]interface{}) addresses := users[0]["addresses"].([]map[string]any)
addresses[0]["city"] = "Modified" addresses[0]["city"] = "Modified"
resultUsers := resultMap["users"].([]map[string]interface{}) resultUsers := resultMap["users"].([]map[string]any)
resultAddresses := resultUsers[0]["addresses"].([]map[string]interface{}) resultAddresses := resultUsers[0]["addresses"].([]map[string]any)
assert.Equal(t, "NYC", resultAddresses[0]["city"]) assert.Equal(t, "NYC", resultAddresses[0]["city"])
} }

Loading…
Cancel
Save