feat: 优化对接口元数据 Meta 的解析
This commit is contained in:
@@ -52,10 +52,6 @@ func (c controller) Parse(inputController any) map[string]UriConfig {
|
||||
// 参数 : 方法反射结果
|
||||
//
|
||||
// 返回值 : 第一个 -> 解析出的接口配置 第二个 -> 是否要注册为接口
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:05 2025/1/27
|
||||
func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, needRegister bool) {
|
||||
methodType := reflectMethod.Type
|
||||
// num0: 函数声明
|
||||
@@ -71,7 +67,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
needRegister = false
|
||||
return
|
||||
}
|
||||
// 解析第二个参数是组合Meta的form表单
|
||||
// 解析第二个参数是组合 Meta 的form表单
|
||||
formType := methodType.In(2)
|
||||
cfg.FormDataType = formType
|
||||
if formType.Kind() == reflect.Ptr {
|
||||
@@ -89,7 +85,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
// 判断是否是实现 error接口的方法
|
||||
outputErrParse := false
|
||||
for j := 0; j < methodType.Out(1).NumMethod(); j++ {
|
||||
if methodType.Out(1).Method(j).Name == ErrorInterfaceFuncName && // 实现Error方法
|
||||
if methodType.Out(1).Method(j).Name == ErrorInterfaceFuncName && // 实现 Error 方法
|
||||
methodType.Out(1).Method(j).Type.NumIn() == 0 && // 没有任何参数
|
||||
methodType.Out(1).Method(j).Type.NumOut() == 1 && // 一个返回值
|
||||
methodType.Out(1).Method(j).Type.Out(0).Kind().String() == reflect.String.String() {
|
||||
@@ -102,16 +98,8 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
return
|
||||
}
|
||||
}
|
||||
// 解析meta信息
|
||||
cfg.Path = metaField.Tag.Get(TagNamePath)
|
||||
cfg.RequestMethod = metaField.Tag.Get(TagNameMethod)
|
||||
cfg.Desc = metaField.Tag.Get(TagNameDesc)
|
||||
cfg.TagList = strings.Split(metaField.Tag.Get(TagNameUriTag), ",")
|
||||
// 解析第一个返回值, 要求必须是结构体或者是map
|
||||
outputStrictModel := metaField.Tag.Get(TagNameOutputStrict)
|
||||
hookSync := strings.ToLower(metaField.Tag.Get(TagNameHookSync))
|
||||
cfg.HookSync = hookSync == "1" || hookSync == "true" // 同步执行判断
|
||||
cfg.OutputStrict = outputStrictModel == "1" || outputStrictModel == "true"
|
||||
c.setUriMeta(metaField, &cfg)
|
||||
|
||||
if cfg.OutputStrict {
|
||||
// 开启输出严格模式校验
|
||||
if methodType.Out(0).Kind() != reflect.Struct && methodType.Out(0).Kind() != reflect.Map {
|
||||
@@ -119,6 +107,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 解析参数配置
|
||||
//cfg.ParamList = c.parseParamConfig(formType)
|
||||
cfg.ApiLogicFunc = reflectMethod
|
||||
@@ -126,24 +115,21 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
return
|
||||
}
|
||||
|
||||
// parseParamConfig 解析参数配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:35 2025/2/7
|
||||
func (c controller) parseParamConfig(formDataType reflect.Type) []UriParam {
|
||||
res := make([]UriParam, 0)
|
||||
for i := 0; i < formDataType.NumField(); i++ {
|
||||
structField := formDataType.Field(i)
|
||||
if structField.Name == FieldNameMeta {
|
||||
// Meta 字段, 忽略
|
||||
continue
|
||||
}
|
||||
jsonTag := structField.Tag.Get("json")
|
||||
if jsonTag == "" {
|
||||
jsonTag = structField.Name
|
||||
}
|
||||
|
||||
// setUriMeta 设置接口的 meta 信息
|
||||
func (c controller) setUriMeta(metaField reflect.StructField, cfg *UriConfig) {
|
||||
// 解析 meta 信息
|
||||
cfg.Path = metaField.Tag.Get(TagNamePath) // 接口路由
|
||||
cfg.RequestMethod = strings.Split(strings.ToUpper(metaField.Tag.Get(TagNameMethod)), ",") // 请求方法
|
||||
cfg.Desc = metaField.Tag.Get(TagNameDesc) // 接口描述
|
||||
cfg.TagList = strings.Split(metaField.Tag.Get(TagNameUriTag), ",") // 接口标签
|
||||
// 以下是bool类型的配置解析
|
||||
var boolMetaParse = func(tagName string) bool {
|
||||
val := strings.ToLower(metaField.Tag.Get(tagName))
|
||||
return val == "1" || val == "true"
|
||||
}
|
||||
return res
|
||||
cfg.OutputStrict = boolMetaParse(TagNameOutputStrict) // 配置接口数据是否严格要求是对象
|
||||
cfg.HookSync = boolMetaParse(TagNameHookSync) // 同步执行判断
|
||||
cfg.IsWebsocket = boolMetaParse(TagNameIsWebsocket) // 是否是 websocket 接口
|
||||
cfg.IsSse = boolMetaParse(TagNameIsSse) // 是否是 sse 接口
|
||||
cfg.NoLogin = boolMetaParse(TagNameNoLogin) // 是否需要登录
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user