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

JSON Tag跳过保留字段 #84

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
58 changes: 58 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
BIN := goctl-swagger
PKG = $(shell cat go.mod | grep "^module " | sed -e "s/module //g")
VERSION = v$(shell cat version)
COMMIT_SHA ?= $(shell git describe --always)

GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
GOBUILD=GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags "-X ${PKG}/version.Version=${VERSION}+sha.${COMMIT_SHA}"
GOINSTALL=GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 go install -ldflags "-X ${PKG}/version.Version=${VERSION}+sha.${COMMIT_SHA}"
GOBIN ?= $(shell go env GOPATH)/bin

MAIN_ROOT ?= .

.PHONY:echo
echo:
@echo "PKG:${PKG}"
@echo "VERSION:${VERSION}"
@echo "COMMIT_SHA:${COMMIT_SHA}"
@echo "GOOS:${GOOS}"
@echo "GOARCH:${GOARCH}"
@echo "GOBUILD:${GOBUILD}"
@echo "GOINSTALL:${GOINSTALL}"
@echo "GOBIN:${GOBIN}"

.PHONY:all
all: clean build

.PHONY:install
install: download
cd $(MAIN_ROOT) && $(GOINSTALL)

.PHONY:build
build:
cd $(MAIN_ROOT) && $(GOBUILD) -o $(BIN)

.PHONY: test
test: build
go test -v ./...

.PHONY: show-version
show-version:
@echo $(VERSION) # silent echo

.PHONY:download
download:
go mod download -x

.PHONY:clean
clean:
rm -rf ./$(BIN)

.PHONY:upgrade
upgrade:
go get -u ./...

.PHONY:tidy
tidy:
go mod tidy
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
GOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/goctl-swagger@latest
```

* 本地调试编译
```shell
make build
```
如果您不想覆盖`$GOPATH/bin`下面的`goctl-swagger`,请为`build`生成的`goctl-swagger`重命名
```shell
mv goctl-swagger swagtest
```

### 2. 配置环境

将$GOPATH/bin中的goctl-swagger添加到环境变量
Expand Down
5 changes: 3 additions & 2 deletions example/user.api
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ type (

//注册请求结构
RegisterReq {
Username string `json:"username"`
Password string `json:"password"`
Username string `json:"username" description:"用户名"`
Password string `json:"password" default:"admin123"`
Mobile string `json:"mobile"`
}

LoginReq {
Username string `json:"username"` //测试
Password string `json:"password"`//测试2
Gender int `json:"gender,optional" description:"性别 0女 1男" validate:"is one of 0 1"`
}
UserInfoReq {
Id string `path:"id"`
Expand Down
8 changes: 7 additions & 1 deletion example/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@
"password": {
"type": "string",
"description": "测试2"
},
"gender": {
"type": "integer",
"format": "int32",
"description": "性别 0女 1男"
}
},
"title": "LoginReq",
Expand All @@ -170,7 +175,8 @@
"type": "object",
"properties": {
"username": {
"type": "string"
"type": "string",
"description": "用户名"
},
"password": {
"type": "string"
Expand Down
17 changes: 16 additions & 1 deletion generate/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ const (
atRespDoc = "@respdoc-"
)

const ( // JSON保留tag => 新增请append到SkipTags里面
validateTag = "validate"
validatorTag = "validator"
descriptionTag = "description"
defaultTag = "default"
)

var skipTags = []string{validateTag, validatorTag, descriptionTag, defaultTag} // 跳过这些保留tag的Key,以后有新的需要跳过字段往这里面加

func parseRangeOption(option string) (float64, float64, bool) {
const str = "\\[([+-]?\\d+(\\.\\d+)?):([+-]?\\d+(\\.\\d+)?)\\]"
result := regexp.MustCompile(str).FindStringSubmatch(option)
Expand Down Expand Up @@ -391,6 +400,9 @@ func renderStruct(member spec.Member) swaggerParameterObject {

for _, tag := range member.Tags() {
sp.Name = tag.Name
if tag.Key == descriptionTag { // 给自定义结构体的描述赋值,如果有description tag,则用,没有的话使用//后面的作为表示
sp.Description = tag.Name
}
if len(tag.Options) == 0 {
sp.Required = true
continue
Expand Down Expand Up @@ -492,7 +504,7 @@ func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec.

for _, tag := range member.Tags() {
if len(tag.Options) == 0 {
if !contains(schema.Required, tag.Name) && tag.Name != "required" {
if !contains(schema.Required, tag.Name) && (tag.Name != "required" && !contains(skipTags, tag.Key)) {
schema.Required = append(schema.Required, tag.Name)
}
continue
Expand Down Expand Up @@ -623,6 +635,9 @@ func schemaOfField(member spec.Member) swaggerSchemaObject {
ret.Description = comment

for _, tag := range member.Tags() {
if tag.Key == descriptionTag {
ret.Description = tag.Name // 给自定义结构体的描述赋值,如果有description tag,则用,没有的话使用//后面的作为表示
}
if len(tag.Options) == 0 {
continue
}
Expand Down
Binary file modified swagtest
Binary file not shown.
1 change: 1 addition & 0 deletions version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0