Skip to content

Commit

Permalink
Merge pull request #33 from ybkuroki/develop
Browse files Browse the repository at this point in the history
Add reviewdog, golangci-lint
  • Loading branch information
ybkuroki authored Dec 20, 2020
2 parents a040d0f + b843824 commit c2e59d4
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 18 deletions.
58 changes: 58 additions & 0 deletions .github/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# ref: https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml
# all available settings of specific linters
linters-settings:
dupl:
# tokens count to trigger issue, 150 by default
threshold: 150
goconst:
# minimal length of string constant, 3 by default
min-len: 3
# minimal occurrences count to trigger, 3 by default
min-occurrences: 2
gocyclo:
# minimal code complexity to report, 30 by default (but we recommend 10-20)
min-complexity: 15
gofmt:
# simplify code: gofmt with `-s` option, true by default
simplify: true
golint:
# minimal confidence for issues, default is 0.8
min-confidence: 0.8
govet:
# report about shadowed variables
check-shadowing: true
misspell:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
locale: US

linters:
disable-all: true
enable:
# default
# ref: https://golangci-lint.run/usage/linters/
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- varcheck
# add
- dupl
- goconst
- golint
- govet
- gofmt
- goimports
- misspell
- unconvert
- unparam

# https://github.com/golangci/golangci/wiki/Configuration
# latest version: https://github.com/golangci/golangci-lint
service:
golangci-lint-version: 1.33.0
18 changes: 12 additions & 6 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: check

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

Expand All @@ -11,23 +9,31 @@ jobs:
name: check
runs-on: ubuntu-latest
steps:
# Set up GOPATH
- name: set up
uses: actions/setup-go@v2
with:
go-version: 1.14
id: go
# Check out this repository
- name: checkout
uses: actions/checkout@v2
# Store cache
- name: cache
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: install lint
run: curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.32.2
- name: lint
run: golangci-lint run
# Run golangci-lint using reviewdog
- name: golangci-lint
uses: reviewdog/action-golangci-lint@v1
with:
github_token: ${{ secrets.github_token }}
level: warning
golangci_lint_flags: "--config=.github/.golangci.yml"
reporter: github-pr-review
# Run test
- name: test
run: go test ./... -v
4 changes: 2 additions & 2 deletions controller/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func JSONErrorHandler(err error, c echo.Context) {
apierr.Message = msg

if !c.Response().Committed {
if err := c.JSON(code, apierr); err != nil {
logger.GetZapLogger().Errorf(err.Error())
if reserr := c.JSON(code, apierr); reserr != nil {
logger.GetZapLogger().Errorf(reserr.Error())
}
}
logger.GetZapLogger().Debugf(err.Error())
Expand Down
5 changes: 3 additions & 2 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ func InitLogger() {
os.Exit(2)
}
var myConfig *Config
if err := yaml.Unmarshal(configYaml, &myConfig); err != nil {
if err = yaml.Unmarshal(configYaml, &myConfig); err != nil {
fmt.Printf("Failed to read zap logger configuration: %s", err)
os.Exit(2)
}
zap, err := build(myConfig)
var zap *zap.Logger
zap, err = build(myConfig)
if err != nil {
fmt.Printf("Failed to compose zap logger : %s", err)
os.Exit(2)
Expand Down
6 changes: 3 additions & 3 deletions model/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (b *Book) FindAllByPage(rep *repository.Repository, page int, size int) (*P
books = append(books, *book)
}

p := createPage(rep, &books, page, size)
p := createPage(&books, page, size)
return p, nil
}

Expand All @@ -115,11 +115,11 @@ func (b *Book) FindByTitle(rep *repository.Repository, title string, page int, s
books = append(books, *book)
}

p := createPage(rep, &books, page, size)
p := createPage(&books, page, size)
return p, nil
}

func createPage(rep *repository.Repository, books *[]Book, page int, size int) *Page {
func createPage(books *[]Book, page int, size int) *Page {
p := NewPage()
p.Page = page
p.Size = size
Expand Down
16 changes: 11 additions & 5 deletions model/dto/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import (
"gopkg.in/go-playground/validator.v9"
)

const (
required string = "required"
max string = "max"
min string = "min"
)

// RegBookDto defines a data transfer object for register.
type RegBookDto struct {
Title string `validate:"required,gte=3,lt=50" json:"title"`
Isbn string `validate:"required,gte=10,lt=20" json:"isbn"`
Title string `validate:"required,min=3,max=50" json:"title"`
Isbn string `validate:"required,min=10,max=20" json:"isbn"`
CategoryID uint `json:"categoryId"`
FormatID uint `json:"formatId"`
}
Expand Down Expand Up @@ -41,17 +47,17 @@ func validateDto(b interface{}) map[string]string {
switch errors[i].StructField() {
case "ID":
switch errors[i].Tag() {
case "required":
case required:
result["id"] = "書籍IDが存在しません"
}
case "Title":
switch errors[i].Tag() {
case "required", "min", "max":
case required, min, max:
result["title"] = "書籍タイトルは、3文字以上50文字以下で入力してください"
}
case "Isbn":
switch errors[i].Tag() {
case "required", "min", "max":
case required, min, max:
result["isbn"] = "ISBNは、10文字以上20文字以下で入力してください"
}
}
Expand Down

0 comments on commit c2e59d4

Please sign in to comment.