Skip to content

Commit c5ed391

Browse files
committed
feat: add .golangci.yaml and optimize the project
1 parent 3c3e567 commit c5ed391

File tree

10 files changed

+325
-371
lines changed

10 files changed

+325
-371
lines changed

.golangci.yaml

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
run:
2+
go: '1.16'
3+
concurrency: 4
4+
timeout: 5m
5+
tests: true
6+
skip-dirs:
7+
- example
8+
9+
linters-settings:
10+
errcheck:
11+
# https://github.com/kisielk/errcheck#errcheck
12+
ignore: fmt:.*,os:.*
13+
goconst:
14+
# https://github.com/jgautheron/goconst#usage
15+
ignore-tests: true
16+
min-len: 3
17+
min-occurrences: 4
18+
gocritic:
19+
# https://go-critic.github.io/overview#checks-overview
20+
disabled-checks:
21+
- ifElseChain
22+
gofumpt:
23+
# https://github.com/mvdan/gofumpt#gofumpt
24+
extra-rules: true
25+
gosimple:
26+
# https://staticcheck.io/docs/options#checks
27+
checks: [ "all" ]
28+
revive:
29+
ignore-generated-header: true
30+
severity: warning
31+
# https://github.com/mgechev/revive#available-rules
32+
rules:
33+
- name: context-keys-type
34+
- name: time-equal
35+
- name: time-naming
36+
- name: var-declaration
37+
- name: unexported-return
38+
- name: errorf
39+
- name: context-as-argument
40+
- name: dot-imports
41+
- name: error-return
42+
- name: error-strings
43+
- name: error-naming
44+
- name: exported
45+
- name: if-return
46+
- name: increment-decrement
47+
- name: var-naming
48+
arguments:
49+
- [ "ID", "JSON" ] # allow list
50+
- name: range
51+
- name: receiver-naming
52+
- name: indent-error-flow
53+
- name: argument-limit
54+
- name: file-header
55+
- name: empty-block
56+
- name: superfluous-else
57+
- name: confusing-naming
58+
- name: get-return
59+
- name: modifies-parameter
60+
- name: confusing-results
61+
# - name: deep-exit
62+
- name: unused-parameter
63+
- name: unreachable-code
64+
# - name: add-constant
65+
- name: unnecessary-stmt
66+
- name: struct-tag
67+
- name: modifies-value-receiver
68+
- name: constant-logical-expr
69+
- name: bool-literal-in-expr
70+
- name: redefines-builtin-id
71+
- name: function-result-limit
72+
- name: imports-blacklist
73+
- name: range-val-in-closure
74+
- name: range-val-address
75+
- name: waitgroup-by-value
76+
- name: atomic
77+
- name: empty-lines
78+
- name: call-to-gc
79+
- name: duplicated-imports
80+
# - name: import-shadowing
81+
- name: string-of-int
82+
- name: string-format
83+
- name: early-return
84+
- name: unconditional-recursion
85+
- name: identical-branches
86+
- name: defer
87+
- name: unexported-naming
88+
# - name: nested-structs
89+
- name: useless-break
90+
- name: banned-characters
91+
- name: optimize-operands-order
92+
# - name: use-any
93+
- name: datarace
94+
- name: comment-spacings
95+
- name: redundant-import-alias
96+
- name: import-alias-naming
97+
- name: enforce-map-style
98+
staticcheck:
99+
# https://staticcheck.io/docs/options#checks
100+
checks: [ "all" ]
101+
stylecheck:
102+
# https://staticcheck.io/docs/options#checks
103+
checks: [ "all", "-ST1000", "-ST1003" ]
104+
105+
linters:
106+
disable-all: true
107+
enable:
108+
- asasalint
109+
- asciicheck
110+
- bidichk
111+
- bodyclose
112+
- containedctx
113+
- contextcheck
114+
- decorder
115+
- dogsled
116+
- dupl
117+
- dupword
118+
- durationcheck
119+
- errcheck
120+
- errchkjson
121+
- errname
122+
- errorlint
123+
- execinquery
124+
# - exhaustive
125+
- exportloopref
126+
- forcetypeassert
127+
- ginkgolinter
128+
- gocheckcompilerdirectives
129+
- gochecksumtype
130+
# - goconst
131+
- gocritic
132+
# - gocyclo
133+
# - godot
134+
- godox
135+
- gofmt
136+
- gofumpt
137+
- goheader
138+
- goimports
139+
# - gomnd
140+
- gomoddirectives
141+
- gomodguard
142+
- goprintffuncname
143+
- gosimple
144+
- govet
145+
- grouper
146+
- importas
147+
- inamedparam
148+
- ineffassign
149+
- interfacebloat
150+
- ireturn
151+
- loggercheck
152+
# - maintidx
153+
- makezero
154+
- mirror
155+
- misspell
156+
- musttag
157+
- nakedret
158+
- nilerr
159+
- nilnil
160+
- noctx
161+
- nolintlint
162+
- nosprintfhostport
163+
- perfsprint
164+
- prealloc
165+
- predeclared
166+
- promlinter
167+
- protogetter
168+
- reassign
169+
- revive
170+
- rowserrcheck
171+
- sloglint
172+
- sqlclosecheck
173+
- staticcheck
174+
- stylecheck
175+
- tenv
176+
- testableexamples
177+
- testifylint
178+
- thelper
179+
- tparallel
180+
- typecheck
181+
- unconvert
182+
- unparam
183+
- unused
184+
- usestdlibvars
185+
- wastedassign
186+
- whitespace
187+
# - wrapcheck
188+
- zerologlint
189+
190+
issues:
191+
exclude-use-default: false
192+
max-issues-per-linter: 0
193+
max-same-issues: 0

