feat: 增加枚举值解析
This commit is contained in:
@@ -36,3 +36,8 @@ const (
|
|||||||
TagNameOmitempty = "omitempty"
|
TagNameOmitempty = "omitempty"
|
||||||
TagNameEnumDescription = "enum-desc" // 枚举值描述: enum1:enum1-desc||enum2:enum2-desc
|
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:"最大长度"`
|
MaxLength *uint64 `json:"max_length" dc:"最大长度"`
|
||||||
Pattern string `json:"pattern" dc:"模式"`
|
Pattern string `json:"pattern" dc:"模式"`
|
||||||
Format string `json:"format" dc:"格式"`
|
Format string `json:"format" dc:"格式"`
|
||||||
Enum []any `json:"enum" dc:"枚举值列表"`
|
Enum []define.EnumValue `json:"enum" dc:"枚举值列表"`
|
||||||
OmitEmpty bool `json:"omit_empty" dc:"是否可控"`
|
OmitEmpty bool `json:"omit_empty" dc:"是否可控"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,12 +114,8 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo {
|
|||||||
info.Format = format
|
info.Format = format
|
||||||
}
|
}
|
||||||
|
|
||||||
if enum := field.Tag.Get("enum"); enum != "" {
|
// 解析枚举值
|
||||||
enums := strings.Split(enum, ",")
|
info.Enum = util.ParseStructFieldTag.EnumDescription(field)
|
||||||
for _, e := range enums {
|
|
||||||
info.Enum = append(info.Enum, strings.TrimSpace(e))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return info
|
return info
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,9 +130,9 @@ func (psf parseStructFieldTag) Summary(structField reflect.StructField) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// EnumDescription .枚举值详细描述
|
// EnumDescription .枚举值详细描述
|
||||||
func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField) map[string]string {
|
func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField) []define.EnumValue {
|
||||||
defaultTagList := []string{define.TagNameEnumDescription}
|
defaultTagList := []string{define.TagNameEnumDescription}
|
||||||
res := map[string]string{}
|
res := make([]define.EnumValue, 0)
|
||||||
for _, tag := range defaultTagList {
|
for _, tag := range defaultTagList {
|
||||||
if tagVal, exist := structField.Tag.Lookup(tag); exist && len(tagVal) > 0 {
|
if tagVal, exist := structField.Tag.Lookup(tag); exist && len(tagVal) > 0 {
|
||||||
tagVal = strings.ReplaceAll(tagVal, "###", "`")
|
tagVal = strings.ReplaceAll(tagVal, "###", "`")
|
||||||
@@ -142,14 +142,14 @@ func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField)
|
|||||||
if len(enumArr) < 2 {
|
if len(enumArr) < 2 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
res[enumArr[0]] = strings.Join(enumArr[1:], ":")
|
res = append(res, define.EnumValue{
|
||||||
|
Value: enumArr[0],
|
||||||
|
Description: strings.Join(enumArr[1:], ":"),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(res) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user