feat: 优化参数名称解析
This commit is contained in:
@@ -10,7 +10,6 @@ package openapi
|
|||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/api-doc/define"
|
"git.zhangdeman.cn/zhangdeman/api-doc/define"
|
||||||
@@ -48,30 +47,9 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo {
|
|||||||
Type: field.Type,
|
Type: field.Type,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
util.ParseStructFieldTag.GetParamName(field)
|
||||||
// 解析 JSON tag
|
// 解析 JSON tag
|
||||||
jsonTag := field.Tag.Get(define.TagJson)
|
info.JSONName, info.OmitEmpty = util.ParseStructFieldTag.GetParamName(field)
|
||||||
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.Description = util.ParseStructFieldTag.GetParamDesc(field)
|
info.Description = util.ParseStructFieldTag.GetParamDesc(field)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ type parseStructFieldTag struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetParamName 获取参数名称
|
// GetParamName 获取参数名称
|
||||||
func (psf parseStructFieldTag) GetParamName(structField reflect.StructField) string {
|
func (psf parseStructFieldTag) GetParamName(structField reflect.StructField) (string, bool) {
|
||||||
paramNameTagList := []string{
|
paramNameTagList := []string{
|
||||||
define.TagJson, define.TagForm,
|
define.TagJson, define.TagForm,
|
||||||
define.TagXml, define.TagYaml,
|
define.TagXml, define.TagYaml,
|
||||||
@@ -32,14 +32,30 @@ func (psf parseStructFieldTag) GetParamName(structField reflect.StructField) str
|
|||||||
}
|
}
|
||||||
for _, tag := range paramNameTagList {
|
for _, tag := range paramNameTagList {
|
||||||
tagVal := structField.Tag.Get(tag)
|
tagVal := structField.Tag.Get(tag)
|
||||||
tagVal = strings.TrimSuffix(strings.TrimPrefix(tagVal, define.TagNameOmitempty+","), ","+define.TagNameOmitempty)
|
if tagVal == "" {
|
||||||
tagVal = strings.Trim(tagVal, ",")
|
continue
|
||||||
if tagVal != "" && tagVal != define.TagNameOmitempty {
|
|
||||||
return tagVal
|
|
||||||
}
|
}
|
||||||
|
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 ...
|
// GetParamDesc ...
|
||||||
@@ -52,7 +68,8 @@ func (psf parseStructFieldTag) GetParamDesc(structField reflect.StructField) str
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 没有显示的设置参数描述, 则使用参数名作为参数描述
|
// 没有显示的设置参数描述, 则使用参数名作为参数描述
|
||||||
return psf.GetParamName(structField)
|
paramName, _ := psf.GetParamName(structField)
|
||||||
|
return paramName
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDefaultValue 获取默认值
|
// GetDefaultValue 获取默认值
|
||||||
@@ -122,10 +139,7 @@ func (psf parseStructFieldTag) Summary(structField reflect.StructField) string {
|
|||||||
return tagVal
|
return tagVal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
paramName := psf.GetParamName(structField)
|
paramName, _ := psf.GetParamName(structField)
|
||||||
if paramName == "-" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return paramName
|
return paramName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user