Files
tdengine-mapper-go/reflect_utils.go
zhoujie b9413d6c40 perf(scan): 使用缓存减少对象扫描
抽取函数,优化变量命名
支持了传入数组类型
2025-11-27 18:24:36 +08:00

59 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}