优化代码
This commit is contained in:
73
struct_field.go
Normal file
73
struct_field.go
Normal file
@ -0,0 +1,73 @@
|
||||
// Package api_doc ...
|
||||
//
|
||||
// Description : api_doc ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-02-12 22:15
|
||||
package api_doc
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/gateway/api-doc/define"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var (
|
||||
ParseStructField = parseStructField{}
|
||||
)
|
||||
|
||||
type parseStructField struct {
|
||||
}
|
||||
|
||||
// GetParamName 获取参数名称
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:58 2025/2/11
|
||||
func (psf parseStructField) 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 (psf parseStructField) 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 psf.GetParamName(structField)
|
||||
}
|
||||
|
||||
// GetDefaultValue 获取默认值
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 22:05 2025/2/11
|
||||
func (psf parseStructField) 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 ""
|
||||
}
|
Reference in New Issue
Block a user