feat: 优化参数名称解析
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user