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

improved code quality checks #2

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
77 changes: 54 additions & 23 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,48 @@ run:
timeout: 5m
tests: true
go: '1.22'
allow-parallel-runners: true

linters:
disable-all: true
enable:
- dogsled
- errcheck
- gofumpt
- unconvert
- unparam
- stylecheck
- gocyclo
- errorlint
- copyloopvar
- gci
- goconst
- prealloc
- gocritic
- gofumpt
- gci
- dogsled
- gosec
- errcheck
- goconst
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- nolintlint
- revive
- staticcheck
- stylecheck
- thelper
- revive
- typecheck
- unconvert
- unused
- thelper
- copyloopvar
- wastedassign
- testifylint

linters-settings:
staticcheck:
checks: [ "all", "-SA1019" ]
stylecheck:
checks: [ "all", "-ST1003" ]
gocyclo:
min-complexity: 15
govet:
disable:
- loopclosure
gosec:
excludes: [ "G108", "G115" ]
exclude-generated: true
confidence: medium
revive:
rules:
- name: redefines-builtin-id
disabled: true
gocritic:
disabled-checks: [ "assignOp", "ifElseChain", "appendAssign" ]
misspell:
locale: US
gofumpt:
Expand All @@ -57,8 +56,8 @@ linters-settings:
require-specific: false
gosimple:
checks: [ "all" ]
staticcheck:
checks: [ "all", "-SA1019" ]
gosec:
excludes: [ "G115" ]
gci:
custom-order: true
sections:
Expand All @@ -68,3 +67,35 @@ linters-settings:
errcheck:
check-type-assertions: false
check-blank: false
unused:
field-writes-are-uses: false
exported-fields-are-used: false
local-variables-are-used: false
revive:
# https://golangci-lint.run/usage/linters/#revive
enable-all-rules: true
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
rules:
- name: line-length-limit
disabled: true
- name: add-constant
disabled: true
- name: cognitive-complexity
disabled: false
exclude:
- "**_test.go"
zakir-code marked this conversation as resolved.
Show resolved Hide resolved
- name: function-length
disabled: true
- name: var-naming
arguments:
- [ "ID", "IDS", "URL", "JSON", "RPC" ] # AllowList
- [ "" ] # DenyList
- - upperCaseConst: true
- name: unhandled-error
arguments:
- "fmt.Print"
- "fmt.Println"
- name: import-shadowing
disabled: true
- name: cognitive-complexity
disabled: true
4 changes: 2 additions & 2 deletions dao/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ func (d *BaseDao) BeginTx(ctx context.Context) context.Context {
return context.WithValue(ctx, keyTx, d.db.Begin())
}

