From 2996fc4732522703acf4282df331b516e87c330c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 5 Jan 2026 18:02:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E6=8F=8F=E8=BF=B0=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 ++ go.sum | 4 ++++ openapi/schema.go | 24 ++++++++----------- util/struct_field.go | 55 +++++++++++++++++++++++--------------------- 4 files changed, 44 insertions(+), 41 deletions(-) diff --git a/go.mod b/go.mod index e49c936..240354d 100644 --- a/go.mod +++ b/go.mod @@ -49,9 +49,11 @@ require ( github.com/leodido/go-urn v1.4.0 // indirect github.com/mailru/easyjson v0.9.1 // 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/reflect2 v1.0.2 // 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/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect diff --git a/go.sum b/go.sum index 796f7b6..2184c20 100644 --- a/go.sum +++ b/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/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/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-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= 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/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/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/go.mod h1:2bpvgLBZEtENV5scfDFEtB/5+1M4hkQhDQrccEJ/qGw= github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 h1:bQx3WeLcUWy+RletIKwUIt4x3t8n2SxavmoclizMb8c= diff --git a/openapi/schema.go b/openapi/schema.go index 04bf40d..2e8b5b4 100644 --- a/openapi/schema.go +++ b/openapi/schema.go @@ -13,6 +13,8 @@ import ( "strings" "time" + "git.zhangdeman.cn/zhangdeman/api-doc/define" + "git.zhangdeman.cn/zhangdeman/api-doc/util" "github.com/getkin/kin-openapi/openapi3" ) @@ -35,10 +37,6 @@ type StructFieldInfo struct { OmitEmpty bool `json:"omit_empty" dc:"是否可控"` } -func getDesc(field reflect.StructField) string { - -} - // ParseStructField 解析结构体字段信息 func ParseStructField(field reflect.StructField) *StructFieldInfo { if !field.IsExported() { @@ -51,7 +49,7 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo { } // 解析 JSON tag - jsonTag := field.Tag.Get("json") + jsonTag := field.Tag.Get(define.TagJson) if jsonTag != "" { parts := strings.Split(jsonTag, ",") if len(parts) > 0 { @@ -64,7 +62,7 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo { } for _, part := range parts[1:] { switch part { - case "omitempty": + case define.TagNameOmitempty: info.OmitEmpty = true } } @@ -75,15 +73,11 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo { info.JSONName = info.Name } - // 解析其他标签 - if desc := field.Tag.Get("description"); desc != "" { - info.Description = desc - } - - if example := field.Tag.Get("example"); example != "" { - info.Example = parseExampleValue(example, field.Type) - } - + // 解析参数描述 + info.Description = util.ParseStructFieldTag.GetParamDesc(field) + // 解析示例值 + info.Example = util.ParseStructFieldTag.GetExampleValue(field) + // 解析验证规则 if required := field.Tag.Get("required"); required == "true" { info.Required = true } diff --git a/util/struct_field.go b/util/struct_field.go index fc6f68c..6cc34d1 100644 --- a/util/struct_field.go +++ b/util/struct_field.go @@ -13,6 +13,7 @@ import ( "strings" "git.zhangdeman.cn/zhangdeman/api-doc/define" + utilPkg "git.zhangdeman.cn/zhangdeman/util" ) var ( @@ -154,36 +155,38 @@ func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField) // GetExampleValue 示例值 func (psf parseStructFieldTag) GetExampleValue(structField reflect.StructField) any { - descTagList := []string{define.TagEg, define.TagExample} - fieldType := structField.Type.Kind().String() - for _, tag := range descTagList { - val := strings.TrimSpace(structField.Tag.Get(tag)) - if val == "" { + exampleTagList := []string{define.TagEg, define.TagExample} + structFieldType := structField.Type + if structFieldType.Kind() == reflect.Ptr { + structFieldType = structFieldType.Elem() + } + fieldTypeKind := structFieldType.Kind() + for _, tag := range exampleTagList { + example := strings.TrimSpace(structField.Tag.Get(tag)) + if example == "" { continue } - if strings.HasPrefix(fieldType, "int") { - i, _ := strconv.Atoi(val) - return i - } - if strings.HasPrefix(fieldType, "uint") { - uintVal, _ := strconv.ParseUint(val, 10, 64) - return uintVal - } - 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 + switch fieldTypeKind { + case reflect.String: + return example + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + var res int64 + if err := utilPkg.ConvertAssign(&res, example); err == nil { + return res } + 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 }