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(lints): all gemini lint warning fixes -> as per current golangci rules #439

Merged
merged 1 commit into from
Nov 29, 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
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ output:
issues:
max-same-issues: 1
max-issues-per-linter: 10
new: true
exclude:
- Error return value of `.*.Unlock` is not checked
- Error return value of `.*.Completed` is not checked
Expand Down
3 changes: 2 additions & 1 deletion cmd/gemini/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func createGenerators(
) (generators.Generators, error) {
partitionRangeConfig := schemaConfig.GetPartitionRangeConfig()

var gs []*generators.Generator
gs := make([]*generators.Generator, 0, len(schema.Tables))

for id := range schema.Tables {
table := schema.Tables[id]
pkVariations := table.PartitionKeys.ValueVariationsNumber(&partitionRangeConfig)
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/statement_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func GetCreateKeyspaces(s *typedef.Schema) (string, string) {
}

func GetCreateSchema(s *typedef.Schema) []string {
var stmts []string
stmts := make([]string, 0, len(s.Tables)*2)

for _, t := range s.Tables {
createTypes := GetCreateTypes(t, s.Keyspace)
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/statement_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func TestGetCreateSchema(t *testing.T) {
t.Parallel()
got := generators.GetCreateTable(test.table, ks)
if diff := cmp.Diff(got, test.want); diff != "" {
t.Fatalf(diff)
t.Fatalf("cmp.Diff failed: %s", diff)
}
})
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/generators/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ func GetCreateTable(t *typedef.Table, ks typedef.Keyspace) string {
t.RLock()
defer t.RUnlock()

var (
partitionKeys []string
clusteringKeys []string
columns []string
)
partitionKeys := make([]string, 0, len(t.PartitionKeys))
clusteringKeys := make([]string, 0, len(t.ClusteringKeys))
columns := make([]string, 0, len(t.PartitionKeys)+len(t.ClusteringKeys)+len(t.Columns))

for _, pk := range t.PartitionKeys {
partitionKeys = append(partitionKeys, pk.Name)
columns = append(columns, fmt.Sprintf("%s %s", pk.Name, pk.Type.CQLDef()))
Expand Down Expand Up @@ -60,7 +59,8 @@ func GetCreateTypes(t *typedef.Table, keyspace typedef.Keyspace) []string {
t.RLock()
defer t.RUnlock()

var stmts []string
stmts := make([]string, 0, len(t.Columns))

for _, column := range t.Columns {
c, ok := column.Type.(*typedef.UDTType)
if !ok {
Expand Down
28 changes: 13 additions & 15 deletions pkg/jobs/gen_check_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ func GenCheckStmt(
rnd *rand.Rand,
p *typedef.PartitionRangeConfig,
) *typedef.Stmt {
n := 0
mvNum := -1
maxClusteringRels := 0
numQueryPKs := 0
if len(table.MaterializedViews) > 0 && rnd.Int()%2 == 0 {
mvNum = utils.RandInt2(rnd, 0, len(table.MaterializedViews))
}

switch mvNum {
case -1:
var n int

if len(table.Indexes) > 0 {
n = rnd.Intn(5)
} else {
Expand All @@ -51,22 +50,22 @@ func GenCheckStmt(
case 0:
return genSinglePartitionQuery(s, table, g)
case 1:
numQueryPKs = utils.RandInt2(rnd, 1, table.PartitionKeys.Len())
numQueryPKs := utils.RandInt2(rnd, 1, table.PartitionKeys.Len())
multiplier := int(math.Pow(float64(numQueryPKs), float64(table.PartitionKeys.Len())))
if multiplier > 100 {
numQueryPKs = 1
}
return genMultiplePartitionQuery(s, table, g, numQueryPKs)
case 2:
maxClusteringRels = utils.RandInt2(rnd, 0, table.ClusteringKeys.Len())
maxClusteringRels := utils.RandInt2(rnd, 0, table.ClusteringKeys.Len())
return genClusteringRangeQuery(s, table, g, rnd, p, maxClusteringRels)
case 3:
numQueryPKs = utils.RandInt2(rnd, 1, table.PartitionKeys.Len())
numQueryPKs := utils.RandInt2(rnd, 1, table.PartitionKeys.Len())
multiplier := int(math.Pow(float64(numQueryPKs), float64(table.PartitionKeys.Len())))
if multiplier > 100 {
numQueryPKs = 1
}
maxClusteringRels = utils.RandInt2(rnd, 0, table.ClusteringKeys.Len())
maxClusteringRels := utils.RandInt2(rnd, 0, table.ClusteringKeys.Len())
return genMultiplePartitionClusteringRangeQuery(s, table, g, rnd, p, numQueryPKs, maxClusteringRels)
case 4:
// Reducing the probability to hit these since they often take a long time to run
Expand All @@ -79,31 +78,30 @@ func GenCheckStmt(
}
}
default:
n = rnd.Intn(4)
switch n {
switch rnd.Intn(4) {
case 0:
return genSinglePartitionQueryMv(s, table, g, rnd, p, mvNum)
case 1:
lenPartitionKeys := table.MaterializedViews[mvNum].PartitionKeys.Len()
numQueryPKs = utils.RandInt2(rnd, 1, lenPartitionKeys)
numQueryPKs := utils.RandInt2(rnd, 1, lenPartitionKeys)
multiplier := int(math.Pow(float64(numQueryPKs), float64(lenPartitionKeys)))
if multiplier > 100 {
numQueryPKs = 1
}
return genMultiplePartitionQueryMv(s, table, g, rnd, p, mvNum, numQueryPKs)
case 2:
lenClusteringKeys := table.MaterializedViews[mvNum].ClusteringKeys.Len()
maxClusteringRels = utils.RandInt2(rnd, 0, lenClusteringKeys)
maxClusteringRels := utils.RandInt2(rnd, 0, lenClusteringKeys)
return genClusteringRangeQueryMv(s, table, g, rnd, p, mvNum, maxClusteringRels)
case 3:
lenPartitionKeys := table.MaterializedViews[mvNum].PartitionKeys.Len()
numQueryPKs = utils.RandInt2(rnd, 1, lenPartitionKeys)
numQueryPKs := utils.RandInt2(rnd, 1, lenPartitionKeys)
multiplier := int(math.Pow(float64(numQueryPKs), float64(lenPartitionKeys)))
if multiplier > 100 {
numQueryPKs = 1
}
lenClusteringKeys := table.MaterializedViews[mvNum].ClusteringKeys.Len()
maxClusteringRels = utils.RandInt2(rnd, 0, lenClusteringKeys)
maxClusteringRels := utils.RandInt2(rnd, 0, lenClusteringKeys)
return genMultiplePartitionClusteringRangeQueryMv(s, table, g, rnd, p, mvNum, numQueryPKs, maxClusteringRels)
}
}
Expand Down Expand Up @@ -285,7 +283,7 @@ func genClusteringRangeQuery(
if vs == nil {
return nil
}
var allTypes []typedef.Type
allTypes := make([]typedef.Type, 0, len(t.PartitionKeys)+maxClusteringRels+1)
values := vs.Value.Copy()
builder := qb.Select(s.Keyspace.Name + "." + t.Name)

Expand Down Expand Up @@ -340,7 +338,7 @@ func genClusteringRangeQueryMv(
}
builder := qb.Select(s.Keyspace.Name + "." + mv.Name)

var allTypes []typedef.Type
allTypes := make([]typedef.Type, 0, len(mv.PartitionKeys)+maxClusteringRels+1)
for _, pk := range mv.PartitionKeys {
builder = builder.Where(qb.Eq(pk.Name))
allTypes = append(allTypes, pk.Type)
Expand Down
4 changes: 3 additions & 1 deletion pkg/jobs/gen_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ func convertStmtToResults(stmt *typedef.Stmt) *result {
types = fmt.Sprintf("%s %s", types, stmt.Types[idx].Name())
}
query, names := stmt.Query.ToCql()
var tokens []resultToken

tokens := make([]resultToken, 0, len(stmt.ValuesWithToken))

for _, valueToken := range stmt.ValuesWithToken {
tokens = append(tokens, resultToken{
Token: fmt.Sprintf("%v", valueToken.Token),
Expand Down
5 changes: 3 additions & 2 deletions pkg/querycache/querycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func genInsertIfNotExistsStmtCache(
}

func genUpdateStmtCache(s *typedef.Schema, t *typedef.Table) *typedef.StmtCache {
var allTypes []typedef.Type
allTypes := make([]typedef.Type, 0, t.PartitionKeys.Len()+t.ClusteringKeys.Len()+t.Columns.Len())
builder := qb.Update(s.Keyspace.Name + "." + t.Name)

for _, cdef := range t.Columns {
Expand Down Expand Up @@ -169,7 +169,8 @@ func genUpdateStmtCache(s *typedef.Schema, t *typedef.Table) *typedef.StmtCache
}

func genDeleteStmtCache(s *typedef.Schema, t *typedef.Table) *typedef.StmtCache {
var allTypes []typedef.Type
allTypes := make([]typedef.Type, 0, t.PartitionKeys.Len()+t.ClusteringKeys.Len())

builder := qb.Delete(s.Keyspace.Name + "." + t.Name)
for _, pk := range t.PartitionKeys {
builder = builder.Where(qb.Eq(pk.Name))
Expand Down
13 changes: 0 additions & 13 deletions pkg/store/cqlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,6 @@ func newSession(cluster *gocql.ClusterConfig, out io.Writer) (*gocql.Session, er
return session, nil
}

func ignore(err error) bool {
if err == nil {
return true
}
//nolint:errorlint
switch {
case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded):
return true
default:
return false
}
}

func opType(stmt *typedef.Stmt) string {
switch stmt.Query.(type) {
case *qb.InsertBuilder:
Expand Down
4 changes: 3 additions & 1 deletion pkg/store/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ import (
)

func pks(t *typedef.Table, rows []map[string]any) []string {
var keySet []string
keySet := make([]string, 0, len(rows))

for _, row := range rows {
keys := make([]string, 0, len(t.PartitionKeys)+len(t.ClusteringKeys))
keys = extractRowValues(keys, t.PartitionKeys, row)
keys = extractRowValues(keys, t.ClusteringKeys, row)
keySet = append(keySet, strings.Join(keys, ", "))
}

return keySet
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/tableopts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func FromCQL(cql string) (Option, error) {
}

func CreateTableOptions(tableOptionStrings []string, logger *zap.Logger) []Option {
var tableOptions []Option
tableOptions := make([]Option, 0, len(tableOptionStrings))

for _, optionString := range tableOptionStrings {
o, err := FromCQL(optionString)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/testutils/name_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ func getTestSchema(table *typedef.Table) (*typedef.Schema, *typedef.SchemaConfig
func createTableOptions(cql string) []tableopts.Option {
opt, _ := tableopts.FromCQL(cql)
opts := []string{opt.ToCQL()}
var tableOptions []tableopts.Option

tableOptions := make([]tableopts.Option, 0, len(opts))

for _, optionString := range opts {
o, err := tableopts.FromCQL(optionString)
Expand Down
19 changes: 12 additions & 7 deletions pkg/typedef/columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ func TestColumnMarshalUnmarshal(t *testing.T) {
def typedef.ColumnDef
expected string
}
var testCases []testCase

testCases := make([]testCase, 0, len(allSimpleTypes))

for _, simpleType := range allSimpleTypes {
testCases = append(testCases, testCase{def: typedef.ColumnDef{
Name: simpleType.Name(),
Type: simpleType,
}, expected: fmt.Sprintf("{\"type\":\"%s\",\"name\":\"%s\"}", simpleType.Name(), simpleType.Name())})
testCases = append(testCases, testCase{
def: typedef.ColumnDef{
Name: simpleType.Name(),
Type: simpleType,
},
expected: fmt.Sprintf("{\"type\":\"%s\",\"name\":\"%s\"}", simpleType.Name(), simpleType.Name()),
})
}
udtTypes := map[string]typedef.SimpleType{}

Expand Down Expand Up @@ -94,7 +99,7 @@ func TestColumnMarshalUnmarshal(t *testing.T) {
fmt.Println(tcase.expected)

if diff := cmp.Diff(string(marshaledData), tcase.expected); diff != "" {
t.Errorf(diff)
t.Errorf("Comparison failed: %s", diff)
}
var unmarshaledDef typedef.ColumnDef
err = json.Unmarshal(marshaledData, &unmarshaledDef)
Expand All @@ -103,7 +108,7 @@ func TestColumnMarshalUnmarshal(t *testing.T) {
}

if diff := cmp.Diff(tcase.def, unmarshaledDef); diff != "" {
t.Errorf(diff)
t.Errorf("cmp.Diff failed: %s", diff)
}
})
}
Expand Down
1 change: 1 addition & 0 deletions pkg/typedef/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func TestCQLPretty(t *testing.T) {
for id := range prettytests {
test := prettytests[id]
t.Run(test.typ.Name(), func(t *testing.T) {
t.Parallel()
builder := bytes.NewBuffer(nil)
err := prettyCQL(builder, test.query, test.values, []Type{test.typ})
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func RandIPV4Address(rnd *rand.Rand, v, pos int) string {
return strings.Join(blocks, ".")
}

func RandInt2(rnd *rand.Rand, min, max int) int {
if max <= min {
return min
func RandInt2(rnd *rand.Rand, minimum, maximum int) int {
if maximum <= minimum {
return minimum
}
return min + rnd.Intn(max-min)
return minimum + rnd.Intn(maximum-minimum)
}

func IgnoreError(fn func() error) {
Expand Down
Loading