feat: 枚举值增加 x-enum-descriptions 用于标记枚举值含义
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
|||||||
func TestGenerate_AddApiDoc(t *testing.T) {
|
func TestGenerate_AddApiDoc(t *testing.T) {
|
||||||
type Category struct {
|
type Category struct {
|
||||||
ID int64 `json:"id" description:"分类ID" eg:"123" binding:"required,min=10,max=100"`
|
ID int64 `json:"id" description:"分类ID" eg:"123" binding:"required,min=10,max=100"`
|
||||||
|
Sex string `json:"sex" dc:"性别" binding:"required,oneof=man woman" enum-desc:"man:男||woman:女"`
|
||||||
Name string `json:"name" description:"分类名称" eg:"baichaqinghuan" binding:"required,min=8"`
|
Name string `json:"name" description:"分类名称" eg:"baichaqinghuan" binding:"required,min=8"`
|
||||||
}
|
}
|
||||||
type Product struct {
|
type Product struct {
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/api-doc/define"
|
|
||||||
"git.zhangdeman.cn/zhangdeman/api-doc/util"
|
"git.zhangdeman.cn/zhangdeman/api-doc/util"
|
||||||
"github.com/getkin/kin-openapi/openapi3"
|
"github.com/getkin/kin-openapi/openapi3"
|
||||||
)
|
)
|
||||||
@@ -33,7 +32,7 @@ type StructFieldInfo struct {
|
|||||||
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 []any `json:"enum" dc:"枚举值列表"`
|
||||||
EnumDesc []define.EnumValue `json:"enum_desc" dc:"枚举值详细描述"`
|
EnumDesc map[string]string `json:"enum_desc" dc:"枚举值详细描述"`
|
||||||
OmitEmpty bool `json:"omit_empty" dc:"是否可控"`
|
OmitEmpty bool `json:"omit_empty" dc:"是否可控"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,9 +211,10 @@ func applyFieldInfoToSchema(schema *openapi3.Schema, info *StructFieldInfo) {
|
|||||||
}
|
}
|
||||||
if len(info.Enum) > 0 {
|
if len(info.Enum) > 0 {
|
||||||
schema.Enum = info.Enum
|
schema.Enum = info.Enum
|
||||||
/*for _, item := range info.Enum {
|
if nil == schema.Extensions {
|
||||||
schema.Enum = append(schema.Enum, item.Value)
|
schema.Extensions = map[string]any{}
|
||||||
}*/
|
}
|
||||||
|
schema.Extensions["x-enum-descriptions"] = info.EnumDesc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -144,9 +144,9 @@ func (psf parseStructFieldTag) Summary(structField reflect.StructField) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// EnumDescription .枚举值详细描述
|
// EnumDescription .枚举值详细描述
|
||||||
func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField) []define.EnumValue {
|
func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField) map[string]string {
|
||||||
defaultTagList := []string{define.TagNameEnumDescription}
|
defaultTagList := []string{define.TagNameEnumDescription}
|
||||||
res := make([]define.EnumValue, 0)
|
res := make(map[string]string)
|
||||||
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, "###", "`")
|
||||||
@@ -156,10 +156,7 @@ func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField)
|
|||||||
if len(enumArr) < 2 {
|
if len(enumArr) < 2 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
res = append(res, define.EnumValue{
|
res[enumArr[0]] = strings.Join(enumArr[1:], ":")
|
||||||
Value: enumArr[0],
|
|
||||||
Description: strings.Join(enumArr[1:], ":"),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user