diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..577754c --- /dev/null +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index c316445..241e498 100644 --- a/README.md +++ b/README.md @@ -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添加到环境变量 diff --git a/example/user.api b/example/user.api index 4a1bf59..13ebf0d 100644 --- a/example/user.api +++ b/example/user.api @@ -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"` diff --git a/example/user.json b/example/user.json index 5ab1adf..cb90b87 100644 --- a/example/user.json +++ b/example/user.json @@ -158,6 +158,11 @@ "password": { "type": "string", "description": "测试2" + }, + "gender": { + "type": "integer", + "format": "int32", + "description": "性别 0女 1男" } }, "title": "LoginReq", @@ -170,7 +175,8 @@ "type": "object", "properties": { "username": { - "type": "string" + "type": "string", + "description": "用户名" }, "password": { "type": "string" diff --git a/generate/parser.go b/generate/parser.go index b4af259..affb803 100644 --- a/generate/parser.go +++ b/generate/parser.go @@ -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) @@ -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 @@ -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 @@ -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 } diff --git a/swagtest b/swagtest index a83422e..2dc0763 100755 Binary files a/swagtest and b/swagtest differ diff --git a/version b/version new file mode 100644 index 0000000..afaf360 --- /dev/null +++ b/version @@ -0,0 +1 @@ +1.0.0 \ No newline at end of file