Skip to content

Commit

Permalink
Merge pull request #2 from PundiAI/zakir/lint
Browse files Browse the repository at this point in the history
improved code quality checks
  • Loading branch information
zakir-code authored Nov 20, 2024
2 parents bcb40b2 + e3eda83 commit 22e53aa
Show file tree
Hide file tree
Showing 39 changed files with 266 additions and 244 deletions.
73 changes: 50 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,31 @@ 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: true
- 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
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
6 changes: 3 additions & 3 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Config struct {
func NewDefConfig() Config {
return Config{
Driver: SqliteDriver,
Source: os.ExpandEnv("$HOME/.coastdao/coastdao.db"),
Source: os.ExpandEnv("$HOME/.my/my.db"),
ConnMaxIdleTime: time.Hour,
ConnMaxLifeTime: time.Hour,
MaxIdleConn: 10,
Expand All @@ -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
22 changes: 11 additions & 11 deletions db/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type ConfigTestSuite struct {

func (suite *ConfigTestSuite) TestNewDefConfig() {
config := db.NewDefConfig()
config.Source = "root:root@tcp(127.0.0.1:3306)/coastdao?charset=utf8mb4&parseTime=True&loc=Local"
config.Source = "root:root@tcp(127.0.0.1:3306)/my?charset=utf8mb4&parseTime=True&loc=Local"
suite.Equal(`driver: sqlite
source: '****:****@tcp(127.0.0.1:3306)/coastdao?charset=utf8mb4&parseTime=True&loc=Local'
source: '****:****@tcp(127.0.0.1:3306)/my?charset=utf8mb4&parseTime=True&loc=Local'
conn_max_idle_time: 1h0m0s
conn_max_life_time: 1h0m0s
max_idle_conn: 10
Expand All @@ -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 All @@ -47,23 +47,23 @@ func TestSourceDesensitization(t *testing.T) {
}{
{
name: "test1",
source: "coastdao.db",
want: "coastdao.db",
source: "my.db",
want: "my.db",
},
{
name: "test2",
source: "root:root@tcp(127.0.0.1:3306)/coastdao?charset=utf8mb4&parseTime=True&loc=Local",
want: "****:****@tcp(127.0.0.1:3306)/coastdao?charset=utf8mb4&parseTime=True&loc=Local",
source: "root:root@tcp(127.0.0.1:3306)/my?charset=utf8mb4&parseTime=True&loc=Local",
want: "****:****@tcp(127.0.0.1:3306)/my?charset=utf8mb4&parseTime=True&loc=Local",
},
{
name: "test3",
source: "root@tcp(127.0.0.1:3306)/coastdao?charset=utf8mb4&parseTime=True&loc=Local",
want: "*:*@tcp(127.0.0.1:3306)/coastdao?charset=utf8mb4&parseTime=True&loc=Local",
source: "root@tcp(127.0.0.1:3306)/my?charset=utf8mb4&parseTime=True&loc=Local",
want: "*:*@tcp(127.0.0.1:3306)/my?charset=utf8mb4&parseTime=True&loc=Local",
},
{
name: "test4",
source: "tcp(127.0.0.1:3306)/coastdao?charset=utf8mb4&parseTime=True&loc=Local",
want: "tcp(127.0.0.1:3306)/coastdao?charset=utf8mb4&parseTime=True&loc=Local",
source: "tcp(127.0.0.1:3306)/my?charset=utf8mb4&parseTime=True&loc=Local",
want: "tcp(127.0.0.1:3306)/my?charset=utf8mb4&parseTime=True&loc=Local",
},
}
for _, tt := range tests {
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

0 comments on commit 22e53aa

Please sign in to comment.