From 73f79a093312a8a7020fe0c610b7fcf652f70dab Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sun, 10 Nov 2024 15:19:25 -0500 Subject: [PATCH] fix(lint): Address new lint warnings from golangci-lint 1.62 Signed-off-by: Dave Henderson --- coll/index.go | 2 ++ internal/datafs/reader_test.go | 4 ++-- internal/funcs/path_test.go | 2 +- internal/funcs/random.go | 24 ++++++++++++------------ internal/funcs/strings.go | 8 +++++--- internal/texttemplate/funcs.go | 2 ++ random/random.go | 12 ++++++++---- 7 files changed, 32 insertions(+), 22 deletions(-) diff --git a/coll/index.go b/coll/index.go index b3b2458b2..ba8153202 100644 --- a/coll/index.go +++ b/coll/index.go @@ -64,6 +64,8 @@ func Index(v interface{}, keys ...interface{}) (interface{}, error) { } // indexArg checks if a reflect.Value can be used as an index, and converts it to int if possible. +// +//nolint:revive func indexArg(index reflect.Value, cap int) (int, error) { var x int64 switch index.Kind() { diff --git a/internal/datafs/reader_test.go b/internal/datafs/reader_test.go index 2ec7485c2..588592fd0 100644 --- a/internal/datafs/reader_test.go +++ b/internal/datafs/reader_test.go @@ -101,7 +101,7 @@ func TestReadFileContent(t *testing.T) { fc, err := sr.readFileContent(ctx, mustParseURL("file:///foo.json"), nil) require.NoError(t, err) - assert.Equal(t, []byte(`{"foo": "bar"}`), fc.b) + assert.JSONEq(t, `{"foo": "bar"}`, string(fc.b)) fc, err = sr.readFileContent(ctx, mustParseURL("dir/"), nil) require.NoError(t, err) @@ -109,7 +109,7 @@ func TestReadFileContent(t *testing.T) { fc, err = sr.readFileContent(ctx, mustParseURL(srv.URL+"/foo.json"), nil) require.NoError(t, err) - assert.Equal(t, []byte(`{"foo": "bar"}`), fc.b) + assert.JSONEq(t, `{"foo": "bar"}`, string(fc.b)) } func TestDatasource(t *testing.T) { diff --git a/internal/funcs/path_test.go b/internal/funcs/path_test.go index a314933b3..aa4d13220 100644 --- a/internal/funcs/path_test.go +++ b/internal/funcs/path_test.go @@ -35,7 +35,7 @@ func TestPathFuncs(t *testing.T) { assert.Equal(t, "/foo/baz", p.Clean("/foo/bar/../baz")) assert.Equal(t, "foo", p.Dir("foo/bar")) assert.Equal(t, ".txt", p.Ext("/foo/bar/baz.txt")) - assert.False(t, false, p.IsAbs("foo/bar")) + assert.False(t, p.IsAbs("foo/bar")) assert.True(t, p.IsAbs("/foo/bar")) assert.Equal(t, "foo/bar/qux", p.Join("foo", "bar", "baz", "..", "qux")) m, _ := p.Match("*.txt", "foo.json") diff --git a/internal/funcs/random.go b/internal/funcs/random.go index 0f18ae927..afadfef45 100644 --- a/internal/funcs/random.go +++ b/internal/funcs/random.go @@ -141,58 +141,58 @@ func (RandomFuncs) Item(items interface{}) (interface{}, error) { // Number - func (RandomFuncs) Number(args ...interface{}) (int64, error) { - var min, max int64 - min, max = 0, 100 + var nMin, nMax int64 + nMin, nMax = 0, 100 var err error switch len(args) { case 0: case 1: - max, err = conv.ToInt64(args[0]) + nMax, err = conv.ToInt64(args[0]) if err != nil { return 0, fmt.Errorf("max must be a number: %w", err) } case 2: - min, err = conv.ToInt64(args[0]) + nMin, err = conv.ToInt64(args[0]) if err != nil { return 0, fmt.Errorf("min must be a number: %w", err) } - max, err = conv.ToInt64(args[1]) + nMax, err = conv.ToInt64(args[1]) if err != nil { return 0, fmt.Errorf("max must be a number: %w", err) } } - return random.Number(min, max) + return random.Number(nMin, nMax) } // Float - func (RandomFuncs) Float(args ...interface{}) (float64, error) { - var min, max float64 - min, max = 0, 1.0 + var nMin, nMax float64 + nMin, nMax = 0, 1.0 var err error switch len(args) { case 0: case 1: - max, err = conv.ToFloat64(args[0]) + nMax, err = conv.ToFloat64(args[0]) if err != nil { return 0, fmt.Errorf("max must be a number: %w", err) } case 2: - min, err = conv.ToFloat64(args[0]) + nMin, err = conv.ToFloat64(args[0]) if err != nil { return 0, fmt.Errorf("min must be a number: %w", err) } - max, err = conv.ToFloat64(args[1]) + nMax, err = conv.ToFloat64(args[1]) if err != nil { return 0, fmt.Errorf("max must be a number: %w", err) } } - return random.Float(min, max) + return random.Float(nMin, nMax) } diff --git a/internal/funcs/strings.go b/internal/funcs/strings.go index a0afc0725..44054f9b9 100644 --- a/internal/funcs/strings.go +++ b/internal/funcs/strings.go @@ -152,6 +152,8 @@ func (StringFuncs) Abbrev(args ...interface{}) (string, error) { } // ReplaceAll - +// +//nolint:revive func (StringFuncs) ReplaceAll(old, new string, s interface{}) string { return strings.ReplaceAll(conv.ToString(s), old, new) } @@ -322,10 +324,10 @@ func (StringFuncs) ShellQuote(in interface{}) string { switch val.Kind() { case reflect.Array, reflect.Slice: var sb strings.Builder - max := val.Len() - for n := 0; n < max; n++ { + vLen := val.Len() + for n := 0; n < vLen; n++ { sb.WriteString(gompstrings.ShellQuote(conv.ToString(val.Index(n)))) - if n+1 != max { + if n+1 != vLen { sb.WriteRune(' ') } } diff --git a/internal/texttemplate/funcs.go b/internal/texttemplate/funcs.go index 08e6bdbbf..e2f7d1506 100644 --- a/internal/texttemplate/funcs.go +++ b/internal/texttemplate/funcs.go @@ -12,6 +12,8 @@ import ( ) // indexArg checks if a reflect.Value can be used as an index, and converts it to int if possible. +// +//nolint:revive func indexArg(index reflect.Value, cap int) (int, error) { var x int64 switch index.Kind() { diff --git a/random/random.go b/random/random.go index e3fd23906..ad0bb48a2 100644 --- a/random/random.go +++ b/random/random.go @@ -4,7 +4,7 @@ package random import ( "fmt" "math" - "math/rand" + "math/rand/v2" "regexp" "unicode" ) @@ -43,7 +43,7 @@ func rndString(count int, chars []rune) (string, error) { s := make([]rune, count) for i := range s { //nolint:gosec - s[i] = chars[rand.Intn(len(chars))] + s[i] = chars[rand.IntN(len(chars))] } return string(s), nil } @@ -83,11 +83,13 @@ func Item(items []interface{}) (interface{}, error) { } //nolint:gosec - n := rand.Intn(len(items)) + n := rand.IntN(len(items)) return items[n], nil } // Number - +// +//nolint:revive func Number(min, max int64) (int64, error) { if min > max { return 0, fmt.Errorf("min must not be greater than max (was %d, %d)", min, max) @@ -100,10 +102,12 @@ func Number(min, max int64) (int64, error) { } //nolint:gosec - return rand.Int63n(max-min+1) + min, nil + return rand.Int64N(max-min+1) + min, nil } // Float - For now this is really just a wrapper around `rand.Float64` +// +//nolint:revive func Float(min, max float64) (float64, error) { //nolint:gosec return min + rand.Float64()*(max-min), nil