接口meta信息解析
This commit is contained in:
@ -10,11 +10,15 @@ package router
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var (
|
||||
Debug = false // 是否开启DEBUG
|
||||
ginRouter = gin.Default()
|
||||
)
|
||||
|
||||
@ -72,6 +76,68 @@ func parseController(controller any) {
|
||||
}
|
||||
fmt.Println(111, middlewareList)
|
||||
}
|
||||
|
||||
fmt.Println(routerPrefix)
|
||||
for funcIdx := 0; funcIdx < controllerType.NumMethod(); funcIdx++ {
|
||||
method := controllerType.Method(funcIdx)
|
||||
// methodValue := controllerValue.Method(funcIdx)
|
||||
if method.Name == PrefixFuncName || method.Name == MiddlewareFuncName {
|
||||
continue
|
||||
}
|
||||
methodType := method.Type
|
||||
uriConfig, err := parseUriConfig(methodType, routerPrefix)
|
||||
if nil != err {
|
||||
debugLog("parseUriConfig error : %s -> %s", err.Error(), methodType.Kind().String())
|
||||
}
|
||||
if nil == uriConfig {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// parseUriConfig 解析Uri配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:40 2024/7/21
|
||||
func parseUriConfig(methodType reflect.Type, routerPrefix string) (*UriConfig, error) {
|
||||
if methodType.NumIn() != 3 || // 结构体指针 + 两个参数
|
||||
methodType.NumOut() != 2 { // 两个返回值
|
||||
return nil, nil
|
||||
}
|
||||
// 接口logic共计两个参数. 两个返回值, 格式 : func(ctx *gin.Context, formData any[组合Meta]) (any[response], error)
|
||||
|
||||
// 解析第一个参数是 *gin.Context
|
||||
if methodType.In(1).String() != "*gin.Context" {
|
||||
return nil, nil
|
||||
}
|
||||
// 解析第二个参数是组合Meta的form表单
|
||||
formType := methodType.In(2)
|
||||
if formType.Kind() == reflect.Ptr {
|
||||
formType = methodType.In(2).Elem()
|
||||
}
|
||||
metaField, metaFieldExist := formType.FieldByName("Meta")
|
||||
if !metaFieldExist {
|
||||
return nil, nil
|
||||
}
|
||||
uriConfig := &UriConfig{
|
||||
Path: strings.TrimRight(routerPrefix, "/") + "/" + strings.TrimLeft(metaField.Tag.Get(TagNamePath), "/"),
|
||||
Method: strings.ToUpper(metaField.Tag.Get(TagNameMethod)),
|
||||
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,
|
||||
}
|
||||
// 解析第一个返回值
|
||||
// 解析第二个返回值
|
||||
return uriConfig, nil
|
||||
}
|
||||
|
||||
// debugLog ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:32 2024/7/21
|
||||
func debugLog(format string, valList ...any) {
|
||||
if !Debug {
|
||||
return
|
||||
}
|
||||
fmt.Printf("[DEBUG] "+format+"\n", valList...)
|
||||
}
|
||||
|
Reference in New Issue
Block a user