59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
|
|
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
|
|||
|
|
}
|