Skip to content

Commit

Permalink
Merge pull request #124 from mazrean/dev/add-golangci-lint
Browse files Browse the repository at this point in the history
golangci-lint設定
mazrean authored Apr 7, 2023

Verified

This commit was signed with the committer’s verified signature.
utkarshgupta137 Utkarsh Gupta
2 parents 1d71840 + 7cf49d1 commit da30f56
Showing 12 changed files with 84 additions and 58 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -20,6 +20,23 @@ jobs:
with:
name: genorm
path: ./genorm
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# go generate用に、golangci-lintの前にGoのinstallをする
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
- run: go generate ./...
- name: golangci-lint
uses: reviewdog/action-golangci-lint@v2.2
with:
go_version_file: go.mod
reporter: github-pr-review
github_token: ${{ secrets.GITHUB_TOKEN }}
fail_on_error: true
test:
name: Test
runs-on: ubuntu-latest
38 changes: 38 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
run:
skip-dirs:
- mock
linters:
enable:
- govet
- errcheck
- staticcheck
- unused
- gosimple
- structcheck
- varcheck
- ineffassign
- typecheck
- revive
- gofmt
- asasalint
- asciicheck
- bidichk
- errname
- errorlint
- exhaustive
- exportloopref
- forcetypeassert
- gocheckcompilerdirectives
- gocritic
- goheader
- goimports
- gosec
- misspell
- nakedret
- nilerr
- nosprintfhostport
- sqlclosecheck
- testpackage
- unconvert
- unparam
- whitespace
2 changes: 2 additions & 0 deletions clause.go
Original file line number Diff line number Diff line change
@@ -238,6 +238,8 @@ func (l *lockClause) getExpr() (string, []ExprType, error) {
return "FOR UPDATE", nil, nil
case ForShare:
return "FOR SHARE", nil, nil
case none:
return "", nil, nil
}

return "", nil, errors.New("invalid lock type")
6 changes: 6 additions & 0 deletions clause_exporter_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package genorm

