From ff2fa198eea6392225a54668f41b35096db993ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 24 Dec 2024 12:11:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BB=93=E6=9E=84=E4=BD=93?= =?UTF-8?q?=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++ define/generate.go | 18 +++++++++-------- define/swagger.go | 4 ++++ swagger.go | 42 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 101 insertions(+), 11 deletions(-) diff --git a/common.go b/common.go index edcde63..4580533 100644 --- a/common.go +++ b/common.go @@ -33,7 +33,55 @@ func GetUriPathParamList(uriPath string) []*define.ParamConfig { Type: consts.DataTypeString.String(), Title: param, Description: param, + IsRequired: true, }) } return result } + +// GetParamType 将文档配置的数据类型转换为归一化处理后的数据类型 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:52 2024/12/24 +func GetParamType(docParamType string, formatType string) consts.DataType { + docParamType = strings.ToLower(docParamType) + formatType = strings.ToLower(formatType) + if len(formatType) == 0 { + formatType = docParamType + } + switch docParamType { + case "integer": + if formatType == "int64" { + return consts.DataTypeInt + } + return consts.DataTypeUint + case "string": + return consts.DataTypeString + default: + return consts.DataTypeAny + } +} + +// GetParamLocation 获取参数位置 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:02 2024/12/24 +func GetParamLocation(docParamLocation string) consts.RequestDataLocation { + docParamLocation = strings.ToLower(docParamLocation) + switch docParamLocation { + case "query": + return consts.RequestDataLocationQuery + case "header": + return consts.RequestDataLocationHeader + case "cookie": + return consts.RequestDataLocationCookie + case "body": + return consts.RequestDataLocationBody + case "path": + return consts.RequestDataLocationUriPath + default: + return consts.RequestDataLocationQuery + } +} diff --git a/define/generate.go b/define/generate.go index ef576b1..08584ce 100644 --- a/define/generate.go +++ b/define/generate.go @@ -27,14 +27,15 @@ type DocParseResult struct { // // Date : 15:21 2024/8/14 type UriBaseConfig struct { - Uri string `json:"uri"` // 接口路由 - Method string `json:"method"` // 接口请求方法 - ContentType string `json:"content_type"` // 接口请求类型 - TagList []string `json:"tag_list"` // 接口标签列表 - Summary string `json:"summary"` // 接口摘要描述 - Description string `json:"description"` // 接口详细描述 - ParamList []*ParamConfig `json:"param_list"` // 参数列表 - ResultList []*ResultConfig `json:"result_list"` // 返回值列表 + Uri string `json:"uri"` // 接口路由 + Method string `json:"method"` // 接口请求方法 + ContentType string `json:"content_type"` // 接口请求类型 + OutputContentType string `json:"output_content_type"` // 输出数据类型 + TagList []string `json:"tag_list"` // 接口标签列表 + Summary string `json:"summary"` // 接口摘要描述 + Description string `json:"description"` // 接口详细描述 + ParamList []*ParamConfig `json:"param_list"` // 参数列表 + ResultList []*ResultConfig `json:"result_list"` // 返回值列表 } // ParamConfig 参数配置 @@ -48,6 +49,7 @@ type ParamConfig struct { Type string `json:"type"` // 参数类型 Title string `json:"title"` // 参数标题 Description string `json:"description"` // 参数描述 + IsRequired bool `json:"is_required"` // 是否必传 } // ResultConfig 返回值配置 diff --git a/define/swagger.go b/define/swagger.go index 1ba01ea..f6ee591 100644 --- a/define/swagger.go +++ b/define/swagger.go @@ -15,6 +15,8 @@ package define type SwaggerPathConfig struct { Description string `json:"description"` // 接口描述 Consumes []string `json:"consumes"` // 请求方式, application/json等 + Produces []string `json:"produces"` // 请求方式, application/json等 + OperationID string `json:"operationId"` // 操作ID Tags []string `json:"tags"` // 接口标签 Summary string `json:"summary"` // 接口摘要 Parameters []*SwaggerPathConfigParameter `json:"parameters"` // 参数列表 @@ -76,6 +78,8 @@ type SwaggerDefinitionProperty struct { Type string `json:"type"` // 类型 Items map[string]string `json:"items,omitempty"` // 引用类型中的引用(数组) AllOf []map[string]string `json:"allOf,omitempty"` // 引用类型中的引用(对象) + XGoName string `json:"x-go-name"` // go字段名称 + XGoPackage string `json:"x-go-package"` // go 包路径 } // Swagger 文档整体结构定义 diff --git a/swagger.go b/swagger.go index c5ecfaf..11602ff 100644 --- a/swagger.go +++ b/swagger.go @@ -9,9 +9,11 @@ package api_doc import ( "errors" - "fmt" "git.zhangdeman.cn/gateway/api-doc/define" + "git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/serialize" + "net/http" + "strings" ) // ParseSwagger2 解析swagger2.0版本文档 @@ -44,8 +46,42 @@ func ParseSwagger2(docContent []byte) (*define.DocParseResult, error) { func buildUriList(swaggerDoc *define.Swagger) ([]*define.UriBaseConfig, error) { uriList := make([]*define.UriBaseConfig, 0) for itemUri, itemUriMethodConfig := range swaggerDoc.Paths { - GetUriPathParamList(itemUri) - fmt.Println(itemUriMethodConfig) + for requestMethod, methodConfig := range itemUriMethodConfig { + if len(methodConfig.Consumes) == 0 { + // 未配置请求方法, 写请求 json , 读请求 form + if strings.ToUpper(requestMethod) == http.MethodPost || + strings.ToUpper(requestMethod) == http.MethodPut || + strings.ToUpper(requestMethod) == http.MethodPatch || + strings.ToUpper(requestMethod) == http.MethodDelete { + methodConfig.Consumes = []string{ + consts.MimeTypeJson, + } + } else { + methodConfig.Consumes = []string{ + consts.MimeTypeXWWWFormUrlencoded, + } + } + } + if len(methodConfig.Produces) == 0 { + // 未配置输出数据类型, 默认 json + methodConfig.Produces = []string{ + consts.MimeTypeJson, + } + } + uriResult := &define.UriBaseConfig{ + Uri: swaggerDoc.BasePath + "/" + strings.TrimLeft(itemUri, "/"), + Method: strings.ToUpper(requestMethod), + ContentType: methodConfig.Consumes[0], + OutputContentType: methodConfig.Produces[0], + TagList: methodConfig.Tags, + Summary: methodConfig.Summary, + Description: methodConfig.Description, + ParamList: make([]*define.ParamConfig, 0), + ResultList: nil, + } + // 解析query/body/header参数 + uriList = append(uriList, uriResult) + } } return uriList, nil }