feat: 增加枚举值解析
This commit is contained in:
@@ -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:"枚举值描述"`
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ type StructFieldInfo struct {
|
||||
MaxLength *uint64 `json:"max_length" dc:"最大长度"`
|
||||
Pattern string `json:"pattern" dc:"模式"`
|
||||
Format string `json:"format" dc:"格式"`
|
||||
Enum []any `json:"enum" dc:"枚举值列表"`
|
||||
Enum []define.EnumValue `json:"enum" dc:"枚举值列表"`
|
||||
OmitEmpty bool `json:"omit_empty" dc:"是否可控"`
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user