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 --- conv/conv.go | 36 ++++++++++++++--------------- conv/conv_test.go | 68 +++++++++++++++++++++++++++---------------------------- conv/evalargs.go | 2 +- 3 files changed, 53 insertions(+), 53 deletions(-) (limited to 'conv') diff --git a/conv/conv.go b/conv/conv.go index fd455244..223dd156 100644 --- a/conv/conv.go +++ b/conv/conv.go @@ -29,7 +29,7 @@ func Bool(in string) bool { // Possible non-boolean true values are: 1 or the strings "t", "true", or "yes" // (any capitalizations) // All other values are considered false. -func ToBool(in interface{}) bool { +func ToBool(in any) bool { if b, ok := in.(bool); ok { return b } @@ -60,7 +60,7 @@ func ToBool(in interface{}) bool { } // ToBools - -func ToBools(in ...interface{}) []bool { +func ToBools(in ...any) []bool { out := make([]bool, len(in)) for i, v := range in { out[i] = ToBool(v) @@ -71,7 +71,7 @@ func ToBools(in ...interface{}) []bool { // Slice creates a slice from a bunch of arguments // // Deprecated: use [github.com/hairyhenderson/gomplate/v4/coll.Slice] instead -func Slice(args ...interface{}) []interface{} { +func Slice(args ...any) []any { return args } @@ -80,14 +80,14 @@ func Slice(args ...interface{}) []interface{} { // // This is functionally identical to strings.Join, except that each element is // coerced to a string first -func Join(in interface{}, sep string) (out string, err error) { +func Join(in any, sep string) (out string, err error) { s, ok := in.([]string) if ok { return strings.Join(s, sep), nil } - var a []interface{} - a, ok = in.([]interface{}) + var a []any + a, ok = in.([]any) if !ok { a, err = iconv.InterfaceSlice(in) if err != nil { @@ -107,7 +107,7 @@ func Join(in interface{}, sep string) (out string, err error) { } // Has determines whether or not a given object has a property with the given key -func Has(in interface{}, key interface{}) bool { +func Has(in any, key any) bool { av := reflect.ValueOf(in) switch av.Kind() { @@ -116,7 +116,7 @@ func Has(in interface{}, key interface{}) bool { return av.MapIndex(kv).IsValid() case reflect.Slice, reflect.Array: l := av.Len() - for i := 0; i < l; i++ { + for i := range l { v := av.Index(i).Interface() if reflect.DeepEqual(v, key) { return true @@ -128,7 +128,7 @@ func Has(in interface{}, key interface{}) bool { } // ToString - -func ToString(in interface{}) string { +func ToString(in any) string { if in == nil { return "nil" } @@ -151,7 +151,7 @@ func ToString(in interface{}) string { } // ToStrings - -func ToStrings(in ...interface{}) []string { +func ToStrings(in ...any) []string { out := make([]string, len(in)) for i, v := range in { out[i] = ToString(v) @@ -184,7 +184,7 @@ func MustAtoi(s string) int { } // ToInt64 - convert input to an int64, if convertible. Otherwise, returns 0. -func ToInt64(v interface{}) (int64, error) { +func ToInt64(v any) (int64, error) { if str, ok := v.(string); ok { return strToInt64(str) } @@ -218,7 +218,7 @@ func ToInt64(v interface{}) (int64, error) { } // ToInt - -func ToInt(in interface{}) (int, error) { +func ToInt(in any) (int, error) { i, err := ToInt64(in) if err != nil { return 0, err @@ -236,7 +236,7 @@ func ToInt(in interface{}) (int, error) { } // ToInt64s - -func ToInt64s(in ...interface{}) ([]int64, error) { +func ToInt64s(in ...any) ([]int64, error) { out := make([]int64, len(in)) for i, v := range in { n, err := ToInt64(v) @@ -251,7 +251,7 @@ func ToInt64s(in ...interface{}) ([]int64, error) { } // ToInts - -func ToInts(in ...interface{}) ([]int, error) { +func ToInts(in ...any) ([]int, error) { out := make([]int, len(in)) for i, v := range in { n, err := ToInt(v) @@ -266,7 +266,7 @@ func ToInts(in ...interface{}) ([]int, error) { } // ToFloat64 - convert input to a float64, if convertible. Otherwise, errors. -func ToFloat64(v interface{}) (float64, error) { +func ToFloat64(v any) (float64, error) { if str, ok := v.(string); ok { return strToFloat64(str) } @@ -334,7 +334,7 @@ func strToFloat64(str string) (float64, error) { } // ToFloat64s - -func ToFloat64s(in ...interface{}) ([]float64, error) { +func ToFloat64s(in ...any) ([]float64, error) { out := make([]float64, len(in)) for i, v := range in { f, err := ToFloat64(v) @@ -352,8 +352,8 @@ func ToFloat64s(in ...interface{}) ([]float64, error) { // is provided, the last is used as the key, and an empty string is // set as the value. // All keys are converted to strings, regardless of input type. -func Dict(v ...interface{}) (map[string]interface{}, error) { - dict := map[string]interface{}{} +func Dict(v ...any) (map[string]any, error) { + dict := map[string]any{} lenv := len(v) for i := 0; i < lenv; i += 2 { key := ToString(v[i]) diff --git a/conv/conv_test.go b/conv/conv_test.go index 2ce0659a..5b986e48 100644 --- a/conv/conv_test.go +++ b/conv/conv_test.go @@ -42,22 +42,22 @@ func TestSlice(t *testing.T) { func TestJoin(t *testing.T) { testdata := []struct { - in interface{} + in any sep string out string }{ - {[]interface{}{"foo", "bar"}, ",", "foo,bar"}, - {[]interface{}{"foo", "bar"}, ",\n", "foo,\nbar"}, + {[]any{"foo", "bar"}, ",", "foo,bar"}, + {[]any{"foo", "bar"}, ",\n", "foo,\nbar"}, // Join handles all kinds of scalar types too... - {[]interface{}{42, uint64(18446744073709551615)}, "-", "42-18446744073709551615"}, + {[]any{42, uint64(18446744073709551615)}, "-", "42-18446744073709551615"}, {[]int{42, 100}, ",", "42,100"}, {[]int64{42, 100}, ",", "42,100"}, {[]uint64{42, 100}, ",", "42,100"}, {[]bool{true, false}, ",", "true,false"}, {[]float64{1, 2}, ",", "1,2"}, - {[]interface{}{1, "", true, 3.14, "foo", nil}, ",", "1,,true,3.14,foo,nil"}, + {[]any{1, "", true, 3.14, "foo", nil}, ",", "1,,true,3.14,foo,nil"}, // and best-effort with weird types - {[]interface{}{[]string{"foo"}, "bar"}, ",", "[foo],bar"}, + {[]any{[]string{"foo"}, "bar"}, ",", "[foo],bar"}, } for _, d := range testdata { out, err := Join(d.in, d.sep) @@ -67,24 +67,24 @@ func TestJoin(t *testing.T) { } func TestHas(t *testing.T) { - in := map[string]interface{}{ + in := map[string]any{ "foo": "bar", - "baz": map[string]interface{}{ + "baz": map[string]any{ "qux": "quux", }, } testdata := []struct { - in interface{} - key interface{} + in any + key any out bool }{ {in, "foo", true}, {in, "bar", false}, {in["baz"], "qux", true}, {[]string{"foo", "bar", "baz"}, "bar", true}, - {[]interface{}{"foo", "bar", "baz"}, "bar", true}, - {[]interface{}{"foo", "bar", "baz"}, 42, false}, + {[]any{"foo", "bar", "baz"}, "bar", true}, + {[]any{"foo", "bar", "baz"}, 42, false}, {[]int{1, 2, 42}, 42, true}, } @@ -321,13 +321,13 @@ func TestToInts(t *testing.T) { } func TestToFloat64(t *testing.T) { - z := []interface{}{nil, "", "foo"} + z := []any{nil, "", "foo"} for _, n := range z { _, err := ToFloat64(n) require.Error(t, err) } - z = []interface{}{0, 0.0, false, float32(0), "0", int64(0), uint(0), "0x0", "00", "0,000"} + z = []any{0, 0.0, false, float32(0), "0", int64(0), uint(0), "0x0", "00", "0,000"} for _, n := range z { actual, err := ToFloat64(n) require.NoError(t, err) @@ -338,14 +338,14 @@ func TestToFloat64(t *testing.T) { require.NoError(t, err) assert.InEpsilon(t, 1.0, actual, 1e-12) - z = []interface{}{42, 42.0, float32(42), "42", "42.0", uint8(42), "0x2A", "052"} + z = []any{42, 42.0, float32(42), "42", "42.0", uint8(42), "0x2A", "052"} for _, n := range z { actual, err = ToFloat64(n) require.NoError(t, err) assert.InEpsilon(t, 42.0, actual, 1e-12) } - z = []interface{}{1000.34, "1000.34", "1,000.34"} + z = []any{1000.34, "1000.34", "1,000.34"} for _, n := range z { actual, err = ToFloat64(n) require.NoError(t, err) @@ -382,7 +382,7 @@ func TestToString(t *testing.T) { var n *string testdata := []struct { - in interface{} + in any out string }{ {nil, "nil"}, @@ -414,7 +414,7 @@ func TestToString(t *testing.T) { } func TestToBool(t *testing.T) { - trueData := []interface{}{ + trueData := []any{ true, 1, int8(1), @@ -442,7 +442,7 @@ func TestToBool(t *testing.T) { assert.True(t, out) } - falseData := []interface{}{ + falseData := []any{ nil, false, 42, @@ -464,33 +464,33 @@ func TestToBool(t *testing.T) { func TestDict(t *testing.T) { testdata := []struct { - expected map[string]interface{} - args []interface{} + expected map[string]any + args []any }{ - {expected: map[string]interface{}{}}, + {expected: map[string]any{}}, { - args: []interface{}{}, - expected: map[string]interface{}{}, + args: []any{}, + expected: map[string]any{}, }, { - args: []interface{}{"foo"}, - expected: map[string]interface{}{"foo": ""}, + args: []any{"foo"}, + expected: map[string]any{"foo": ""}, }, { - args: []interface{}{42}, - expected: map[string]interface{}{"42": ""}, + args: []any{42}, + expected: map[string]any{"42": ""}, }, { - args: []interface{}{"foo", nil}, - expected: map[string]interface{}{"foo": nil}, + args: []any{"foo", nil}, + expected: map[string]any{"foo": nil}, }, { - args: []interface{}{"foo", "bar"}, - expected: map[string]interface{}{"foo": "bar"}, + args: []any{"foo", "bar"}, + expected: map[string]any{"foo": "bar"}, }, { - args: []interface{}{"foo", "bar", "baz", true}, - expected: map[string]interface{}{ + args: []any{"foo", "bar", "baz", true}, + expected: map[string]any{ "foo": "bar", "baz": true, }, diff --git a/conv/evalargs.go b/conv/evalargs.go index f7df2ec1..306de54f 100644 --- a/conv/evalargs.go +++ b/conv/evalargs.go @@ -13,7 +13,7 @@ var ( // printableValue returns the, possibly indirected, interface value inside v that // is best for a call to formatted printer. -func printableValue(v reflect.Value) (interface{}, bool) { +func printableValue(v reflect.Value) (any, bool) { if v.Kind() == reflect.Ptr { v, _ = indirect(v) // fmt.Fprint handles nil. } -- cgit v1.2.3