2025-02-12 22:20:50 +08:00
|
|
|
// Package api_doc ...
|
|
|
|
//
|
|
|
|
// Description : api_doc ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2025-02-12 22:15
|
|
|
|
package api_doc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.zhangdeman.cn/gateway/api-doc/define"
|
|
|
|
"reflect"
|
2025-02-13 21:11:27 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2025-02-12 22:20:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2025-02-18 10:08:45 +08:00
|
|
|
ParseStructFieldTag = parseStructFieldTag{}
|
2025-02-12 22:20:50 +08:00
|
|
|
)
|
|
|
|
|
2025-02-18 10:08:45 +08:00
|
|
|
type parseStructFieldTag struct {
|
2025-02-12 22:20:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetParamName 获取参数名称
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:58 2025/2/11
|
2025-02-18 10:08:45 +08:00
|
|
|
func (psf parseStructFieldTag) GetParamName(structField reflect.StructField) string {
|
2025-02-12 22:20:50 +08:00
|
|
|
paramNameTagList := []string{
|
|
|
|
define.TagJson, define.TagForm,
|
|
|
|
define.TagXml, define.TagYaml,
|
|
|
|
define.TagYml,
|
|
|
|
}
|
|
|
|
for _, tag := range paramNameTagList {
|
|
|
|
tagVal := structField.Tag.Get(tag)
|
2025-02-17 18:10:51 +08:00
|
|
|
tagVal = strings.TrimSuffix(strings.TrimPrefix(tagVal, define.TagNameOmitempty+","), ","+define.TagNameOmitempty)
|
|
|
|
tagVal = strings.Trim(tagVal, ",")
|
|
|
|
if tagVal != "" && tagVal != define.TagNameOmitempty {
|
2025-02-12 22:20:50 +08:00
|
|
|
return tagVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 未设置相关字段, 则字段名即为参数名
|
|
|
|
return structField.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetParamDesc ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 22:01 2025/2/11
|
2025-02-18 10:08:45 +08:00
|
|
|
func (psf parseStructFieldTag) GetParamDesc(structField reflect.StructField) string {
|
2025-02-18 14:56:18 +08:00
|
|
|
descTagList := []string{define.TagDc, define.TagDesc, define.TagDescription}
|
2025-02-12 22:20:50 +08:00
|
|
|
for _, tag := range descTagList {
|
|
|
|
tagVal := structField.Tag.Get(tag)
|
|
|
|
if tagVal != "" {
|
2025-02-18 21:26:20 +08:00
|
|
|
return strings.ReplaceAll(tagVal, "###", "`")
|
2025-02-12 22:20:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 没有显示的设置参数描述, 则使用参数名作为参数描述
|
|
|
|
return psf.GetParamName(structField)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefaultValue 获取默认值
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 22:05 2025/2/11
|
2025-02-18 10:08:45 +08:00
|
|
|
func (psf parseStructFieldTag) GetDefaultValue(structField reflect.StructField) any {
|
2025-02-12 22:20:50 +08:00
|
|
|
defaultTagList := []string{define.TagD, define.TagDefault}
|
2025-02-13 21:11:27 +08:00
|
|
|
fieldType := structField.Type.Kind().String()
|
2025-02-12 22:20:50 +08:00
|
|
|
for _, tag := range defaultTagList {
|
2025-02-18 22:21:53 +08:00
|
|
|
val, exist := structField.Tag.Lookup(tag)
|
|
|
|
val = strings.TrimSpace(val)
|
|
|
|
if !exist || len(val) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fieldType, "int") {
|
|
|
|
i, _ := strconv.Atoi(val)
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fieldType, "uint") {
|
|
|
|
uintVal, _ := strconv.ParseUint(val, 10, 64)
|
|
|
|
return uintVal
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fieldType, "float") {
|
|
|
|
floatVal, _ := strconv.ParseFloat(val, 64)
|
|
|
|
return floatVal
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fieldType, "string") {
|
2025-02-13 21:11:27 +08:00
|
|
|
return val
|
2025-02-12 22:20:50 +08:00
|
|
|
}
|
2025-02-18 22:21:53 +08:00
|
|
|
if strings.HasPrefix(fieldType, "bool") {
|
|
|
|
if val == "true" {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val
|
2025-02-12 22:20:50 +08:00
|
|
|
}
|
2025-02-13 21:11:27 +08:00
|
|
|
return nil
|
2025-02-12 22:20:50 +08:00
|
|
|
}
|
2025-02-13 16:03:31 +08:00
|
|
|
|
|
|
|
// GetValidateRule 获取验证规则
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:30 2025/2/13
|
2025-02-18 10:08:45 +08:00
|
|
|
func (psf parseStructFieldTag) GetValidateRule(structField reflect.StructField) string {
|
2025-02-13 16:03:31 +08:00
|
|
|
defaultTagList := []string{define.TagValidate, define.TagBinding}
|
|
|
|
for _, tag := range defaultTagList {
|
|
|
|
if tagVal, exist := structField.Tag.Lookup(tag); exist && len(tagVal) > 0 {
|
|
|
|
return tagVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2025-02-13 21:57:18 +08:00
|
|
|
|
|
|
|
// Deprecated 是否弃用
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:12 2025/2/13
|
2025-02-18 10:08:45 +08:00
|
|
|
func (psf parseStructFieldTag) Deprecated(structField reflect.StructField) bool {
|
2025-02-13 21:57:18 +08:00
|
|
|
defaultTagList := []string{define.TagDeprecated}
|
|
|
|
for _, tag := range defaultTagList {
|
|
|
|
if tagVal, exist := structField.Tag.Lookup(tag); exist && (tagVal == "1" || strings.ToLower(tagVal) == "true") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2025-02-14 15:31:53 +08:00
|
|
|
|
|
|
|
// Summary 摘要信息
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:15 2025/2/14
|
2025-02-18 10:08:45 +08:00
|
|
|
func (psf parseStructFieldTag) Summary(structField reflect.StructField) string {
|
2025-02-14 15:31:53 +08:00
|
|
|
defaultTagList := []string{define.TagSummary}
|
|
|
|
for _, tag := range defaultTagList {
|
|
|
|
if tagVal, exist := structField.Tag.Lookup(tag); exist && len(tagVal) > 0 {
|
|
|
|
return tagVal
|
|
|
|
}
|
|
|
|
}
|
2025-02-14 18:22:53 +08:00
|
|
|
paramName := psf.GetParamName(structField)
|
|
|
|
if paramName == "-" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return paramName
|
2025-02-14 15:31:53 +08:00
|
|
|
}
|
2025-02-18 23:02:46 +08:00
|
|
|
|
|
|
|
// EnumDescription .枚举值详细描述
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 22:40 2025/2/18
|
|
|
|
func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField) map[string]string {
|
|
|
|
defaultTagList := []string{define.TagNameEnumDescription}
|
|
|
|
res := map[string]string{}
|
|
|
|
for _, tag := range defaultTagList {
|
|
|
|
if tagVal, exist := structField.Tag.Lookup(tag); exist && len(tagVal) > 0 {
|
|
|
|
tagVal = strings.ReplaceAll(tagVal, "###", "`")
|
|
|
|
enumList := strings.Split(tagVal, "||")
|
|
|
|
for _, enum := range enumList {
|
|
|
|
enumArr := strings.Split(enum, ":")
|
|
|
|
if len(enumArr) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res[enumArr[0]] = strings.Join(enumArr[1:], ":")
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(res) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2025-02-20 16:22:23 +08:00
|
|
|
|
|
|
|
// GetExampleValue 示例值
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 14:42 2025/2/20
|
2025-02-20 18:50:16 +08:00
|
|
|
func (psf parseStructFieldTag) GetExampleValue(structField reflect.StructField) any {
|
2025-02-20 16:22:23 +08:00
|
|
|
descTagList := []string{define.TagEg, define.TagExample}
|
2025-02-20 18:50:16 +08:00
|
|
|
fieldType := structField.Type.Kind().String()
|
2025-02-20 16:22:23 +08:00
|
|
|
for _, tag := range descTagList {
|
2025-02-20 18:50:16 +08:00
|
|
|
val := strings.TrimSpace(structField.Tag.Get(tag))
|
|
|
|
if val == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fieldType, "int") {
|
|
|
|
i, _ := strconv.Atoi(val)
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fieldType, "uint") {
|
|
|
|
uintVal, _ := strconv.ParseUint(val, 10, 64)
|
|
|
|
return uintVal
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fieldType, "float") {
|
|
|
|
floatVal, _ := strconv.ParseFloat(val, 64)
|
|
|
|
return floatVal
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fieldType, "string") {
|
|
|
|
return val
|
2025-02-20 16:22:23 +08:00
|
|
|
}
|
2025-02-20 18:50:16 +08:00
|
|
|
if strings.HasPrefix(fieldType, "bool") {
|
|
|
|
if val == "true" {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val
|
2025-02-20 16:22:23 +08:00
|
|
|
}
|
2025-02-20 18:50:16 +08:00
|
|
|
return nil
|
2025-02-20 16:22:23 +08:00
|
|
|
}
|