fix: 修复深度克隆时 map/slice 中基本类型值被错误存储为指针的问题

- 提取 cloneSequence 函数统一处理 slice/array 克隆
- 提取 cloneElement 函数处理单个元素克隆
- 提取 needsDeepClone 函数判断是否需要深度克隆
- 减少代码重复,提高可维护性

为 ExampleNew_withValue* 系列测试添加 New() 后的输出,
清晰展示深度克隆后的初始状态和值类型正确性。
This commit is contained in:
2025-12-03 10:19:44 +08:00
parent 8fbc859e74
commit f9e7f8e781
2 changed files with 57 additions and 10 deletions

55
util.go
View File

@@ -61,14 +61,10 @@ func CloneValue(dst, src reflect.Value) {
return
}
dst.Set(reflect.MakeSlice(src.Type(), src.Len(), src.Cap()))
for i := 0; i < src.Len(); i++ {
CloneValue(dst.Index(i), src.Index(i))
}
cloneSequence(dst, src)
case reflect.Array:
for i := 0; i < src.Len(); i++ {
CloneValue(dst.Index(i), src.Index(i))
}
cloneSequence(dst, src)
case reflect.Map:
if src.IsNil() {
@@ -77,10 +73,8 @@ func CloneValue(dst, src reflect.Value) {
dst.Set(reflect.MakeMap(src.Type()))
for _, key := range src.MapKeys() {
val := src.MapIndex(key)
// Map 的值需要特殊处理
newVal := reflect.New(val.Type()).Elem()
CloneValue(newVal, val)
dst.SetMapIndex(key, newVal)
clonedVal := cloneElement(val)
dst.SetMapIndex(key, clonedVal)
}
default:
@@ -91,6 +85,47 @@ func CloneValue(dst, src reflect.Value) {
}
}
// cloneSequence 克隆序列类型(slice/array)的元素
func cloneSequence(dst, src reflect.Value) {
for i := 0; i < src.Len(); i++ {
elem := src.Index(i)
clonedElem := cloneElement(elem)
dst.Index(i).Set(clonedElem)
}
}
// cloneElement 克隆单个元素,处理 interface 包装的情况
// 对于复杂类型(struct, map, slice, array)进行深度克隆
// 对于基本类型,直接返回原始值
func cloneElement(elem reflect.Value) reflect.Value {
// 解引用以获取实际值
actualElem := elem
for actualElem.Kind() == reflect.Interface || actualElem.Kind() == reflect.Ptr {
if actualElem.IsNil() {
return elem
}
actualElem = actualElem.Elem()
}
// 如果元素需要递归克隆(struct, map, slice, array)
if actualElem.IsValid() && needsDeepClone(actualElem.Kind()) {
clonedElem := reflect.New(actualElem.Type()).Elem()
CloneValue(clonedElem, actualElem)
return clonedElem
}
// 对于基本类型,直接返回原始值
return elem
}
// needsDeepClone 判断某个类型是否需要深度克隆
func needsDeepClone(kind reflect.Kind) bool {
return kind == reflect.Struct ||
kind == reflect.Map ||
kind == reflect.Slice ||
kind == reflect.Array
}
// expandPath 展开路径,支持点号分割
// 例如: expandPath("a.b", "c") -> []string{"a", "b", "c"}
func expandPath(p ...string) []string {