反射支持 controller 指针与普通实例; 函数调用支持指针参数与非指针参数
This commit is contained in:
parent
74f0ae2069
commit
293f829945
@ -37,21 +37,9 @@ func (c controller) Parse(inputController any) map[string]UriConfig {
|
|||||||
return parseRes
|
return parseRes
|
||||||
}
|
}
|
||||||
controllerType := reflect.TypeOf(inputController)
|
controllerType := reflect.TypeOf(inputController)
|
||||||
|
inputController = reflect.New(controllerType).Interface()
|
||||||
|
controllerType = reflect.TypeOf(inputController)
|
||||||
controllerValue := reflect.ValueOf(inputController)
|
controllerValue := reflect.ValueOf(inputController)
|
||||||
/*if controllerType.Kind() == reflect.Func {
|
|
||||||
// 直接函数注册
|
|
||||||
cfg, isNeedRegister := c.methodConfig(controllerType)
|
|
||||||
if isNeedRegister {
|
|
||||||
parseRes[cfg.Path] = cfg
|
|
||||||
}
|
|
||||||
return parseRes
|
|
||||||
}*/
|
|
||||||
if controllerType.Kind() == reflect.Ptr {
|
|
||||||
controllerValue = controllerValue.Elem()
|
|
||||||
}
|
|
||||||
/*if controllerValue.IsNil() {
|
|
||||||
return parseRes
|
|
||||||
}*/
|
|
||||||
for methodIdx := 0; methodIdx < controllerType.NumMethod(); methodIdx++ {
|
for methodIdx := 0; methodIdx < controllerType.NumMethod(); methodIdx++ {
|
||||||
uriCfg, needRegister := c.methodConfig(controllerType.Method(methodIdx))
|
uriCfg, needRegister := c.methodConfig(controllerType.Method(methodIdx))
|
||||||
if !needRegister {
|
if !needRegister {
|
||||||
@ -88,10 +76,10 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
|||||||
}
|
}
|
||||||
// 解析第二个参数是组合Meta的form表单
|
// 解析第二个参数是组合Meta的form表单
|
||||||
formType := methodType.In(2)
|
formType := methodType.In(2)
|
||||||
|
cfg.FormDataType = formType
|
||||||
if formType.Kind() == reflect.Ptr {
|
if formType.Kind() == reflect.Ptr {
|
||||||
formType = methodType.In(2).Elem()
|
formType = methodType.In(2).Elem()
|
||||||
}
|
}
|
||||||
cfg.FormDataType = formType
|
|
||||||
metaField, metaFieldExist := formType.FieldByName(FieldNameMeta)
|
metaField, metaFieldExist := formType.FieldByName(FieldNameMeta)
|
||||||
if !metaFieldExist {
|
if !metaFieldExist {
|
||||||
needRegister = false
|
needRegister = false
|
||||||
|
@ -28,17 +28,27 @@ func RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
|
|||||||
e exception.IException
|
e exception.IException
|
||||||
)
|
)
|
||||||
|
|
||||||
formData := reflect.New(uriCfg.FormDataType).Interface()
|
var formParam reflect.Value
|
||||||
|
if uriCfg.FormDataType.Kind() == reflect.Ptr {
|
||||||
|
formParam = reflect.New(uriCfg.FormDataType.Elem())
|
||||||
|
} else {
|
||||||
// 表单解析
|
// 表单解析
|
||||||
if err = request.Form.Parse(ctx, formData); nil != err {
|
formParam = reflect.New(uriCfg.FormDataType)
|
||||||
|
}
|
||||||
|
formValue := formParam.Interface()
|
||||||
|
if err = request.Form.Parse(ctx, &formParam); nil != err {
|
||||||
// 格式化验证错误的信息
|
// 格式化验证错误的信息
|
||||||
err = GetValidateErr(formData, err)
|
err = GetValidateErr(formValue, err)
|
||||||
e = exception.NewFromError(400, err)
|
e = exception.NewFromError(400, err)
|
||||||
response.SendWithException(ctx, e, nil)
|
response.SendWithException(ctx, e, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行逻辑
|
// 执行逻辑
|
||||||
resList := uriCfg.ApiLogicFunc.Func.Call([]reflect.Value{uriCfg.ApiStructValue, reflect.ValueOf(ctx), reflect.ValueOf(formData)})
|
if uriCfg.FormDataType.Kind() != reflect.Ptr {
|
||||||
|
formParam = formParam.Elem()
|
||||||
|
}
|
||||||
|
resList := uriCfg.ApiLogicFunc.Func.Call([]reflect.Value{uriCfg.ApiStructValue, reflect.ValueOf(ctx), formParam})
|
||||||
if resList[1].IsNil() {
|
if resList[1].IsNil() {
|
||||||
// 请求成功
|
// 请求成功
|
||||||
response.Success(ctx, resList[0].Interface())
|
response.Success(ctx, resList[0].Interface())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user