完成基础路由注册, 遗留 : form表单, any数据类型解析失败

This commit is contained in:
白茶清欢 2024-07-21 18:49:44 +08:00
parent 455f74ad89
commit f8f63691b7
3 changed files with 86 additions and 17 deletions

View File

@ -7,6 +7,8 @@
// Date : 2024-07-20 22:57
package router
import "reflect"
const (
PrefixFuncName = "RouterPrefix" // 路由前缀函数名称
MiddlewareFuncName = "RouterMiddleware" // 路由中间件函数名称
@ -31,4 +33,5 @@ type UriConfig struct {
TagList []string `json:"tag_list"` // 接口分组
Desc string `json:"desc"` // 接口描述
Strict bool `json:"strict"` // 接口是否为严格模式 : 不配置, 则为严格模式.严格模式 : POST 仅解析 BODY , GET 仅解析 QUERY
FormDataType reflect.Type `json:"-"` // 表单数据类型
}

View File

@ -9,9 +9,15 @@ package router
import (
"fmt"
"net/http"
"reflect"
"strings"
"git.zhangdeman.cn/zhangdeman/exception"
"git.zhangdeman.cn/zhangdeman/gin/response"
"git.zhangdeman.cn/zhangdeman/gin/request"
"git.zhangdeman.cn/zhangdeman/wrapper"
"github.com/gin-gonic/gin"
@ -37,7 +43,9 @@ func Register(port int, controllerList ...any) {
// 忽略空指针
continue
}
parseController(controller)
}
ginRouter.Run(fmt.Sprintf(":%d", port))
}
// parseController 解析controller
@ -74,11 +82,9 @@ func parseController(controller any) {
middlewareList = res[0].Interface().([]gin.HandlerFunc)
}
}
fmt.Println(111, middlewareList)
}
for funcIdx := 0; funcIdx < controllerType.NumMethod(); funcIdx++ {
method := controllerType.Method(funcIdx)
// methodValue := controllerValue.Method(funcIdx)
if method.Name == PrefixFuncName || method.Name == MiddlewareFuncName {
continue
}
@ -90,6 +96,7 @@ func parseController(controller any) {
if nil == uriConfig {
continue
}
registerUri(uriConfig, controllerValue.Method(funcIdx), middlewareList)
}
}
@ -124,12 +131,69 @@ func parseUriConfig(methodType reflect.Type, routerPrefix string) (*UriConfig, e
TagList: strings.Split(metaField.Tag.Get(TagNameUriTag), "|"),
Desc: metaField.Tag.Get(TagNameDesc),
Strict: wrapper.ArrayType([]string{"", "true"}).Has(strings.ToLower(metaField.Tag.Get(TagNameStrict))) >= 0,
FormDataType: methodType.In(2).Elem(),
}
// 解析第一个返回值
// 解析第二个返回值
return uriConfig, nil
}
// registerUri 注册路由
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:00 2024/7/21
func registerUri(uriConfig *UriConfig, methodValue reflect.Value, middlewareList []gin.HandlerFunc) {
if nil == middlewareList {
middlewareList = make([]gin.HandlerFunc, 0)
}
handlerFunc := func(ctx *gin.Context) {
formDataReceiver := reflect.New(uriConfig.FormDataType)
if err := request.Form.Parse(ctx, formDataReceiver.Interface()); nil != err {
panic(err)
}
returnValue := methodValue.Call([]reflect.Value{reflect.ValueOf(ctx), formDataReceiver})
businessData := returnValue[0].Interface()
errData := returnValue[1]
if errData.IsNil() {
response.Success(ctx, businessData)
return
}
err := errData.Interface()
if e, ok := err.(exception.IException); ok {
response.SendWithException(ctx, e, businessData)
return
} else {
response.SendWithException(ctx, exception.NewFromError(-1, errData.Interface().(error)), businessData)
return
}
}
middlewareList = append(middlewareList, handlerFunc)
switch uriConfig.Method {
case http.MethodGet:
ginRouter.GET(uriConfig.Path, middlewareList...)
case http.MethodHead:
ginRouter.HEAD(uriConfig.Path, middlewareList...)
case http.MethodPost:
ginRouter.PUT(uriConfig.Path, middlewareList...)
case http.MethodPut:
ginRouter.PUT(uriConfig.Path, middlewareList...)
case http.MethodPatch:
ginRouter.PATCH(uriConfig.Path, middlewareList...)
case http.MethodDelete:
ginRouter.DELETE(uriConfig.Path, middlewareList...)
case http.MethodConnect:
ginRouter.Handle(http.MethodConnect, uriConfig.Path, middlewareList...)
case http.MethodOptions:
ginRouter.OPTIONS(uriConfig.Path, middlewareList...)
case http.MethodTrace:
ginRouter.Handle(http.MethodTrace, uriConfig.Path, middlewareList...)
case "ANY":
ginRouter.Any(uriConfig.Path, middlewareList...)
default:
panic(uriConfig.Path + " : " + uriConfig.Method + " is not support")
}
}
// debugLog ...
//
// Author : go_developer@163.com<白茶清欢>

View File

@ -27,17 +27,19 @@ func (t *TestController) RouterMiddleware() []gin.HandlerFunc {
}
}
func (t *TestController) Uri(ctx *gin.Context, formData *TestForm) (any, error) {
return nil, nil
return formData, nil
}
type TestForm struct {
Meta `tag:"测试表单" path:"/a/b/c/d" desc:"测试接口" method:"get" strict:"true"`
Age int `json:"age" form:"age"`
Name string `json:"name" form:"name"`
Test any `json:"test" form:"test"`
}
func Test_parseController(t *testing.T) {
type args struct {
controller any
}
parseController(TestController{})
parseController(&TestController{})
Register(8080, &TestController{})
}