func (d *BaseDao) CommitTx(ctx context.Context) error {
func (*BaseDao) CommitTx(ctx context.Context) error {
tx, canOperator := hasOperatorTx(ctx)
if !canOperator {
return nil
}
return tx.Commit()
}

func (d *BaseDao) RollbackTx(ctx context.Context) error {
func (*BaseDao) RollbackTx(ctx context.Context) error {
tx, canOperator := hasOperatorTx(ctx)
if !canOperator {
return nil
Expand Down
4 changes: 2 additions & 2 deletions dao/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type TestModel struct {
Number uint64 `gorm:"column:number; type:bigint(20);not null;comment:block number"`
}

func (v *TestModel) TableName() string {
func (*TestModel) TableName() string {
return "test_model"
}

Expand Down Expand Up @@ -99,7 +99,7 @@ func (s *DaoTestSuite) TestNoTransaction() {
data := NewTestModel("test", 100)
var txErr error
defer func() {
s.Require().NotNil(txErr)
s.Require().Error(txErr)
s.Require().EqualError(txErr, "db create error: UNIQUE constraint failed: test_model.name")
}()
if txErr = s.baseDao.Insert(data); txErr != nil {
Expand Down
4 changes: 2 additions & 2 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c Config) String() string {
return string(out)
}

func (c Config) MarshalYAML() (interface{}, error) {
func (c Config) MarshalYAML() (any, error) {
type marshalConfig Config
temp := marshalConfig(c)
temp.Source = SourceDesensitization(temp.Source)
Expand All @@ -68,7 +68,7 @@ func (c Config) MarshalJSON() ([]byte, error) {
return json.Marshal(temp)
}

func (c Config) Check() error {
func (c Config) Check() error { //nolint:revive // cyclomatic
if c.Driver == "" {
return errors.New("check: driver is empty")
}
Expand Down
2 changes: 1 addition & 1 deletion db/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ refresh_metric_interval: 15s

func (suite *ConfigTestSuite) TestCheck() {
config := db.NewDefConfig()
suite.NoError(config.Check())
suite.Require().NoError(config.Check())

config.Driver = ""
suite.EqualError(config.Check(), "check: driver is empty")
Expand Down
52 changes: 26 additions & 26 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,35 @@ import (
)

type DB interface {
Model(value interface{}) DB
Where(query interface{}, args ...interface{}) DB
Model(value any) DB
Where(query any, args ...any) DB
Limit(limit int) DB
Scopes(funcs ...func(DB) DB) DB
Offset(offset int) DB
Order(value interface{}) DB
Order(value any) DB
Count(count *int64) DB
Group(query string) DB
RowsAffected(number int64) DB

Select(query interface{}, args ...interface{}) DB
Distinct(args ...interface{}) DB
Find(dest interface{}, conds ...interface{}) (err error)
First(dest interface{}, conds ...interface{}) (found bool, err error)
MustFirst(dest interface{}, conds ...interface{}) (err error)
Select(query any, args ...any) DB
Distinct(args ...any) DB
Find(dest any, conds ...any) (err error)
First(dest any, conds ...any) (found bool, err error)
MustFirst(dest any, conds ...any) (err error)

Exec(sql string, values ...interface{}) error
Exec(sql string, values ...any) error

Create(value interface{}) error
Update(column string, value interface{}) error
Updates(values interface{}) error
Create(value any) error
Update(column string, value any) error
Updates(values any) error

Transaction(fn func(tx DB) error) error

Begin() DB
Commit() error
Rollback() error

AutoMigrate(dst ...interface{}) error
AutoMigrate(dst ...any) error

GetSource() string
GetDriver() Driver
Expand Down Expand Up @@ -193,11 +193,11 @@ func (g *gDB) WithContext(ctx context.Context) DB {
return g.copy(g.db.WithContext(ctx))
}

func (g *gDB) Model(value interface{}) DB {
func (g *gDB) Model(value any) DB {
return g.copy(g.db.Model(value))
}

func (g *gDB) Where(query interface{}, args ...interface{}) DB {
func (g *gDB) Where(query any, args ...any) DB {
return g.copy(g.db.Where(query, args...))
}

Expand All @@ -218,7 +218,7 @@ func (g *gDB) Offset(offset int) DB {
return g.copy(g.db.Offset(offset))
}

func (g *gDB) Order(value interface{}) DB {
func (g *gDB) Order(value any) DB {
return g.copy(g.db.Order(value))
}

Expand All @@ -230,15 +230,15 @@ func (g *gDB) Group(query string) DB {
return g.copy(g.db.Group(query))
}

func (g *gDB) Distinct(args ...interface{}) DB {
func (g *gDB) Distinct(args ...any) DB {
return g.copy(g.db.Distinct(args...))
}

func (g *gDB) Select(query interface{}, args ...interface{}) DB {
func (g *gDB) Select(query any, args ...any) DB {
return g.copy(g.db.Select(query, args...))
}

func (g *gDB) Find(dest interface{}, conds ...interface{}) error {
func (g *gDB) Find(dest any, conds ...any) error {
err := g.db.Find(dest, conds...).Error
if err != nil {
g.logger.Error("db find error", "dest", dest, "conds", conds, "error", err)
Expand All @@ -247,7 +247,7 @@ func (g *gDB) Find(dest interface{}, conds ...interface{}) error {
return nil
}

func (g *gDB) First(dest interface{}, conds ...interface{}) (bool, error) {
func (g *gDB) First(dest any, conds ...any) (bool, error) {
err := g.db.First(dest, conds...).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
Expand All @@ -259,7 +259,7 @@ func (g *gDB) First(dest interface{}, conds ...interface{}) (bool, error) {
return true, nil
}

func (g *gDB) MustFirst(dest interface{}, conds ...interface{}) error {
func (g *gDB) MustFirst(dest any, conds ...any) error {
return errors.Wrap(g.db.First(dest, conds...).Error, "db must first error")
}

Expand All @@ -281,7 +281,7 @@ func (g *gDB) Rollback() error {
return g.db.Rollback().Error
}

func (g *gDB) Create(value interface{}) error {
func (g *gDB) Create(value any) error {
tx := g.db.Create(value)
if err := tx.Error; err != nil {
g.logger.Error("db create error", "value", value, "error", err)
Expand All @@ -294,7 +294,7 @@ func (g *gDB) Create(value interface{}) error {
return nil
}

func (g *gDB) Update(column string, value interface{}) error {
func (g *gDB) Update(column string, value any) error {
tx := g.db.Update(column, value)
if err := tx.Error; err != nil {
g.logger.Error("db update error", "column", column, "value", value, "error", err)
Expand All @@ -307,7 +307,7 @@ func (g *gDB) Update(column string, value interface{}) error {
return nil
}

func (g *gDB) Updates(values interface{}) error {
func (g *gDB) Updates(values any) error {
tx := g.db.Updates(values)
if err := tx.Error; err != nil {
g.logger.Error("db updates error", "values", values, "error", err)
Expand All @@ -320,15 +320,15 @@ func (g *gDB) Updates(values interface{}) error {
return nil
}

func (g *gDB) Exec(sql string, values ...interface{}) error {
func (g *gDB) Exec(sql string, values ...any) error {
if err := g.db.Exec(sql, values...).Error; err != nil {
g.logger.Error("db exec error", "sql", sql, "values", values, "error", err)
return errors.Wrap(err, "db exec error")
}
return nil
}

func (g *gDB) AutoMigrate(dst ...interface{}) error {
func (g *gDB) AutoMigrate(dst ...any) error {
return g.db.Transaction(func(tx *gorm.DB) error {
for key, value := range g.driver.MigrateOptions() {
tx = tx.Set(key, value)
Expand Down
Loading