Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lint): Address new lint warnings from golangci-lint 1.62 #2256

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions coll/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions internal/datafs/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ 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)
assert.JSONEq(t, `["1.yaml", "2.yaml", "sub"]`, string(fc.b))

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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/funcs/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
24 changes: 12 additions & 12 deletions internal/funcs/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 5 additions & 3 deletions internal/funcs/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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(' ')
}
}
Expand Down
2 changes: 2 additions & 0 deletions internal/texttemplate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 8 additions & 4 deletions random/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package random
import (
"fmt"
"math"
"math/rand"
"math/rand/v2"
"regexp"
"unicode"
)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down