From f9a1222712594d76df5ebb7e314f55b8135cdc15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 11 Feb 2025 22:10:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A0=87=E7=AD=BE=E8=8E=B7?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/openapi.go | 10 ++++----- define/tag.go | 24 ++++++++++++++++++++ generate.go | 57 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 define/tag.go diff --git a/define/openapi.go b/define/openapi.go index 7767261..404a772 100644 --- a/define/openapi.go +++ b/define/openapi.go @@ -140,11 +140,11 @@ type Schema struct { // // Date : 17:05 2024/7/19 type Property struct { - Type string `json:"type"` // 数据类型, swagger本身的定义 - Format string `json:"format"` // 对应编程语言中的数据类型描述 - Enum []any `json:"enum,omitempty"` // 枚举值列表 - Default any `json:"default"` // 默认值 : 不同于 JSON Schema,这个值必须符合定义与相同级别的 Schema 对象 中定义的类型,比如 type 是 string,那么 default 可以是 "foo" 但不能是 1。 - Description string `json:"description"` // 数据描述, CommonMark syntax可以被用来呈现富文本格式. + Type string `json:"type,omitempty"` // 数据类型, swagger本身的定义 + Format string `json:"format,omitempty"` // 对应编程语言中的数据类型描述 + Enum []string `json:"enum,omitempty"` // 枚举值列表 + Default string `json:"default,omitempty"` // 默认值 : 不同于 JSON Schema,这个值必须符合定义与相同级别的 Schema 对象 中定义的类型,比如 type 是 string,那么 default 可以是 "foo" 但不能是 1。 + Description string `json:"description,omitempty"` // 数据描述, CommonMark syntax可以被用来呈现富文本格式. AllOf []*PropertyXOf `json:"allOf,omitempty"` // type 是一个对象, allOf 指向对象描述 OneOf []*PropertyXOf `json:"oneOf,omitempty"` // type 是一个对象, allOf 指向对象描述 AnyOf []*PropertyXOf `json:"anyOf,omitempty"` // type 是一个对象, allOf 指向对象描述 diff --git a/define/tag.go b/define/tag.go new file mode 100644 index 0000000..5528dcb --- /dev/null +++ b/define/tag.go @@ -0,0 +1,24 @@ +// Package define ... +// +// Description : define ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-02-11 21:51 +package define + +const ( + TagJson = "json" + TagXml = "xml" + TagYaml = "yaml" + TagYml = "yml" + TagForm = "form" + TagBinding = "binding" + TagValidate = "validate" + TagErr = "err" + TagMsg = "msg" + TagDesc = "desc" + TagDescription = "description" + TagD = "d" + TagDefault = "default" +) diff --git a/generate.go b/generate.go index 13a56c4..83c31c1 100644 --- a/generate.go +++ b/generate.go @@ -274,7 +274,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string, Type: g.realType2SwaggerType(inputType.String()), Format: inputType.String(), Enum: nil, - Default: nil, + Default: "", Description: "", AllOf: nil, OneOf: nil, @@ -401,7 +401,7 @@ func (g *Generate) parseSliceItem(rootSchemaName string, inputType reflect.Type) Type: "", Format: inputType.String(), Enum: nil, - Default: nil, + Default: "", Description: "", AllOf: nil, OneOf: nil, @@ -465,3 +465,56 @@ func (g *Generate) realType2SwaggerType(realType string) string { } return g.realBaseType2SwaggerType(realType) } + +// getParamName 获取参数名称 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:58 2025/2/11 +func (g *Generate) getParamName(structField reflect.StructField) string { + paramNameTagList := []string{ + define.TagJson, define.TagForm, + define.TagXml, define.TagYaml, + define.TagYml, + } + for _, tag := range paramNameTagList { + tagVal := structField.Tag.Get(tag) + if tagVal != "" { + return tagVal + } + } + // 未设置相关字段, 则字段名即为参数名 + return structField.Name +} + +// getParamDesc ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 22:01 2025/2/11 +func (g *Generate) getParamDesc(structField reflect.StructField) string { + descTagList := []string{define.TagDesc, define.TagDescription} + for _, tag := range descTagList { + tagVal := structField.Tag.Get(tag) + if tagVal != "" { + return tagVal + } + } + // 没有显示的设置参数描述, 则使用参数名作为参数描述 + return g.getParamName(structField) +} + +// getDefaultValue 获取默认值 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 22:05 2025/2/11 +func (g *Generate) getDefaultValue(structField reflect.StructField) string { + defaultTagList := []string{define.TagD, define.TagDefault} + for _, tag := range defaultTagList { + if tagVal, exist := structField.Tag.Lookup(tag); exist { + return tagVal + } + } + return "" +}