提交一版返回值处理, 待调试
This commit is contained in:
parent
9569f7dfd0
commit
a55cb66fd0
@ -52,7 +52,7 @@ type SwaggerPathConfig struct {
|
|||||||
Tags []string `json:"tags"` // 接口标签
|
Tags []string `json:"tags"` // 接口标签
|
||||||
Summary string `json:"summary"` // 接口摘要
|
Summary string `json:"summary"` // 接口摘要
|
||||||
Parameters []*SwaggerPathConfigParameter `json:"parameters"` // 参数列表
|
Parameters []*SwaggerPathConfigParameter `json:"parameters"` // 参数列表
|
||||||
Response map[string]*SwaggerPathConfigResponse `json:"response"` // code码 => 响应描述
|
Responses map[string]*SwaggerPathConfigResponse `json:"responses"` // code码 => 响应描述
|
||||||
}
|
}
|
||||||
|
|
||||||
// SwaggerPathConfigParameter ...
|
// SwaggerPathConfigParameter ...
|
||||||
@ -146,6 +146,12 @@ type SwaggerPathInput struct {
|
|||||||
ResponseList []*SwaggerResponseInput `json:"response_list"` // 响应数据
|
ResponseList []*SwaggerResponseInput `json:"response_list"` // 响应数据
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SwaggerResponseInput struct {
|
||||||
|
Code string `json:"code"` // 状态码
|
||||||
|
Description string `json:"description"` // 描述
|
||||||
|
List []*SwaggerResponseItemInput `json:"list"` // 数据列表
|
||||||
|
}
|
||||||
|
|
||||||
// SwaggerParameterInput ...
|
// SwaggerParameterInput ...
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
@ -160,12 +166,12 @@ type SwaggerParameterInput struct {
|
|||||||
EnumList []interface{} `json:"enum_list"` // 枚举值列表
|
EnumList []interface{} `json:"enum_list"` // 枚举值列表
|
||||||
}
|
}
|
||||||
|
|
||||||
// SwaggerResponseInput 响应数据
|
// SwaggerResponseItemInput 响应数据
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 10:49 2024/4/22
|
// Date : 10:49 2024/4/22
|
||||||
type SwaggerResponseInput struct {
|
type SwaggerResponseItemInput struct {
|
||||||
Type string `json:"type"` // 类型
|
Type string `json:"type"` // 类型
|
||||||
Description string `json:"description"` // 描述
|
Description string `json:"description"` // 描述
|
||||||
Field string `json:"field"` // 字段名
|
Field string `json:"field"` // 字段名
|
||||||
|
@ -55,7 +55,7 @@ func formatDocConfig(docConfig *define.SwaggerInput) {
|
|||||||
itemPath.ContentType = wrapper.TernaryOperator.String(len(itemPath.ContentType) == 0, consts.MimeTypeJson, wrapper.String(itemPath.ContentType)).Value()
|
itemPath.ContentType = wrapper.TernaryOperator.String(len(itemPath.ContentType) == 0, consts.MimeTypeJson, wrapper.String(itemPath.ContentType)).Value()
|
||||||
// 默认post请求
|
// 默认post请求
|
||||||
itemPath.Method = strings.TrimSpace(itemPath.Method)
|
itemPath.Method = strings.TrimSpace(itemPath.Method)
|
||||||
itemPath.Method = wrapper.TernaryOperator.String(len(itemPath.ContentType) == 0, http.MethodPost, wrapper.String(strings.ToUpper(itemPath.Method))).Value()
|
itemPath.Method = wrapper.TernaryOperator.String(len(itemPath.ContentType) == 0, wrapper.String(strings.ToLower(http.MethodPost)), wrapper.String(strings.ToLower(itemPath.Method))).Value()
|
||||||
// 默认summary
|
// 默认summary
|
||||||
itemPath.Summary = strings.TrimSpace(itemPath.Summary)
|
itemPath.Summary = strings.TrimSpace(itemPath.Summary)
|
||||||
itemPath.Summary = wrapper.TernaryOperator.String(len(itemPath.Summary) == 0, wrapper.String("接口 : "+itemPath.Uri), wrapper.String(itemPath.Summary)).Value()
|
itemPath.Summary = wrapper.TernaryOperator.String(len(itemPath.Summary) == 0, wrapper.String("接口 : "+itemPath.Uri), wrapper.String(itemPath.Summary)).Value()
|
||||||
@ -75,13 +75,15 @@ func formatDocConfig(docConfig *define.SwaggerInput) {
|
|||||||
itemParam.Description = wrapper.TernaryOperator.String(len(itemParam.Description) == 0, wrapper.String(itemParam.Type+" : "+itemParam.Name), wrapper.String(itemParam.Description)).Value()
|
itemParam.Description = wrapper.TernaryOperator.String(len(itemParam.Description) == 0, wrapper.String(itemParam.Type+" : "+itemParam.Name), wrapper.String(itemParam.Description)).Value()
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, itemResponse := range itemPath.ResponseList {
|
for _, itemResponseConfig := range itemPath.ResponseList {
|
||||||
// 默认返回数据类型
|
for _, itemResponse := range itemResponseConfig.List {
|
||||||
itemResponse.Type = strings.TrimSpace(itemResponse.Type)
|
// 默认返回数据类型
|
||||||
itemResponse.Type = wrapper.TernaryOperator.String(len(itemResponse.Type) == 0, consts.DataTypeString, wrapper.String(itemResponse.Type)).Value()
|
itemResponse.Type = strings.TrimSpace(itemResponse.Type)
|
||||||
// 填充默认描述
|
itemResponse.Type = wrapper.TernaryOperator.String(len(itemResponse.Type) == 0, consts.DataTypeString, wrapper.String(itemResponse.Type)).Value()
|
||||||
itemResponse.Description = strings.TrimSpace(itemResponse.Description)
|
// 填充默认描述
|
||||||
itemResponse.Description = wrapper.TernaryOperator.String(len(itemResponse.Description) == 0, wrapper.String(itemResponse.Type+" : "+itemResponse.Field), wrapper.String(itemResponse.Description)).Value()
|
itemResponse.Description = strings.TrimSpace(itemResponse.Description)
|
||||||
|
itemResponse.Description = wrapper.TernaryOperator.String(len(itemResponse.Description) == 0, wrapper.String(itemResponse.Type+" : "+itemResponse.Field), wrapper.String(itemResponse.Description)).Value()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,7 +106,7 @@ func generatePathConfig(swaggerInfo *define.Swagger, docConfig *define.SwaggerIn
|
|||||||
Tags: itemPath.TagList,
|
Tags: itemPath.TagList,
|
||||||
Summary: itemPath.Summary,
|
Summary: itemPath.Summary,
|
||||||
Parameters: make([]*define.SwaggerPathConfigParameter, 0),
|
Parameters: make([]*define.SwaggerPathConfigParameter, 0),
|
||||||
Response: make(map[string]*define.SwaggerPathConfigResponse),
|
Responses: make(map[string]*define.SwaggerPathConfigResponse),
|
||||||
}
|
}
|
||||||
// 生成参数配置
|
// 生成参数配置
|
||||||
generatePathParameterConfig(swaggerInfo, itemPath)
|
generatePathParameterConfig(swaggerInfo, itemPath)
|
||||||
@ -119,6 +121,7 @@ func generatePathConfig(swaggerInfo *define.Swagger, docConfig *define.SwaggerIn
|
|||||||
func generatePathParameterConfig(swaggerInfo *define.Swagger, pathConfig *define.SwaggerPathInput) {
|
func generatePathParameterConfig(swaggerInfo *define.Swagger, pathConfig *define.SwaggerPathInput) {
|
||||||
swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters = make([]*define.SwaggerPathConfigParameter, 0)
|
swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters = make([]*define.SwaggerPathConfigParameter, 0)
|
||||||
hasDealTable := map[string]bool{}
|
hasDealTable := map[string]bool{}
|
||||||
|
// hasDealResponseTable := map[string]bool{}
|
||||||
|
|
||||||
for _, itemParamInput := range pathConfig.ParameterList {
|
for _, itemParamInput := range pathConfig.ParameterList {
|
||||||
if len(itemParamInput.Name) == 0 {
|
if len(itemParamInput.Name) == 0 {
|
||||||
@ -142,6 +145,29 @@ func generatePathParameterConfig(swaggerInfo *define.Swagger, pathConfig *define
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*for _, itemResponseConfig := range pathConfig.ResponseList {
|
||||||
|
for _, itemResponseInput := range itemResponseConfig.List {
|
||||||
|
if len(itemResponseInput.Field) == 0 {
|
||||||
|
// 未指定参数名, 忽略
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// name 可能是 x.x.x 递归数组, 或者 x.x.[].x
|
||||||
|
outputDefine := pathConfig.Uri + "." + itemResponseConfig.Code + ".output"
|
||||||
|
generateResponseDefinitions(swaggerInfo, pathConfig.Uri, outputDefine, itemResponseInput.Field, itemResponseInput)
|
||||||
|
namePath := strings.Split(itemResponseInput.Field, ".")
|
||||||
|
if _, exist := hasDealResponseTable[namePath[0]]; !exist {
|
||||||
|
hasDealResponseTable[namePath[0]] = true
|
||||||
|
swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Responses[itemResponseConfig.Code] = &define.SwaggerPathConfigResponse{
|
||||||
|
Description: itemResponseInput.Description,
|
||||||
|
Schema: map[string]string{
|
||||||
|
"$ref": "#/definitions" + outputDefine,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateParameterDefinitions 生成请求参数的描述
|
// generateParameterDefinitions 生成请求参数的描述
|
||||||
@ -192,6 +218,51 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
|
|||||||
generateParameterDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), paramConfig)
|
generateParameterDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), paramConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generateResponseDefinitions ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 18:28 2024/4/22
|
||||||
|
func generateResponseDefinitions(swaggerInfo *define.Swagger, uri string, parentPath string, subPath string, responseConfig *define.SwaggerResponseItemInput) {
|
||||||
|
if nil == swaggerInfo.Definitions {
|
||||||
|
swaggerInfo.Definitions = map[string]*define.SwaggerDefinition{}
|
||||||
|
}
|
||||||
|
subPathArr := strings.Split(subPath, ".")
|
||||||
|
if _, exist := swaggerInfo.Definitions[parentPath]; !exist && len(parentPath) > 0 {
|
||||||
|
swaggerInfo.Definitions[parentPath] = &define.SwaggerDefinition{
|
||||||
|
Type: "object",
|
||||||
|
Required: make([]string, 0),
|
||||||
|
Properties: make(map[string]*define.SwaggerDefinitionProperty),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(subPathArr) == 1 {
|
||||||
|
if len(parentPath) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
swaggerInfo.Definitions[parentPath].Type = responseConfig.Type
|
||||||
|
/*swaggerInfo.Definitions[parentPath].Properties[paramConfig.Name] = &define.SwaggerDefinitionProperty{
|
||||||
|
Description: paramConfig.Description,
|
||||||
|
Type: paramConfig.Type,
|
||||||
|
}*/
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(parentPath) == 0 {
|
||||||
|
parentPath = uri + ".output"
|
||||||
|
}
|
||||||
|
if len(subPathArr) == 2 {
|
||||||
|
if subPathArr[1] == "[]" {
|
||||||
|
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{
|
||||||
|
Description: responseConfig.Description,
|
||||||
|
Type: "Array",
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
generateResponseDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), responseConfig)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
generateResponseDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), responseConfig)
|
||||||
|
}
|
||||||
|
|
||||||
func getUri(uri string) string {
|
func getUri(uri string) string {
|
||||||
return "/" + strings.TrimLeft(uri, "/")
|
return "/" + strings.TrimLeft(uri, "/")
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,20 @@ func TestGenerate(t *testing.T) {
|
|||||||
EnumList: []interface{}{"zhang", "de", "man"},
|
EnumList: []interface{}{"zhang", "de", "man"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
ResponseList: nil,
|
ResponseList: []*define.SwaggerResponseInput{
|
||||||
|
&define.SwaggerResponseInput{
|
||||||
|
Code: "200",
|
||||||
|
Description: "成功",
|
||||||
|
List: []*define.SwaggerResponseItemInput{
|
||||||
|
&define.SwaggerResponseItemInput{
|
||||||
|
Type: consts.DataTypeString,
|
||||||
|
Description: "姓名",
|
||||||
|
Field: "nick_name",
|
||||||
|
IsRequired: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user