完善数据类型转换
This commit is contained in:
+23
-4
@@ -28,10 +28,14 @@ func (dm *defaultFieldMapper) GetFieldMap(t reflect.Type) map[string]FieldInfo {
|
||||
}
|
||||
cacheMutex.RUnlock()
|
||||
|
||||
cacheMutex.Lock()
|
||||
// 双重检查:避免两个goroutine同时构建
|
||||
if cached, exists := typeInfoCache[t]; exists {
|
||||
cacheMutex.Unlock()
|
||||
return cached
|
||||
}
|
||||
fieldMap := make(map[string]FieldInfo)
|
||||
dm.buildFieldMapRecursive(t, []int{}, fieldMap, "")
|
||||
|
||||
cacheMutex.Lock()
|
||||
typeInfoCache[t] = fieldMap
|
||||
cacheMutex.Unlock()
|
||||
|
||||
@@ -40,6 +44,16 @@ func (dm *defaultFieldMapper) GetFieldMap(t reflect.Type) map[string]FieldInfo {
|
||||
|
||||
// 递归构建字段映射表
|
||||
func (dm *defaultFieldMapper) buildFieldMapRecursive(t reflect.Type, index []int, fieldMap map[string]FieldInfo, prefix string) {
|
||||
dm.buildFieldMapRecursiveWithVisited(t, index, fieldMap, prefix, make(map[reflect.Type]bool))
|
||||
}
|
||||
|
||||
func (dm *defaultFieldMapper) buildFieldMapRecursiveWithVisited(t reflect.Type, index []int, fieldMap map[string]FieldInfo, prefix string, stack map[reflect.Type]bool) {
|
||||
if stack[t] {
|
||||
return
|
||||
}
|
||||
stack[t] = true
|
||||
defer delete(stack, t)
|
||||
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
if !field.IsExported() {
|
||||
@@ -58,10 +72,15 @@ func (dm *defaultFieldMapper) buildFieldMapRecursive(t reflect.Type, index []int
|
||||
isPtr := fieldType.Kind() == reflect.Ptr
|
||||
actualType := fieldType
|
||||
if isPtr {
|
||||
// 解引用
|
||||
actualType = fieldType.Elem()
|
||||
}
|
||||
|
||||
// 嵌入式结构体字段提升——其字段注册到父级别
|
||||
if field.Anonymous && actualType.Kind() == reflect.Struct && !isBasicStructType(actualType) {
|
||||
dm.buildFieldMapRecursiveWithVisited(actualType, currentIndex, fieldMap, prefix, stack)
|
||||
continue
|
||||
}
|
||||
|
||||
if actualType.Kind() == reflect.Slice || actualType.Kind() == reflect.Array {
|
||||
fieldMap[fullKey] = FieldInfo{
|
||||
Index: currentIndex,
|
||||
@@ -75,7 +94,7 @@ func (dm *defaultFieldMapper) buildFieldMapRecursive(t reflect.Type, index []int
|
||||
}
|
||||
|
||||
if actualType.Kind() == reflect.Struct && !isBasicStructType(actualType) {
|
||||
dm.buildFieldMapRecursive(actualType, currentIndex, fieldMap, fullKey)
|
||||
dm.buildFieldMapRecursiveWithVisited(actualType, currentIndex, fieldMap, fullKey, stack)
|
||||
}
|
||||
|
||||
fieldMap[fullKey] = FieldInfo{
|
||||
|
||||
Reference in New Issue
Block a user