完成基础反射调用接口 + 数据响应
This commit is contained in:
@ -8,6 +8,7 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
@ -38,25 +39,26 @@ func (c controller) Parse(inputController any) map[string]UriConfig {
|
||||
}
|
||||
controllerType := reflect.TypeOf(inputController)
|
||||
controllerValue := reflect.ValueOf(inputController)
|
||||
if controllerType.Kind() == reflect.Func {
|
||||
/*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() {
|
||||
/*if controllerValue.IsNil() {
|
||||
return parseRes
|
||||
}
|
||||
}*/
|
||||
for methodIdx := 0; methodIdx < controllerType.NumMethod(); methodIdx++ {
|
||||
uriCfg, needRegister := c.methodConfig(controllerType.Method(methodIdx).Type)
|
||||
uriCfg, needRegister := c.methodConfig(controllerType.Method(methodIdx))
|
||||
if !needRegister {
|
||||
continue
|
||||
}
|
||||
uriCfg.ApiStructValue = controllerValue
|
||||
parseRes[uriCfg.Path] = uriCfg
|
||||
}
|
||||
return parseRes
|
||||
@ -71,7 +73,8 @@ func (c controller) Parse(inputController any) map[string]UriConfig {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:05 2025/1/27
|
||||
func (c controller) methodConfig(methodType reflect.Type) (cfg UriConfig, needRegister bool) {
|
||||
func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, needRegister bool) {
|
||||
methodType := reflectMethod.Type
|
||||
// num0: 函数声明
|
||||
// num1: 第一个参数
|
||||
// num2: 第二个参数
|
||||
@ -89,6 +92,7 @@ func (c controller) methodConfig(methodType reflect.Type) (cfg UriConfig, needRe
|
||||
if formType.Kind() == reflect.Ptr {
|
||||
formType = methodType.In(2).Elem()
|
||||
}
|
||||
cfg.FormDataType = formType
|
||||
metaField, metaFieldExist := formType.FieldByName(FieldNameMeta)
|
||||
if !metaFieldExist {
|
||||
needRegister = false
|
||||
@ -96,34 +100,37 @@ func (c controller) methodConfig(methodType reflect.Type) (cfg UriConfig, needRe
|
||||
}
|
||||
|
||||
// 解析第一个返回值, 要求必须是结构体或者是map
|
||||
if methodType.Out(0).Kind() != reflect.Struct && methodType.Out(0).Kind() != reflect.Map {
|
||||
/*if methodType.Out(0).Kind() != reflect.Struct && methodType.Out(0).Kind() != reflect.Map {
|
||||
needRegister = false
|
||||
return
|
||||
}
|
||||
}*/
|
||||
if methodType.Out(1).Kind().String() != "error" {
|
||||
// 判断是否是实现 error接口的方法
|
||||
outputErrParse := false
|
||||
for j := 0; j < methodType.Out(1).NumMethod(); j++ {
|
||||
if methodType.Out(1).Method(j).Name == "Error" && // 实现Error方法
|
||||
methodType.Out(1).Method(j).Type.NumIn() == 1 && // 没有任何参数
|
||||
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() {
|
||||
outputErrParse = true
|
||||
break
|
||||
}
|
||||
fmt.Println(methodType.Out(1).Method(j).Name, methodType.Out(1).Method(j).Type.NumIn(), methodType.Out(1).Method(j).Type.NumOut(), methodType.Out(1).Method(j).Type.Out(0).Kind().String())
|
||||
}
|
||||
if !outputErrParse {
|
||||
needRegister = false
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
// 解析meta信息
|
||||
cfg.Path = metaField.Tag.Get(TagNamePath)
|
||||
cfg.Method = metaField.Tag.Get(TagNameMethod)
|
||||
cfg.RequestMethod = metaField.Tag.Get(TagNameMethod)
|
||||
cfg.Desc = metaField.Tag.Get(TagNameDesc)
|
||||
cfg.TagList = strings.Split(metaField.Tag.Get(TagNameUriTag), ",")
|
||||
// 解析参数配置
|
||||
cfg.ParamList = c.parseParamConfig(formType)
|
||||
cfg.ApiLogicFunc = reflectMethod
|
||||
needRegister = true
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user