Skip to content

Commit

Permalink
Update golangci-lint and turn it on in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
dkropachev committed Jun 15, 2024
1 parent a9ab270 commit 4ec08b4
Show file tree
Hide file tree
Showing 37 changed files with 222 additions and 151 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,9 @@ jobs:
- name: Download Dependencies
run: make get-deps

# - name: Lint
# uses: golangci/golangci-lint-action@v3
# with:
# version: v1.45.2

- run: go vet ./...

- name: Lint
run: make check

- name: Run Scylla Container
run: make run-scylla

Expand Down
62 changes: 49 additions & 13 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
run:
deadline: 5m
tests: false
tests: true
allow-parallel-runners: true
modules-download-mode: readonly
build-tags: [ all, integration ]

linters-settings:
revive:
rules:
- name: package-comments
disabled: true
goimports:
local-prefixes: github.com/scylladb/gocqlx
gofumpt:
extra-rules: true
govet:
enable-all: true
disable:
- shadow
errcheck:
check-blank: true
gocognit:
Expand All @@ -23,22 +38,43 @@ linters-settings:
line-length: 180

linters:
enable-all: true
disable:
- funlen
- gas
- gochecknoglobals
- gochecknoinits
- gomnd
- interfacer
- maligned
- nakedret
- prealloc
- wsl
disable-all: true
enable:
- errcheck
- gocritic
- gofumpt
- goheader
- goimports
- gosimple
- govet
- ineffassign
- lll
- misspell
- predeclared
- revive
- staticcheck
- thelper
- tparallel
- typecheck
- unused
- forbidigo

issues:
new: true
new-from-rev: origin/master
exclude-use-default: false
exclude:
- composite literal uses unkeyed fields
- Error return value of `.+\.Close` is not checked
- method Json should be JSON
exclude-rules:
- path: (.*_test.go|migrate/example|gocqlxtest/)
linters:
- fieldalignment
- govet
- errcheck
- path: doc_test.go
linters:
- unused
- revive

8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ fmt:
check:
@$(GOBIN)/golangci-lint run ./...

.PHONY: fix
fix:
@$(GOBIN)/golangci-lint run --fix ./...
@$(GOBIN)/fieldalignment -fix -test false ./...

GOTEST := go test -cpu $(GOTEST_CPU) -count=1 -cover -race -tags all

.PHONY: test
Expand Down Expand Up @@ -69,4 +74,5 @@ endef
.PHONY: get-tools
get-tools:
@echo "==> Installing tools at $(GOBIN)..."
@$(call dl_tgz,golangci-lint,https://github.com/golangci/golangci-lint/releases/download/v1.45.2/golangci-lint-v1.45.2-linux-amd64.tar.gz)
@$(call dl_tgz,golangci-lint,https://github.com/golangci/golangci-lint/releases/download/v1.59.1/golangci-lint-1.59.1-linux-amd64.tar.gz)
go install golang.org/x/[email protected]/go/analysis/passes/fieldalignment/cmd/fieldalignment
1 change: 1 addition & 0 deletions batchx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/gocql/gocql"
)

// Batch a batch operations.
type Batch struct {
*gocql.Batch
}
Expand Down
1 change: 1 addition & 0 deletions batchx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/gocql/gocql"
"github.com/google/go-cmp/cmp"

"github.com/scylladb/gocqlx/v2"
"github.com/scylladb/gocqlx/v2/gocqlxtest"
"github.com/scylladb/gocqlx/v2/qb"
Expand Down
18 changes: 10 additions & 8 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"

"github.com/scylladb/gocqlx/v2"
. "github.com/scylladb/gocqlx/v2/gocqlxtest"
"github.com/scylladb/gocqlx/v2/gocqlxtest"
"github.com/scylladb/gocqlx/v2/qb"
)

