2024-12-24 11:18:15 +08:00
|
|
|
// Package api_doc ...
|
|
|
|
//
|
|
|
|
// Description : api_doc ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2024-12-24 10:52
|
|
|
|
package api_doc
|
|
|
|
|
|
|
|
import (
|
2024-12-25 11:55:22 +08:00
|
|
|
"fmt"
|
2024-12-24 11:18:15 +08:00
|
|
|
"git.zhangdeman.cn/gateway/api-doc/define"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/consts"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetUriPathParamList 获取uri参数列表
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 10:52 2024/12/24
|
|
|
|
func GetUriPathParamList(uriPath string) []*define.ParamConfig {
|
|
|
|
var (
|
|
|
|
paramList []string
|
|
|
|
result = make([]*define.ParamConfig, 0)
|
|
|
|
)
|
|
|
|
if paramList = define.UriParamRegexp.FindAllString(uriPath, -1); len(paramList) == 0 {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
for _, param := range paramList {
|
|
|
|
result = append(result, &define.ParamConfig{
|
|
|
|
Location: consts.RequestDataLocationUriPath.String(),
|
|
|
|
Path: strings.TrimRight(strings.TrimLeft(param, "{"), "}"),
|
|
|
|
Type: consts.DataTypeString.String(),
|
|
|
|
Title: param,
|
|
|
|
Description: param,
|
2024-12-24 12:33:50 +08:00
|
|
|
Required: true,
|
2024-12-24 11:18:15 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2024-12-24 12:11:17 +08:00
|
|
|
|
2024-12-25 11:55:22 +08:00
|
|
|
// GetDataType 将文档配置的数据类型转换为归一化处理后的数据类型
|
2024-12-24 12:11:17 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 11:52 2024/12/24
|
2024-12-25 11:55:22 +08:00
|
|
|
func GetDataType(docParamType string, formatType string) consts.DataType {
|
2024-12-24 12:11:17 +08:00
|
|
|
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
|
2024-12-24 16:59:37 +08:00
|
|
|
case "object":
|
|
|
|
return consts.DataTypeMapStrAny
|
2024-12-25 11:55:22 +08:00
|
|
|
case "boolean":
|
|
|
|
return consts.DataTypeBool
|
|
|
|
case "number", "float", "double", "float32", "float64":
|
|
|
|
return consts.DataTypeFloat
|
2024-12-24 16:59:37 +08:00
|
|
|
case "array":
|
|
|
|
if formatType == "integer" {
|
|
|
|
return consts.DataTypeSliceInt
|
|
|
|
} else if formatType == "string" {
|
|
|
|
return consts.DataTypeSliceString
|
|
|
|
} else {
|
|
|
|
return consts.DataTypeSliceAny
|
|
|
|
}
|
2024-12-24 12:11:17 +08:00
|
|
|
default:
|
2024-12-24 12:33:50 +08:00
|
|
|
return consts.DataTypeMapStrAny
|
2024-12-24 12:11:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
2024-12-24 14:34:19 +08:00
|
|
|
|
2024-12-24 17:49:20 +08:00
|
|
|
// GetRealDefinitionsKey 通过schema下的 $ref 获取真实的 definitions key
|
2024-12-24 14:34:19 +08:00
|
|
|
func GetRealDefinitionsKey(ref string) string {
|
|
|
|
return strings.TrimPrefix(ref, "#/definitions/")
|
|
|
|
}
|
2024-12-24 17:49:20 +08:00
|
|
|
|
|
|
|
// GetRealResponseKey 通过schema下的 $ref 获取真实的 response key
|
|
|
|
func GetRealResponseKey(ref string) string {
|
|
|
|
return strings.TrimPrefix(ref, "#/responses/")
|
|
|
|
}
|
2024-12-25 11:55:22 +08:00
|
|
|
|
|
|
|
// GetSuccessResponseConfig 获取成功的响应配置
|
|
|
|
func GetSuccessResponseConfig(resultConfig map[string]*define.SwaggerPathConfigResponse) *define.SwaggerPathConfigResponse {
|
|
|
|
for httpCode := 200; httpCode <= 299; httpCode++ {
|
|
|
|
if cfg := resultConfig[fmt.Sprintf("%d", httpCode)]; nil != cfg {
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|