summaryrefslogtreecommitdiff
path: root/internal/conv
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2025-03-09 20:14:46 -0400
committerGitHub <noreply@github.com>2025-03-10 00:14:46 +0000
commitbfa6b9dcef7592e6dd8225aaa0d0ab5aef5b3f84 (patch)
tree7e844defee92dc3af320df20baa6f9b421d4a4c9 /internal/conv
parent7942441e61471f578a57910b3aa93636f5a0310d (diff)
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 <dhenderson@gmail.com>
Diffstat (limited to 'internal/conv')
-rw-r--r--internal/conv/conv.go12
-rw-r--r--internal/conv/conv_test.go22
2 files changed, 17 insertions, 17 deletions
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)
}
})