From 5ff7dac2273c26db18b92f67913ea9173c807a4b 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:46:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- openapi/schema.go | 26 ++------------------------ util/struct_field.go | 36 +++++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/openapi/schema.go b/openapi/schema.go index 20b192b..6f1dc90 100644 --- a/openapi/schema.go +++ b/openapi/schema.go @@ -10,7 +10,6 @@ package openapi import ( "reflect" "strconv" - "strings" "time" "git.zhangdeman.cn/zhangdeman/api-doc/define" @@ -48,30 +47,9 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo { Type: field.Type, } + util.ParseStructFieldTag.GetParamName(field) // 解析 JSON tag - jsonTag := field.Tag.Get(define.TagJson) - if jsonTag != "" { - parts := strings.Split(jsonTag, ",") - if len(parts) > 0 { - if parts[0] == "-" { - return nil // 跳过该字段 - } - if parts[0] != "" { - info.JSONName = parts[0] - } - } - for _, part := range parts[1:] { - switch part { - case define.TagNameOmitempty: - info.OmitEmpty = true - } - } - } - - if info.JSONName == "" { - // 使用结构体字段名作为默认的 json tag - info.JSONName = info.Name - } + info.JSONName, info.OmitEmpty = util.ParseStructFieldTag.GetParamName(field) // 解析参数描述 info.Description = util.ParseStructFieldTag.GetParamDesc(field) diff --git a/util/struct_field.go b/util/struct_field.go index 7b82fde..683c2d8 100644 --- a/util/struct_field.go +++ b/util/struct_field.go @@ -24,7 +24,7 @@ type parseStructFieldTag struct { } // GetParamName 获取参数名称 -func (psf parseStructFieldTag) GetParamName(structField reflect.StructField) string { +func (psf parseStructFieldTag) GetParamName(structField reflect.StructField) (string, bool) { paramNameTagList := []string{ define.TagJson, define.TagForm, define.TagXml, define.TagYaml, @@ -32,14 +32,30 @@ func (psf parseStructFieldTag) GetParamName(structField reflect.StructField) str } for _, tag := range paramNameTagList { tagVal := structField.Tag.Get(tag) - tagVal = strings.TrimSuffix(strings.TrimPrefix(tagVal, define.TagNameOmitempty+","), ","+define.TagNameOmitempty) - tagVal = strings.Trim(tagVal, ",") - if tagVal != "" && tagVal != define.TagNameOmitempty { - return tagVal + if tagVal == "" { + continue } + parts := strings.Split(tagVal, ",") + jsonName := "" + omit := false + if len(parts) > 0 { + if parts[0] == "-" { + return "", false // 跳过该字段 + } + if parts[0] != "" { + jsonName = parts[0] + } + } + for _, part := range parts[1:] { + switch part { + case define.TagNameOmitempty: + omit = true + } + } + return jsonName, omit } // 未设置相关字段, 则字段名即为参数名 - return structField.Name + return structField.Name, false } // GetParamDesc ... @@ -52,7 +68,8 @@ func (psf parseStructFieldTag) GetParamDesc(structField reflect.StructField) str } } // 没有显示的设置参数描述, 则使用参数名作为参数描述 - return psf.GetParamName(structField) + paramName, _ := psf.GetParamName(structField) + return paramName } // GetDefaultValue 获取默认值 @@ -122,10 +139,7 @@ func (psf parseStructFieldTag) Summary(structField reflect.StructField) string { return tagVal } } - paramName := psf.GetParamName(structField) - if paramName == "-" { - return "" - } + paramName, _ := psf.GetParamName(structField) return paramName }