//nolint:revive
func NewWhereConditionClause[T Table](condition TypedTableExpr[T, WrappedPrimitive[bool]]) *whereConditionClause[T] {
return &whereConditionClause[T]{
condition: condition,
@@ -22,6 +23,7 @@ func (c *whereConditionClause[T]) GetExpr() (string, []ExprType, error) {
return c.getExpr()
}

//nolint:revive
func NewGroupClause[T Table](exprs []TableExpr[T]) *groupClause[T] {
return &groupClause[T]{
exprs: exprs,
@@ -57,6 +59,7 @@ func (c *OrderItem[T]) Value() (TableExpr[T], OrderDirection) {
return c.expr, c.direction
}

//nolint:revive
func NewOrderClause[T Table](items []OrderItem[T]) *orderClause[T] {
orderItems := make([]orderItem[T], 0, len(items))
for _, item := range items {
@@ -89,6 +92,7 @@ func (c *orderClause[T]) GetExpr() (string, []ExprType, error) {
return c.getExpr()
}

//nolint:revive
func NewLimitClause(limit uint64) *limitClause {
return &limitClause{
limit: limit,
@@ -111,6 +115,7 @@ func (c *limitClause) GetExpr() (string, []ExprType, error) {
return c.getExpr()
}

//nolint:revive
func NewOffsetClause(offset uint64) *offsetClause {
return &offsetClause{
offset: offset,
@@ -133,6 +138,7 @@ func (c *offsetClause) GetExpr() (string, []ExprType, error) {
return c.getExpr()
}

//nolint:revive
func NewLockClause(lockType LockType) *lockClause {
return &lockClause{
lockType: lockType,
19 changes: 6 additions & 13 deletions clause_test.go
Original file line number Diff line number Diff line change
@@ -62,10 +62,8 @@ func TestWhereConditionClauseSetTest(t *testing.T) {
if test.err {
assert.Error(t, err)
return
} else {
if !assert.NoError(t, err) {
return
}
} else if !assert.NoError(t, err) {
return
}

assert.Equal(t, setExpr, c.GetCondition())
@@ -168,10 +166,8 @@ func TestWhereConditionClauseGetExprTest(t *testing.T) {
if test.err {
assert.Error(t, err)
return
} else {
if !assert.NoError(t, err) {
return
}
} else if !assert.NoError(t, err) {
return
}

assert.Equal(t, test.expr.query, query)
@@ -186,7 +182,6 @@ func TestGroupClauseSetTest(t *testing.T) {
type expr struct {
query string
args []genorm.ExprType
errs []error
}

tests := []struct {
@@ -289,7 +284,6 @@ func TestGroupClauseExistTest(t *testing.T) {
type expr struct {
query string
args []genorm.ExprType
errs []error
}

tests := []struct {
@@ -446,7 +440,6 @@ func TestOrderClauseAddTest(t *testing.T) {
type expr struct {
query string
args []genorm.ExprType
errs []error
}

type orderItem struct {
@@ -770,7 +763,7 @@ func TestOrderClauseGetExprTest(t *testing.T) {
args: []genorm.ExprType{genorm.Wrap(1)},
},
{
description: "invlid direction",
description: "invalid direction",
items: []orderItem{
{
expr: &expr{
@@ -1222,7 +1215,7 @@ func TestLockClauseGetExprTest(t *testing.T) {
},
{
description: "empty lock type",
err: true,
query: "",
},
}

1 change: 1 addition & 0 deletions cmd/genorm/generator/convert/convert.go
Original file line number Diff line number Diff line change
@@ -68,6 +68,7 @@ func (cjt *converterJoinedTable) tablesHash(tableNum int) int64 {
return joinedTableIDs[i] < joinedTableIDs[j]
})

//nolint:revive
var joinedTableHash int64 = 0
var key int64 = 1
for _, joinTableID := range joinedTableIDs {
19 changes: 8 additions & 11 deletions cmd/genorm/generator/parser/parser.go
Original file line number Diff line number Diff line change
@@ -45,10 +45,7 @@ func Parse(f *ast.File) ([]*types.Table, error) {
continue
}

method, isMethod, err := parseFuncDecl(funcDecl)
if err != nil {
return nil, fmt.Errorf("parse func: %w", err)
}
method, isMethod := parseFuncDecl(funcDecl)

if isMethod {
methodMap[method.StructName] = append(methodMap[method.StructName], method)
@@ -138,41 +135,41 @@ func convertTable(table *parserTable) *types.Table {
}
}

func parseFuncDecl(f *ast.FuncDecl) (*parserMethod, bool, error) {
func parseFuncDecl(f *ast.FuncDecl) (*parserMethod, bool) {
recv := f.Recv
if recv == nil {
return nil, false, nil
return nil, false
}

if len(recv.List) == 0 {
return nil, false, nil
return nil, false
}

recvType := recv.List[0].Type
identType, ok := recvType.(*ast.Ident)
if !ok {
starType, ok := recvType.(*ast.StarExpr)
if !ok {
return nil, false, nil
return nil, false
}

identType, ok = starType.X.(*ast.Ident)
if !ok {
return nil, false, nil
return nil, false
}

return &parserMethod{
StructName: identType.Name,
Type: types.MethodTypeStar,
Decl: f,
}, true, nil
}, true
}

return &parserMethod{
StructName: identType.Name,
Type: types.MethodTypeIdentifier,
Decl: f,
}, true, nil
}, true
}

func parseGenDecl(g *ast.GenDecl) ([]*parserTable, error) {
13 changes: 1 addition & 12 deletions cmd/genorm/generator/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -205,7 +205,6 @@ func TestParseFuncDecl(t *testing.T) {
f *ast.FuncDecl
method *parserMethod
isMethod bool
err bool
}{
{
description: "normal func -> not method",
@@ -253,17 +252,7 @@ func TestParseFuncDecl(t *testing.T) {

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
method, isMethod, err := parseFuncDecl(test.f)
if err != nil {
if !test.err {
t.Fatalf("failed to parse func decl: %s", err)
}
return
}

if test.err {
t.Fatalf("expected error but got no error")
}
method, isMethod := parseFuncDecl(test.f)

if isMethod != test.isMethod {
t.Fatalf("is method is not match")
2 changes: 1 addition & 1 deletion cmd/genorm/main.go
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ func printVersionInfo() error {

func openSource(source string) (io.ReadCloser, error) {
if len(source) == 0 {
return nil, errors.New("Source file is required.")
return nil, errors.New("empty source file")
}

file, err := os.Open(source)
17 changes: 0 additions & 17 deletions insert_test.go
Original file line number Diff line number Diff line change
@@ -12,23 +12,6 @@ import (
func TestInsertBuildQuery(t *testing.T) {
t.Parallel()

type field struct {
tableName string
columnName string
sqlColumnName string
}

type expr struct {
query string
args []genorm.ExprType
errs []error
}

type orderItem struct {
direction genorm.OrderDirection
expr expr
}

columnFieldExpr1 := genorm.Wrap(1)
columnFieldExpr2 := genorm.Wrap(2)
columnFieldNull := genorm.WrappedPrimitive[int]{}
2 changes: 2 additions & 0 deletions relation/relation.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import (
"github.com/mazrean/genorm"
)

//nolint:revive
type RelationContext[S Table, T Table, _ JoinedTablePointer[V], V any] struct {
baseTable S
refTable T
@@ -185,6 +186,7 @@ func (r *Relation) JoinedTableName() (string, []genorm.ExprType, []error) {
return sb.String(), args, nil
}

//nolint:revive
type RelationType int8

const (
6 changes: 2 additions & 4 deletions relation/relation_test.go
Original file line number Diff line number Diff line change
@@ -207,10 +207,8 @@ func TestJoinedTableName(t *testing.T) {
if test.err {
assert.Greater(t, len(errs), 0)
return
} else {
if !assert.Len(t, errs, 0) {
return
}
} else if !assert.Len(t, errs, 0) {
return
}

assert.Equal(t, test.query, query)

0 comments on commit da30f56

Please sign in to comment.