feat: 完善字段描述获取
This commit is contained in:
2
go.mod
2
go.mod
@@ -49,9 +49,11 @@ require (
|
|||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
github.com/mailru/easyjson v0.9.1 // indirect
|
github.com/mailru/easyjson v0.9.1 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
|
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
|
||||||
|
github.com/mozillazg/go-pinyin v0.21.0 // indirect
|
||||||
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect
|
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect
|
||||||
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
|
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -103,6 +103,8 @@ github.com/mailru/easyjson v0.9.1 h1:LbtsOm5WAswyWbvTEOqhypdPeZzHavpZx96/n553mR8
|
|||||||
github.com/mailru/easyjson v0.9.1/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
|
github.com/mailru/easyjson v0.9.1/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
|
||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
@@ -110,6 +112,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
|
|||||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
|
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
|
||||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
|
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
|
||||||
|
github.com/mozillazg/go-pinyin v0.21.0 h1:Wo8/NT45z7P3er/9YSLHA3/kjZzbLz5hR7i+jGeIGao=
|
||||||
|
github.com/mozillazg/go-pinyin v0.21.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||||
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 h1:G7ERwszslrBzRxj//JalHPu/3yz+De2J+4aLtSRlHiY=
|
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 h1:G7ERwszslrBzRxj//JalHPu/3yz+De2J+4aLtSRlHiY=
|
||||||
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037/go.mod h1:2bpvgLBZEtENV5scfDFEtB/5+1M4hkQhDQrccEJ/qGw=
|
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037/go.mod h1:2bpvgLBZEtENV5scfDFEtB/5+1M4hkQhDQrccEJ/qGw=
|
||||||
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 h1:bQx3WeLcUWy+RletIKwUIt4x3t8n2SxavmoclizMb8c=
|
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 h1:bQx3WeLcUWy+RletIKwUIt4x3t8n2SxavmoclizMb8c=
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.zhangdeman.cn/zhangdeman/api-doc/define"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/api-doc/util"
|
||||||
"github.com/getkin/kin-openapi/openapi3"
|
"github.com/getkin/kin-openapi/openapi3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,10 +37,6 @@ type StructFieldInfo struct {
|
|||||||
OmitEmpty bool `json:"omit_empty" dc:"是否可控"`
|
OmitEmpty bool `json:"omit_empty" dc:"是否可控"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDesc(field reflect.StructField) string {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseStructField 解析结构体字段信息
|
// ParseStructField 解析结构体字段信息
|
||||||
func ParseStructField(field reflect.StructField) *StructFieldInfo {
|
func ParseStructField(field reflect.StructField) *StructFieldInfo {
|
||||||
if !field.IsExported() {
|
if !field.IsExported() {
|
||||||
@@ -51,7 +49,7 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 解析 JSON tag
|
// 解析 JSON tag
|
||||||
jsonTag := field.Tag.Get("json")
|
jsonTag := field.Tag.Get(define.TagJson)
|
||||||
if jsonTag != "" {
|
if jsonTag != "" {
|
||||||
parts := strings.Split(jsonTag, ",")
|
parts := strings.Split(jsonTag, ",")
|
||||||
if len(parts) > 0 {
|
if len(parts) > 0 {
|
||||||
@@ -64,7 +62,7 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo {
|
|||||||
}
|
}
|
||||||
for _, part := range parts[1:] {
|
for _, part := range parts[1:] {
|
||||||
switch part {
|
switch part {
|
||||||
case "omitempty":
|
case define.TagNameOmitempty:
|
||||||
info.OmitEmpty = true
|
info.OmitEmpty = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,15 +73,11 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo {
|
|||||||
info.JSONName = info.Name
|
info.JSONName = info.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析其他标签
|
// 解析参数描述
|
||||||
if desc := field.Tag.Get("description"); desc != "" {
|
info.Description = util.ParseStructFieldTag.GetParamDesc(field)
|
||||||
info.Description = desc
|
// 解析示例值
|
||||||
}
|
info.Example = util.ParseStructFieldTag.GetExampleValue(field)
|
||||||
|
// 解析验证规则
|
||||||
if example := field.Tag.Get("example"); example != "" {
|
|
||||||
info.Example = parseExampleValue(example, field.Type)
|
|
||||||
}
|
|
||||||
|
|
||||||
if required := field.Tag.Get("required"); required == "true" {
|
if required := field.Tag.Get("required"); required == "true" {
|
||||||
info.Required = true
|
info.Required = true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/api-doc/define"
|
"git.zhangdeman.cn/zhangdeman/api-doc/define"
|
||||||
|
utilPkg "git.zhangdeman.cn/zhangdeman/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -154,36 +155,38 @@ func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField)
|
|||||||
|
|
||||||
// GetExampleValue 示例值
|
// GetExampleValue 示例值
|
||||||
func (psf parseStructFieldTag) GetExampleValue(structField reflect.StructField) any {
|
func (psf parseStructFieldTag) GetExampleValue(structField reflect.StructField) any {
|
||||||
descTagList := []string{define.TagEg, define.TagExample}
|
exampleTagList := []string{define.TagEg, define.TagExample}
|
||||||
fieldType := structField.Type.Kind().String()
|
structFieldType := structField.Type
|
||||||
for _, tag := range descTagList {
|
if structFieldType.Kind() == reflect.Ptr {
|
||||||
val := strings.TrimSpace(structField.Tag.Get(tag))
|
structFieldType = structFieldType.Elem()
|
||||||
if val == "" {
|
}
|
||||||
|
fieldTypeKind := structFieldType.Kind()
|
||||||
|
for _, tag := range exampleTagList {
|
||||||
|
example := strings.TrimSpace(structField.Tag.Get(tag))
|
||||||
|
if example == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(fieldType, "int") {
|
switch fieldTypeKind {
|
||||||
i, _ := strconv.Atoi(val)
|
case reflect.String:
|
||||||
return i
|
return example
|
||||||
}
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
if strings.HasPrefix(fieldType, "uint") {
|
var res int64
|
||||||
uintVal, _ := strconv.ParseUint(val, 10, 64)
|
if err := utilPkg.ConvertAssign(&res, example); err == nil {
|
||||||
return uintVal
|
return res
|
||||||
}
|
|
||||||
if strings.HasPrefix(fieldType, "float") {
|
|
||||||
floatVal, _ := strconv.ParseFloat(val, 64)
|
|
||||||
return floatVal
|
|
||||||
}
|
|
||||||
if strings.HasPrefix(fieldType, "string") {
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
if strings.HasPrefix(fieldType, "bool") {
|
|
||||||
if val == "true" {
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
var res float64
|
||||||
|
if err := utilPkg.ConvertAssign(&res, example); err == nil {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
case reflect.Bool:
|
||||||
|
var res bool
|
||||||
|
if err := utilPkg.ConvertAssign(&res, example); err == nil {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return example
|
||||||
}
|
}
|
||||||
return val
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user