Makefile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.PHONY: proxy tidy fmt lint pre-commit
2+
3+
proxy:
4+
@go env -w GO111MODULE="on"
5+
@go env -w GOPROXY="https://goproxy.cn,direct"
6+
7+
tidy:
8+
@go mod tidy -e -v
9+
10+
fmt:
11+
@find . -name '*.go' -not -path "./example/*" | xargs gofumpt -w -extra
12+
@find . -name '*.go' -not -path "./example/*" | xargs -n 1 -t goimports-reviser -rm-unused -set-alias -company-prefixes "github.com/sliveryou" -project-name "github.com/sliveryou/goctl-swagger"
13+
@find . -name '*.sh' -not -path "./example/*" | xargs shfmt -w -s -i 2 -ci -bn -sr
14+
15+
lint:
16+
@golangci-lint run ./...
17+
18+
pre-commit: tidy fmt lint

README.md

+18-10
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,36 @@
55

66
1. 修复:当存在第三方 tag 时,生成的参数名称错误的问题
77
2. 修复:当结构体嵌套超过2层时,参数不能正确生成的问题
8-
3. 修复:当同时存在 form 和 json tag时,request body 包含多余字段的问题
8+
3. 修复:当同时存在 form 和 json tag 时,request body 包含多余字段的问题
99
4. 优化:支持从 [validate](https://github.com/go-playground/validator) tag 中获取参数约束
10-
5. 升级 goctl 和 go-zero 到 v1.6.0,修复 [#71](https://github.com/zeromicro/goctl-swagger/issues/71)
11-
6. 当请求方法是 POST/PUT/PATCH 时,如果请求字段不包含 json tag,且包含 form tag时,在请求的 content-type 中添加 "multipart/form-data", "application/x-www-form-urlencoded"
12-
7. 添加 `-pack``-response` 选项,允许在 api 返回结构外再嵌套包装一层
10+
5. 修复:升级 goctl 和 go-zero 到 v1.6.1,修复 [#71](https://github.com/zeromicro/goctl-swagger/issues/71)
11+
6. 添加:当请求方法是 POST/PUT/PATCH 时,如果请求字段不包含 json tag,且包含 form tag 时,在请求的 content-type 中添加 "multipart/form-data", "application/x-www-form-urlencoded"
12+
7. 添加`-pack``-response` 选项,允许在 api 返回结构外再嵌套包装一层
1313
8. 修复:当结构体嵌套超过2层时,不能继承内联结构体属性的问题
1414
9. 优化:支持根据 `@doc()` 里的 `file_*``file_array_*` 键值生成文件类型的请求字段
15-
10. 添加 `delete` 请求方式允许携带请求体 [git commit](https://github.com/sliveryou/goctl-swagger/-/commit/7859e55451a11af8fbfdb91a636b23f965bb5bbe)
15+
10. 添加`delete` 请求方式允许携带请求体
1616

17-
### 2. 编译goctl-swagger插件
17+
### 2. 编译 goctl-swagger 插件
1818

1919
```bash
20-
git clone https://github.com/sliveryou/goctl-swagger.git
21-
cd goctl-swagger
22-
go install
20+
# 可选:自行编译安装:
21+
$ git clone https://github.com/sliveryou/goctl-swagger.git
22+
$ cd goctl-swagger
23+
$ go install
24+
25+
# 推荐:
26+
# 如果 go 版本在 1.16 以前,使用如下命令安装:
27+
$ GO111MODULE=on go get -u github.com/sliveryou/goctl-swagger@latest
28+
29+
# 如果 go 版本在 1.16 及以后,使用如下命令安装:
30+
$ GO111MODULE=on go install github.com/sliveryou/goctl-swagger@latest
2331
```
2432

2533
### 3. goctl-swagger 使用说明
2634

2735
在 api 返回结构外再嵌套包装一层:
2836

29-
```shell
37+
```bash
3038
# -filename 指定生成的 swagger 文件名称
3139
# -pack 开启响应包装并指定响应结构名称
3240
# 未指定 -response 时,默认使用如下响应结构,其中 is_data 字段为指定响应结构的包装字段

action/action.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package action
33
import (
44
cli "github.com/urfave/cli/v2"
55
"github.com/zeromicro/go-zero/tools/goctl/plugin"
6-
"github.com/zeromicro/goctl-swagger/generate"
6+
7+
"github.com/sliveryou/goctl-swagger/generate"
78
)
89

10+
// Generator generates the swagger json doc.
911
func Generator(ctx *cli.Context) error {
1012
fileName := ctx.String("filename")
1113

generate/entities.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ type messageMap map[string]*descriptor.Message
287287

288288
// Internal type mapping from FQEN to descriptor.Enum. Used as a set by the
289289
// findServiceMessages function.
290-
type enumMap map[string]*descriptor.Enum
290+
// type enumMap map[string]*descriptor.Enum
291291

292292
// Internal type to store used references.
293293
type refMap map[string]struct{}

generate/generate.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"io/ioutil"
7+
"os"
88

99
"github.com/zeromicro/go-zero/tools/goctl/plugin"
1010
)
1111

12+
// Do generates the swagger json doc.
1213
func Do(filename, host, basePath, schemes, pack, response string, in *plugin.Plugin) error {
1314
swagger, err := applyGenerate(in, host, basePath, schemes, pack, response)
1415
if err != nil {
@@ -24,7 +25,7 @@ func Do(filename, host, basePath, schemes, pack, response string, in *plugin.Plu
2425

2526
output := in.Dir + "/" + filename
2627

27-
err = ioutil.WriteFile(output, formatted.Bytes(), 0o666)
28+
err = os.WriteFile(output, formatted.Bytes(), 0o666)
2829
if err != nil {
2930
fmt.Println(err)
3031
}

0 commit comments

Comments
 (0)