feat: 优化注册方法检测
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/gin/logger"
|
||||
"git.zhangdeman.cn/zhangdeman/util"
|
||||
)
|
||||
|
||||
@@ -49,37 +50,59 @@ func (c controller) Parse(inputController any) map[string]UriConfig {
|
||||
return parseRes
|
||||
}
|
||||
|
||||
// methodConfig 解析方法配置, 要求函数格式, 两个参数, 两个返回值, 格式 : func(ctx *gin.Context, formData anyStruct[组合Meta]) (anyStruct|map[response], error)
|
||||
//
|
||||
// 参数 : 方法反射结果
|
||||
//
|
||||
// 返回值 : 第一个 -> 解析出的接口配置 第二个 -> 是否要注册为接口
|
||||
func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, needRegister bool) {
|
||||
// preCheckMethod 预检查方法是否可以注册为接口
|
||||
func (c controller) preCheckMethod(reflectMethod reflect.Method) (bool, reflect.Type, reflect.StructField) {
|
||||
var (
|
||||
metaField reflect.StructField
|
||||
metaFieldExist bool
|
||||
)
|
||||
|
||||
methodType := reflectMethod.Type
|
||||
// num0: 函数声明
|
||||
// num1: 第一个参数
|
||||
// num2: 第二个参数
|
||||
if methodType.NumIn() != 3 {
|
||||
needRegister = false
|
||||
return
|
||||
return false, nil, metaField
|
||||
}
|
||||
// 第一个参数必须是 *gin.Context 或者 *define.Context
|
||||
paramOne := methodType.In(1).String()
|
||||
if paramOne != GinContextType {
|
||||
needRegister = false
|
||||
return
|
||||
return false, nil, metaField
|
||||
}
|
||||
|
||||
// 解析第二个参数是组合 Meta 的form表单
|
||||
formType := methodType.In(2)
|
||||
cfg.FormDataType = formType
|
||||
if formType.Kind() == reflect.Ptr {
|
||||
formType = methodType.In(2).Elem()
|
||||
formType = formType.Elem()
|
||||
}
|
||||
metaField, metaFieldExist := formType.FieldByName(FieldNameMeta)
|
||||
if !metaFieldExist {
|
||||
needRegister = false
|
||||
return
|
||||
|
||||
if metaField, metaFieldExist = formType.FieldByName(FieldNameMeta); !metaFieldExist {
|
||||
return false, nil, metaField
|
||||
}
|
||||
return true, formType, metaField
|
||||
}
|
||||
|
||||
// methodConfig 解析方法配置, 要求函数格式, 两个参数, 两个返回值, 格式 : func(ctx *gin.Context, formData anyStruct[组合Meta]) (anyStruct|map[response], error)
|
||||
//
|
||||
// 参数 : 方法反射结果
|
||||
//
|
||||
// 返回值 : 第一个 -> 解析出的接口配置 第二个 -> 是否要注册为接口
|
||||
func (c controller) methodConfig(reflectMethod reflect.Method) (UriConfig, bool) {
|
||||
var (
|
||||
needRegister bool
|
||||
metaField reflect.StructField
|
||||
cfg UriConfig
|
||||
)
|
||||
|
||||
methodType := reflectMethod.Type
|
||||
// num0: 函数声明
|
||||
// num1: 第一个参数
|
||||
// num2: 第二个参数
|
||||
if needRegister, cfg.FormDataType, metaField = c.preCheckMethod(reflectMethod); !needRegister {
|
||||
logger.Instance.Info("接口方法不符合要求, 不注册为接口, 方法名: " + reflectMethod.Name + ", 方法签名: " + methodType.String())
|
||||
return UriConfig{}, false
|
||||
}
|
||||
|
||||
cfg.ResultDataType = methodType.Out(0)
|
||||
if methodType.Out(1).Kind().String() != ErrorType {
|
||||
// 判断是否是实现 error接口的方法
|
||||
@@ -95,7 +118,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
}
|
||||
if !outputErrParse {
|
||||
needRegister = false
|
||||
return
|
||||
return cfg, needRegister
|
||||
}
|
||||
}
|
||||
c.setUriMeta(metaField, &cfg)
|
||||
@@ -111,7 +134,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
//cfg.ParamList = c.parseParamConfig(formType)
|
||||
cfg.ApiLogicFunc = reflectMethod
|
||||
needRegister = true
|
||||
return
|
||||
return cfg, needRegister
|
||||
}
|
||||
|
||||
// setUriMeta 设置接口的 meta 信息
|
||||
|
||||
Reference in New Issue
Block a user