增加get类请求 body 解析

This commit is contained in:
白茶清欢 2025-02-14 15:31:53 +08:00
parent 8d31a7f2ce
commit f51388c1d6
3 changed files with 77 additions and 62 deletions

View File

@ -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"
)

View File

@ -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
}

View File

@ -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)
}