feat: 增加枚举值解析

This commit is contained in:
2026-01-05 18:23:23 +08:00
parent 8ce50fd235
commit 1157973b65
3 changed files with 28 additions and 27 deletions

View File

@@ -36,3 +36,8 @@ const (
TagNameOmitempty = "omitempty"
TagNameEnumDescription = "enum-desc" // 枚举值描述: enum1:enum1-desc||enum2:enum2-desc
)
type EnumValue struct {
Value string `json:"value" dc:"枚举值"`
Description string `json:"description" dc:"枚举值描述"`
}

View File

@@ -20,21 +20,21 @@ import (
// StructFieldInfo 结构体字段信息
type StructFieldInfo struct {
Name string `json:"name" dc:"结构体字段名"`
JSONName string `json:"json_name" dc:"json tag"`
Type reflect.Type `json:"type" dc:"字段类型"`
Description string `json:"description" dc:"参数描述"`
Example any `json:"example" dc:"示例值"`
Default any `json:"default" dc:"默认值"`
Required bool `json:"required" dc:"是否必传"`
Min *float64 `json:"min" dc:"最小值"`
Max *float64 `json:"max" dc:"最大值"`
MinLength *uint64 `json:"min_length" dc:"最小长度"`
MaxLength *uint64 `json:"max_length" dc:"最大长度"`
Pattern string `json:"pattern" dc:"模式"`
Format string `json:"format" dc:"格式"`
Enum []any `json:"enum" dc:"枚举值列表"`
OmitEmpty bool `json:"omit_empty" dc:"是否可控"`
Name string `json:"name" dc:"结构体字段名"`
JSONName string `json:"json_name" dc:"json tag"`
Type reflect.Type `json:"type" dc:"字段类型"`
Description string `json:"description" dc:"参数描述"`
Example any `json:"example" dc:"示例值"`
Default any `json:"default" dc:"默认值"`
Required bool `json:"required" dc:"是否必传"`
Min *float64 `json:"min" dc:"最小值"`
Max *float64 `json:"max" dc:"最大值"`
MinLength *uint64 `json:"min_length" dc:"最小长度"`
MaxLength *uint64 `json:"max_length" dc:"最大长度"`
Pattern string `json:"pattern" dc:"模式"`
Format string `json:"format" dc:"格式"`
Enum []define.EnumValue `json:"enum" dc:"枚举值列表"`
OmitEmpty bool `json:"omit_empty" dc:"是否可控"`
}
// ParseStructField 解析结构体字段信息
@@ -114,12 +114,8 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo {
info.Format = format
}
if enum := field.Tag.Get("enum"); enum != "" {
enums := strings.Split(enum, ",")
for _, e := range enums {
info.Enum = append(info.Enum, strings.TrimSpace(e))
}
}
// 解析枚举值
info.Enum = util.ParseStructFieldTag.EnumDescription(field)
return info
}

View File

@@ -130,9 +130,9 @@ func (psf parseStructFieldTag) Summary(structField reflect.StructField) string {
}
// EnumDescription .枚举值详细描述
func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField) map[string]string {
func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField) []define.EnumValue {
defaultTagList := []string{define.TagNameEnumDescription}
res := map[string]string{}
res := make([]define.EnumValue, 0)
for _, tag := range defaultTagList {
if tagVal, exist := structField.Tag.Lookup(tag); exist && len(tagVal) > 0 {
tagVal = strings.ReplaceAll(tagVal, "###", "`")
@@ -142,14 +142,14 @@ func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField)
if len(enumArr) < 2 {
continue
}
res[enumArr[0]] = strings.Join(enumArr[1:], ":")
res = append(res, define.EnumValue{
Value: enumArr[0],
Description: strings.Join(enumArr[1:], ":"),
})
}
return res
}
}
if len(res) == 0 {
return nil
}
return res
}