// Package swagger ... // // Description : swagger ... // // Author : go_developer@163.com<白茶清欢> // // Date : 2024-04-19 18:16 package swagger import ( "git.zhangdeman.cn/gateway/api-doc/define" "git.zhangdeman.cn/gateway/api-doc/util" "git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/wrapper" "net/http" "strings" ) // Generate 生成文档 // // Author : zhangdeman001@ke.com<张德满> // // Date : 18:17 2024/4/19 func Generate(docConfig *define.SwaggerInput) (*define.Swagger, error) { formatDocConfig(docConfig) swaggerInfo := &define.Swagger{ Schemes: docConfig.Schemes, Swagger: define.SwaggerVersion2, Host: docConfig.Host, BasePath: docConfig.BasePath, Info: docConfig.Info, Paths: map[string]map[string]define.SwaggerPathConfig{}, Definitions: map[string]define.SwaggerDefinition{}, } return swaggerInfo, nil } // formatDocConfig 格式化填充dock配置 // // Author : go_developer@163.com<白茶清欢> // // Date : 10:54 2024/4/22 func formatDocConfig(docConfig *define.SwaggerInput) { if docConfig.Schemes == nil { docConfig.Schemes = make([]string, 0) } for _, itemPath := range docConfig.PathConfigList { // 默认请求类型 application/json itemPath.ContentType = strings.TrimSpace(itemPath.ContentType) itemPath.ContentType = wrapper.TernaryOperator.String(len(itemPath.ContentType) == 0, consts.MimeTypeJson, wrapper.String(itemPath.ContentType)).Value() // 默认post请求 itemPath.Method = strings.TrimSpace(itemPath.Method) itemPath.Method = wrapper.TernaryOperator.String(len(itemPath.ContentType) == 0, http.MethodPost, wrapper.String(strings.ToUpper(itemPath.Method))).Value() // 默认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.TagList = wrapper.TernaryOperator.Array(len(itemPath.TagList) == 0, wrapper.ArrayType([]string{"未分组"}), wrapper.ArrayType(itemPath.TagList)).ToStringSlice() for _, itemParam := range itemPath.ParameterList { // 填充默认参数位置 itemParam.In = strings.TrimSpace(itemParam.In) itemPath.Summary = wrapper.TernaryOperator.String(len(itemParam.In) == 0, wrapper.String(util.GetParameterDefaultLocation(itemPath.Method)), wrapper.String(itemParam.In)).Value() // 参数类型没填, 按照字符串处理 itemParam.Type = strings.TrimSpace(itemParam.Type) itemParam.Type = wrapper.TernaryOperator.String(len(itemParam.Type) == 0, wrapper.String(itemParam.Type), wrapper.String(itemParam.Type)).Value() // 参数描述 itemParam.Description = strings.TrimSpace(itemParam.Description) 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 { // 默认返回数据类型 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() } } }