Expand Down Expand Up @@ -46,7 +46,7 @@ var benchPersonCols = []string{"id", "first_name", "last_name", "email", "gender
// BenchmarkBaseGocqlInsert performs standard insert.
func BenchmarkBaseGocqlInsert(b *testing.B) {
people := loadFixtures()
session := CreateSession(b)
session := gocqlxtest.CreateSession(b)
defer session.Close()

if err := session.ExecStmt(benchPersonSchema); err != nil {
Expand All @@ -69,7 +69,7 @@ func BenchmarkBaseGocqlInsert(b *testing.B) {
// BenchmarkGocqlInsert performs insert with struct binding.
func BenchmarkGocqlxInsert(b *testing.B) {
people := loadFixtures()
session := CreateSession(b)
session := gocqlxtest.CreateSession(b)
defer session.Close()

if err := session.ExecStmt(benchPersonSchema); err != nil {
Expand All @@ -96,7 +96,7 @@ func BenchmarkGocqlxInsert(b *testing.B) {
// BenchmarkBaseGocqlGet performs standard scan.
func BenchmarkBaseGocqlGet(b *testing.B) {
people := loadFixtures()
session := CreateSession(b)
session := gocqlxtest.CreateSession(b)
defer session.Close()

initTable(b, session, people)
Expand All @@ -119,7 +119,7 @@ func BenchmarkBaseGocqlGet(b *testing.B) {
// BenchmarkGocqlxGet performs get.
func BenchmarkGocqlxGet(b *testing.B) {
people := loadFixtures()
session := CreateSession(b)
session := gocqlxtest.CreateSession(b)
defer session.Close()

initTable(b, session, people)
Expand Down Expand Up @@ -147,7 +147,7 @@ func BenchmarkGocqlxGet(b *testing.B) {
// pointers.
func BenchmarkBaseGocqlSelect(b *testing.B) {
people := loadFixtures()
session := CreateSession(b)
session := gocqlxtest.CreateSession(b)
defer session.Close()

initTable(b, session, people)
Expand All @@ -162,7 +162,7 @@ func BenchmarkBaseGocqlSelect(b *testing.B) {
v := make([]*benchPerson, 100)
p := new(benchPerson)
for iter.Scan(&p.ID, &p.FirstName, &p.LastName, &p.Email, &p.Gender, &p.IPAddress) {
v = append(v, p)
v = append(v, p) // nolint:staticcheck
p = new(benchPerson)
}
if err := iter.Close(); err != nil {
Expand All @@ -174,7 +174,7 @@ func BenchmarkBaseGocqlSelect(b *testing.B) {
// BenchmarkGocqlSelect performs select to a slice pointers.
func BenchmarkGocqlxSelect(b *testing.B) {
people := loadFixtures()
session := CreateSession(b)
session := gocqlxtest.CreateSession(b)
defer session.Close()

initTable(b, session, people)
Expand Down Expand Up @@ -212,6 +212,8 @@ func loadFixtures() []*benchPerson {
}

func initTable(b *testing.B, session gocqlx.Session, people []*benchPerson) {
b.Helper()

if err := session.ExecStmt(benchPersonSchema); err != nil {
b.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/schemagen/map_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func mapScyllaToGoType(s string) string {

typeStr := "struct {\n"
for i, t := range types {
typeStr = typeStr + "\t\tField" + strconv.Itoa(i+1) + " " + mapScyllaToGoType(t) + "\n"
typeStr += "\t\tField" + strconv.Itoa(i+1) + " " + mapScyllaToGoType(t) + "\n"
}
typeStr = typeStr + "\t}"
typeStr += "\t}"

return typeStr
}
Expand All @@ -96,8 +96,8 @@ func typeToString(t interface{}) string {
return t.(gocql.NativeType).String()
case "gocql.CollectionType":
collectionType := t.(gocql.CollectionType).String()
collectionType = strings.Replace(collectionType, "(", "<", -1)
collectionType = strings.Replace(collectionType, ")", ">", -1)
collectionType = strings.ReplaceAll(collectionType, "(", "<")
collectionType = strings.ReplaceAll(collectionType, ")", ">")
return collectionType
default:
panic(fmt.Sprintf("Did not expect %v type in user defined type", tType))
Expand Down
10 changes: 4 additions & 6 deletions cmd/schemagen/schemagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"

"github.com/gocql/gocql"

"github.com/scylladb/gocqlx/v2"
_ "github.com/scylladb/gocqlx/v2/table"
)
Expand All @@ -31,10 +32,8 @@ var (
flagIgnoreIndexes = cmd.Bool("ignore-indexes", false, "don't generate types for indexes")
)

var (
//go:embed keyspace.tmpl
keyspaceTmpl string
)
//go:embed keyspace.tmpl
var keyspaceTmpl string

func main() {
err := cmd.Parse(os.Args[1:])
Expand Down Expand Up @@ -80,7 +79,6 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
Funcs(template.FuncMap{"mapScyllaToGoType": mapScyllaToGoType}).
Funcs(template.FuncMap{"typeToString": typeToString}).
Parse(keyspaceTmpl)

if err != nil {
log.Fatalln("unable to parse models template:", err)
}
Expand Down Expand Up @@ -169,7 +167,7 @@ func existsInSlice(s []string, v string) bool {
// The second element contains the name of the type.
//
// [["<my_type,", "my_type"] ["my_other_type>", "my_other_type"]]
var userTypes = regexp.MustCompile(`(?:<|\s)(\w+)(?:>|,)`) // match all types contained in set<X>, list<X>, tuple<A, B> etc.
var userTypes = regexp.MustCompile(`(?:<|\s)(\w+)[>,]`) // match all types contained in set<X>, list<X>, tuple<A, B> etc.

// usedInTables reports whether the typeName is used in any of columns of the provided tables.
func usedInTables(typeName string, tables map[string]*gocql.TableMetadata) bool {
Expand Down
3 changes: 2 additions & 1 deletion cmd/schemagen/schemagen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/gocql/gocql"
"github.com/google/go-cmp/cmp"

"github.com/scylladb/gocqlx/v2/gocqlxtest"
)

Expand Down Expand Up @@ -105,7 +106,7 @@ func Test_usedInTables(t *testing.T) {
})
}

t.Run("doesn't panic with empty type name", func(t *testing.T) {
t.Run("doesn't panic with empty type name", func(_ *testing.T) {
tables := map[string]*gocql.TableMetadata{
"table": {Columns: map[string]*gocql.ColumnMetadata{
"column": {Validator: "map<text, album>"},
Expand Down
6 changes: 3 additions & 3 deletions dbutil/rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
"time"

"github.com/scylladb/gocqlx/v2/dbutil"
. "github.com/scylladb/gocqlx/v2/gocqlxtest"
"github.com/scylladb/gocqlx/v2/gocqlxtest"
"github.com/scylladb/gocqlx/v2/qb"
"github.com/scylladb/gocqlx/v2/table"
)

func TestRewriteTableTTL(t *testing.T) {
session := CreateSession(t)
session := gocqlxtest.CreateSession(t)
defer session.Close()

if err := session.ExecStmt(`CREATE TABLE gocqlx_test.rewrite_table (testtext text PRIMARY KEY)`); err != nil {
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestRewriteTableTTL(t *testing.T) {
}

func TestRewriteTableClone(t *testing.T) {
session := CreateSession(t)
session := gocqlxtest.CreateSession(t)
defer session.Close()

if err := session.ExecStmt(`CREATE TABLE gocqlx_test.rewrite_table_clone_src (testtext text PRIMARY KEY, testint int)`); err != nil {
Expand Down
1 change: 1 addition & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package gocqlx_test

import (
"github.com/gocql/gocql"

"github.com/scylladb/gocqlx/v2"
"github.com/scylladb/gocqlx/v2/qb"
)
Expand Down
Loading

0 comments on commit 4ec08b4

Please sign in to comment.