This commit is contained in:
白茶清欢 2024-12-23 17:56:01 +08:00
parent 15beb6bae0
commit fb9e22ac66

View File

@ -43,11 +43,13 @@ func Generate(docConfig *define.SwaggerInput) (*define.Swagger, error) {
// Date : 10:54 2024/4/22 // Date : 10:54 2024/4/22
func formatDocConfig(docConfig *define.SwaggerInput) { func formatDocConfig(docConfig *define.SwaggerInput) {
if len(docConfig.Schemes) == 0 { if len(docConfig.Schemes) == 0 {
docConfig.Schemes = []string{consts.SchemeHTTP} docConfig.Schemes = []string{
consts.SchemeHTTP.String(),
}
} }
docConfig.Host = wrapper.String(docConfig.Host).ReplaceChar(map[string]string{ docConfig.Host = wrapper.String(docConfig.Host).ReplaceChar(map[string]string{
consts.SchemeHTTP + "://": "", consts.SchemeHTTP.String() + "://": "",
consts.SchemeHTTPS + "://": "", consts.SchemeHTTPS.String() + "://": "",
}).Value() }).Value()
for _, itemPath := range docConfig.PathConfigList { for _, itemPath := range docConfig.PathConfigList {
// 默认请求类型 application/json // 默认请求类型 application/json
@ -79,7 +81,7 @@ func formatDocConfig(docConfig *define.SwaggerInput) {
for _, itemResponse := range itemResponseConfig.List { for _, itemResponse := range itemResponseConfig.List {
// 默认返回数据类型 // 默认返回数据类型
itemResponse.Type = strings.TrimSpace(itemResponse.Type) itemResponse.Type = strings.TrimSpace(itemResponse.Type)
itemResponse.Type = wrapper.TernaryOperator.String(len(itemResponse.Type) == 0, consts.DataTypeString, wrapper.String(itemResponse.Type)).Value() itemResponse.Type = wrapper.TernaryOperator.String(len(itemResponse.Type) == 0, wrapper.String(consts.DataTypeString), wrapper.String(itemResponse.Type)).Value()
// 填充默认描述 // 填充默认描述
itemResponse.Description = strings.TrimSpace(itemResponse.Description) 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 = wrapper.TernaryOperator.String(len(itemResponse.Description) == 0, wrapper.String(itemResponse.Type+" : "+itemResponse.Field), wrapper.String(itemResponse.Description)).Value()
@ -133,7 +135,7 @@ func generatePathParameterConfig(swaggerInfo *define.Swagger, pathConfig *define
namePath := strings.Split(itemParamInput.Name, ".") namePath := strings.Split(itemParamInput.Name, ".")
realParamName := namePath[0] realParamName := namePath[0]
parentPath := "" parentPath := ""
if strings.ToUpper(itemParamInput.In) == consts.RequestDataLocationBody && !strings.Contains(realParamName, ".") { if strings.ToUpper(itemParamInput.In) == consts.RequestDataLocationBody.String() && !strings.Contains(realParamName, ".") {
realParamName = "jsonBody" realParamName = "jsonBody"
parentPath = strings.ReplaceAll(pathConfig.Uri, ".", "-") + ".jsonBody" parentPath = strings.ReplaceAll(pathConfig.Uri, ".", "-") + ".jsonBody"
} }
@ -177,7 +179,7 @@ func generatePathResponseConfig(swaggerInfo *define.Swagger, pathConfig *define.
Type: itemResponseInput.Type, Type: itemResponseInput.Type,
Description: itemResponseInput.Description, Description: itemResponseInput.Description,
Name: itemResponseInput.Field, Name: itemResponseInput.Field,
In: consts.RequestDataLocationBody, In: consts.RequestDataLocationBody.String(),
Required: false, Required: false,
EnumList: nil, EnumList: nil,
}) })
@ -186,8 +188,8 @@ func generatePathResponseConfig(swaggerInfo *define.Swagger, pathConfig *define.
hasDealResponseTable[namePath[0]] = true hasDealResponseTable[namePath[0]] = true
swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Responses[itemResponseConfig.Code] = &define.SwaggerPathConfigResponse{ swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Responses[itemResponseConfig.Code] = &define.SwaggerPathConfigResponse{
Description: "返回数据", Description: "返回数据",
Schema: map[string]string{ Schema: &define.SwaggerPathConfigResponseSchema{
"$ref": getRefValue(outputDefine), Ref: getRefValue(outputDefine),
}, },
} }
} }
@ -298,7 +300,7 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
// //
// Date : 16:50 2024/4/25 // Date : 16:50 2024/4/25
func handleOneLevelSubPath(swaggerInfo *define.Swagger, uri string, parentPath string, subPath string, paramConfig *define.SwaggerParameterInput) { func handleOneLevelSubPath(swaggerInfo *define.Swagger, uri string, parentPath string, subPath string, paramConfig *define.SwaggerParameterInput) {
if paramConfig.In != strings.ToLower(consts.RequestDataLocationBody) && !isGenerateOutput(parentPath) { if paramConfig.In != strings.ToLower(consts.RequestDataLocationBody.String()) && !isGenerateOutput(parentPath) {
// 长度为1, 还不在 body, 无需生成结构体 // 长度为1, 还不在 body, 无需生成结构体
return return
} }
@ -368,13 +370,13 @@ func setGlobalMapDefinition(swaggerInfo *define.Swagger, dataType string) {
// Date : 15:46 2024/4/25 // Date : 15:46 2024/4/25
func isGlobalMapType(dataType string) bool { func isGlobalMapType(dataType string) bool {
return wrapper.ArrayType([]string{ return wrapper.ArrayType([]string{
consts.DataTypeMapAnyAny, consts.DataTypeMapAnyAny.String(),
consts.DataTypeMapStrUint, consts.DataTypeMapStrUint.String(),
consts.DataTypeMapStrInt, consts.DataTypeMapStrInt.String(),
consts.DataTypeMapStrSlice, consts.DataTypeMapStrSlice.String(),
consts.DataTypeMapStrFloat, consts.DataTypeMapStrFloat.String(),
consts.DataTypeMapStrBool, consts.DataTypeMapStrBool.String(),
consts.DataTypeMapStrAny, consts.DataTypeMapStrAny.String(),
}).Has(dataType) >= 0 }).Has(dataType) >= 0
} }
@ -395,7 +397,7 @@ func initAnyDefinition(swaggerInfo *define.Swagger, definitionName string) {
} }
swaggerInfo.Definitions[definitionName] = &define.SwaggerDefinition{ swaggerInfo.Definitions[definitionName] = &define.SwaggerDefinition{
Type: consts.SwaggerDataTypeObject, Type: consts.SwaggerDataTypeObject,
Format: consts.DataTypeMapStrAny, Format: consts.DataTypeMapStrAny.String(),
Required: make([]string, 0), Required: make([]string, 0),
Properties: make(map[string]*define.SwaggerDefinitionProperty), Properties: make(map[string]*define.SwaggerDefinitionProperty),
} }