perf(scan): 使用缓存减少对象扫描
抽取函数,优化变量命名 支持了传入数组类型
This commit is contained in:
58
reflect_utils.go
Normal file
58
reflect_utils.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package tdmap
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// extractReflectInfo 提取反射信息(自动解引用到基础类型)
|
||||
func extractReflectInfo(data any) (reflect.Type, reflect.Value) {
|
||||
t := reflect.TypeOf(data)
|
||||
v := reflect.ValueOf(data)
|
||||
|
||||
// 处理结构体和结构体指针
|
||||
for t.Kind() == reflect.Ptr {
|
||||
t = t.Elem()
|
||||
v = v.Elem()
|
||||
}
|
||||
return t, v
|
||||
}
|
||||
|
||||
// buildTypeKey 构建类型键(包路径.类型名)
|
||||
func buildTypeKey(t reflect.Type) string {
|
||||
return t.PkgPath() + "." + t.Name()
|
||||
}
|
||||
|
||||
// tryGetSuperTableName 调用对象的 SuperTableName 方法(如果存在)
|
||||
func tryGetSuperTableName(obj any) (string, bool) {
|
||||
ptr := ensurePtr(obj)
|
||||
if it, ok := ptr.(interface{ SuperTableName() string }); ok {
|
||||
return it.SuperTableName(), true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// ensurePtr 确保返回的是指针类型(不是指针则转换为指针)
|
||||
func ensurePtr(obj any) any {
|
||||
if v := reflect.ValueOf(obj); v.Kind() != reflect.Ptr {
|
||||
// 创建对应类型的指针
|
||||
ptr := reflect.New(v.Type())
|
||||
// 设置指针指向的值为元素本身
|
||||
ptr.Elem().Set(v)
|
||||
// 返回 指针
|
||||
return ptr.Interface()
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
// isNil 判断值是否为 nil(包括指针类型)
|
||||
func isNil(d any) bool {
|
||||
if d == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
// 反射判断指针类型是否为nil
|
||||
if v := reflect.ValueOf(d); v.Kind() == reflect.Ptr {
|
||||
return v.IsNil()
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user