feat: 优化路由注册逻辑

This commit is contained in:
2025-12-30 12:20:18 +08:00
parent 10c96af782
commit 6201961b61
2 changed files with 44 additions and 35 deletions

View File

@@ -16,7 +16,7 @@ import (
)
// controller 解析controller有哪些方法要注册为接口
type controller struct {
type controllerParser struct {
}
// Parse 执行解析
@@ -26,11 +26,7 @@ type controller struct {
// 1. 函数接受两个入参, 第一个参数为 gin.Context , 第二个参数为 任意结构体指针, 但是必须声明 Meta 相关信息, 否则会报错
//
// 2. 函数有两个返回值, 第一个返回值为任意结构体/结构体指针(限制死不能为map/slice, 方便后续统一标准化) , 第二个返回值为 error , 代表处理的异常, 会自动适配 exception.IException 类型
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:31 2025/1/27
func (c controller) Parse(inputController any) map[string]UriConfig {
func (c controllerParser) Parse(inputController any) map[string]UriConfig {
parseRes := make(map[string]UriConfig)
if nil == inputController {
return parseRes
@@ -51,7 +47,7 @@ func (c controller) Parse(inputController any) map[string]UriConfig {
}
// preCheckMethod 预检查方法是否可以注册为接口
func (c controller) preCheckMethod(reflectMethod reflect.Method) (bool, reflect.Type, reflect.StructField) {
func (c controllerParser) preCheckMethod(reflectMethod reflect.Method) (bool, reflect.Type, reflect.StructField) {
var (
metaField reflect.StructField
metaFieldExist bool
@@ -87,7 +83,7 @@ func (c controller) preCheckMethod(reflectMethod reflect.Method) (bool, reflect.
// 参数 : 方法反射结果
//
// 返回值 : 第一个 -> 解析出的接口配置 第二个 -> 是否要注册为接口
func (c controller) methodConfig(reflectMethod reflect.Method) (UriConfig, bool) {
func (c controllerParser) methodConfig(reflectMethod reflect.Method) (UriConfig, bool) {
var (
needRegister bool
metaField reflect.StructField
@@ -138,7 +134,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (UriConfig, bool)
}
// setUriMeta 设置接口的 meta 信息
func (c controller) setUriMeta(metaField reflect.StructField, cfg *UriConfig) {
func (c controllerParser) setUriMeta(metaField reflect.StructField, cfg *UriConfig) {
// 解析 meta 信息
cfg.Path = metaField.Tag.Get(TagNamePath) // 接口路由
cfg.RequestMethod = strings.Split(strings.ToUpper(metaField.Tag.Get(TagNameMethod)), ",") // 请求方法