From f51388c1d64de52e76c65b1d1ad78e18aa1eb2a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 14 Feb 2025 15:31:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0get=E7=B1=BB=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=20body=20=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/tag.go | 49 +++++++++++++++----------------- generate.go | 75 ++++++++++++++++++++++++++----------------------- struct_field.go | 15 ++++++++++ 3 files changed, 77 insertions(+), 62 deletions(-) diff --git a/define/tag.go b/define/tag.go index ac42381..4f89323 100644 --- a/define/tag.go +++ b/define/tag.go @@ -8,31 +8,26 @@ 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" - TagDeprecated = "deprecated" -) - -const ( - TagNamePath = "path" // 接口的请求路径 - TagNameMethod = "method" // 接口的请求方法 - TagNameUriTag = "tag" // 接口的tag - TagNameDesc = "desc" // 接口的描述 - TagNameOutputStrict = "output_strict" // 接口数据是否为严格模式 : 严格模式, 响应数据必须是结构体/map,非严格模式返回任意值 - TagNameBinding = "binding" // gin 内置的验证规则tag - TagNameValidate = "validate" // validator v10 默认的验证规则tag - TagNameErrMsg = "err" // 验证失败错误信息tag - TagNameContentType = "content_type" - TagNameOutputContentType = "output_content_type" + 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" + TagDeprecated = "deprecated" + TagSummary = "summary" + TagPath = "path" // 接口的请求路径 + TagMethod = "method" // 接口的请求方法 + TagUriTag = "tag" // 接口的tag + TagOutputStrict = "output_strict" // 接口数据是否为严格模式 : 严格模式, 响应数据必须是结构体/map,非严格模式返回任意值 + TagErrMsg = "err" // 验证失败错误信息tag + TagContentType = "content_type" + TagOutputContentType = "output_content_type" ) diff --git a/generate.go b/generate.go index 6963c13..b1f6dae 100644 --- a/generate.go +++ b/generate.go @@ -177,17 +177,10 @@ func (g *Generate) AddApiFromInAndOut(paramType reflect.Type, resultType reflect if resultType.Kind() == reflect.Ptr { resultType = resultType.Elem() } - baseCfg := g.parseBaseUriConfig(paramType) - if nil == baseCfg { - return errors.New("baseCfg is nil") + baseCfg, err := g.parseBaseUriConfig(paramType) + if nil != err { + return err } - if baseCfg.Method == "" { - return errors.New("baseCfg.Method is empty") - } - if baseCfg.Uri == "" { - return errors.New("baseCfg.Uri is empty") - } - baseCfg.Method = strings.ToUpper(baseCfg.Method) if _, exist := g.docData.Paths[baseCfg.Uri]; !exist { g.docData.Paths[baseCfg.Uri] = &define.PathConfig{} } @@ -238,26 +231,26 @@ func (g *Generate) AddApiFromInAndOut(paramType reflect.Type, resultType reflect http.MethodGet, http.MethodHead, http.MethodConnect, http.MethodOptions, http.MethodTrace, } if wrapper.ArrayType(paramMethod).Has(baseCfg.Method) >= 0 { + cfg.RequestBody = nil // get类请求没有request body // Get类请求, TODO : get类解析 // 参数解析 g.ParseReadConfigParam(baseCfg, cfg, paramType) - // 返回值解析 - g.AddComponentsSchema("", resultType.PkgPath(), resultType) - return nil - } - // post类解析 - paramSchemaName := g.AddComponentsSchema("", paramType.PkgPath(), paramType) - resultSchemaName := g.AddComponentsSchema("", resultType.PkgPath(), resultType) - for _, itemType := range baseCfg.ContentType { - cfg.RequestBody.Content[itemType] = &define.Media{ - Schema: &define.Schema{ - Ref: g.getSchemaRef(paramSchemaName), - }, - Example: "", - Examples: nil, - Encoding: nil, + } else { + // post类解析 + paramSchemaName := g.AddComponentsSchema("", paramType.PkgPath(), paramType) + for _, itemType := range baseCfg.ContentType { + cfg.RequestBody.Content[itemType] = &define.Media{ + Schema: &define.Schema{ + Ref: g.getSchemaRef(paramSchemaName), + }, + Example: "", + Examples: nil, + Encoding: nil, + } } } + // 无论什么请求, 对于result解析逻辑一致 + resultSchemaName := g.AddComponentsSchema("", resultType.PkgPath(), resultType) for _, itemOutputType := range baseCfg.OutputContentType { cfg.Responses[fmt.Sprintf("%v", http.StatusOK)].Content[itemOutputType] = &define.Media{ Schema: &define.Schema{ @@ -568,11 +561,16 @@ func (g *Generate) setStructFieldProperty(schemaName string, structField reflect g.docData.Components.Schemas[schemaName].Properties[ParseStructField.GetParamName(structField)].Enum = ValidateRule.Enum(structField) } -func (g *Generate) parseBaseUriConfig(paramType reflect.Type) *define.UriBaseConfig { +// parseBaseUriConfig 通过Meta字段解析Uri基础配置信息 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:13 2025/2/14 +func (g *Generate) parseBaseUriConfig(paramType reflect.Type) (*define.UriBaseConfig, error) { // 解析meta信息 metaField, metaFieldExist := paramType.FieldByName("Meta") if !metaFieldExist { - return nil + return nil, errors.New("Meta field not found") } res := &define.UriBaseConfig{ Uri: "", @@ -586,16 +584,23 @@ func (g *Generate) parseBaseUriConfig(paramType reflect.Type) *define.UriBaseCon ResultList: nil, Deprecated: false, } - res.Uri = metaField.Tag.Get(define.TagNamePath) - res.Method = metaField.Tag.Get(define.TagNameMethod) - res.Description = metaField.Tag.Get(define.TagNameDesc) - res.TagList = strings.Split(metaField.Tag.Get(define.TagNameUriTag), ",") + res.Uri = metaField.Tag.Get(define.TagPath) + res.Method = strings.ToUpper(metaField.Tag.Get(define.TagMethod)) + res.Description = metaField.Tag.Get(define.TagDesc) + res.TagList = strings.Split(metaField.Tag.Get(define.TagUriTag), ",") // 解析第一个返回值, 要求必须是结构体或者是map - outputStrictModel := metaField.Tag.Get(define.TagNameOutputStrict) + outputStrictModel := metaField.Tag.Get(define.TagOutputStrict) res.OutputStrict = outputStrictModel == "1" || outputStrictModel == "true" deprecated := metaField.Tag.Get(define.TagDeprecated) res.Deprecated = deprecated == "1" || deprecated == "true" - res.ContentType = strings.Split(metaField.Tag.Get(define.TagNameContentType), ",") - res.OutputContentType = strings.Split(metaField.Tag.Get(define.TagNameOutputContentType), ",") - return res + res.ContentType = strings.Split(metaField.Tag.Get(define.TagContentType), ",") + res.OutputContentType = strings.Split(metaField.Tag.Get(define.TagOutputContentType), ",") + res.Summary = ParseStructField.Summary(metaField) + if res.Method == "" { + return nil, errors.New("baseCfg.Method is empty") + } + if res.Uri == "" { + return nil, errors.New("baseCfg.Uri is empty") + } + return res, nil } diff --git a/struct_field.go b/struct_field.go index ea67dae..42f40d3 100644 --- a/struct_field.go +++ b/struct_field.go @@ -126,3 +126,18 @@ func (psf parseStructField) Deprecated(structField reflect.StructField) bool { } return false } + +// Summary 摘要信息 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:15 2025/2/14 +func (psf parseStructField) Summary(structField reflect.StructField) string { + defaultTagList := []string{define.TagSummary} + for _, tag := range defaultTagList { + if tagVal, exist := structField.Tag.Lookup(tag); exist && len(tagVal) > 0 { + return tagVal + } + } + return psf.GetParamName(structField) +}