From bfa6b9dcef7592e6dd8225aaa0d0ab5aef5b3f84 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sun, 9 Mar 2025 20:14:46 -0400 Subject: chore(refactoring): Refactor/modernizations (#2345) chore(refactoring): Refactor with modernization refactorings * range over int * replace interface{} with any * replace common map operations with maps.Copy/maps.Clone * simplifying loops with slices.Contains/ContainsFunc * modernize benchmarks with b.Loop * modernize tests with t.Context * use fmt.Appendf * range over strings.SplitSeq * use new stdlib crypto/pbkdf2 package --------- Signed-off-by: Dave Henderson --- internal/conv/conv.go | 12 ++++++------ internal/conv/conv_test.go | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'internal/conv') diff --git a/internal/conv/conv.go b/internal/conv/conv.go index 3b2ca7c7..29dca28e 100644 --- a/internal/conv/conv.go +++ b/internal/conv/conv.go @@ -5,11 +5,11 @@ import ( "reflect" ) -// InterfaceSlice converts an array or slice of any type into an []interface{} +// InterfaceSlice converts an array or slice of any type into an []any // for use in functions that expect this. -func InterfaceSlice(slice interface{}) ([]interface{}, error) { - // avoid all this nonsense if this is already a []interface{}... - if s, ok := slice.([]interface{}); ok { +func InterfaceSlice(slice any) ([]any, error) { + // avoid all this nonsense if this is already a []any... + if s, ok := slice.([]any); ok { return s, nil } s := reflect.ValueOf(slice) @@ -17,8 +17,8 @@ func InterfaceSlice(slice interface{}) ([]interface{}, error) { switch kind { case reflect.Slice, reflect.Array: l := s.Len() - ret := make([]interface{}, l) - for i := 0; i < l; i++ { + ret := make([]any, l) + for i := range l { ret[i] = s.Index(i).Interface() } return ret, nil diff --git a/internal/conv/conv_test.go b/internal/conv/conv_test.go index c83c8dd0..f2542383 100644 --- a/internal/conv/conv_test.go +++ b/internal/conv/conv_test.go @@ -9,14 +9,14 @@ import ( func TestInterfaceSlice(t *testing.T) { data := []struct { - in, expected interface{} + in, expected any }{ - {[]int{1, 2, 3}, []interface{}{1, 2, 3}}, - {[3]int{1, 2, 3}, []interface{}{1, 2, 3}}, - {[]string{"foo", "bar", "baz"}, []interface{}{"foo", "bar", "baz"}}, - {[3]string{"foo", "bar", "baz"}, []interface{}{"foo", "bar", "baz"}}, - {[]interface{}{[]string{}, []int{1, 2}, 3}, []interface{}{[]string{}, []int{1, 2}, 3}}, - {[3]interface{}{[]string{}, []int{1, 2}, 3}, []interface{}{[]string{}, []int{1, 2}, 3}}, + {[]int{1, 2, 3}, []any{1, 2, 3}}, + {[3]int{1, 2, 3}, []any{1, 2, 3}}, + {[]string{"foo", "bar", "baz"}, []any{"foo", "bar", "baz"}}, + {[3]string{"foo", "bar", "baz"}, []any{"foo", "bar", "baz"}}, + {[]any{[]string{}, []int{1, 2}, 3}, []any{[]string{}, []int{1, 2}, 3}}, + {[3]any{[]string{}, []int{1, 2}, 3}, []any{[]string{}, []int{1, 2}, 3}}, } for _, d := range data { @@ -30,18 +30,18 @@ func TestInterfaceSlice(t *testing.T) { } func BenchmarkInterfaceSlice(b *testing.B) { - data := []interface{}{ + data := []any{ []int{1, 2, 3}, [3]int{1, 2, 3}, []string{"foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz"}, [12]string{"foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz"}, - []interface{}{[]string{}, []int{1, 2}, 3}, - [3]interface{}{[]string{}, []int{1, 2}, 3}, + []any{[]string{}, []int{1, 2}, 3}, + [3]any{[]string{}, []int{1, 2}, 3}, } for _, d := range data { b.Run(fmt.Sprintf("%T(%v)", d, d), func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { InterfaceSlice(d) } }) -- cgit v1.2.3