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

View File

@@ -71,6 +71,8 @@ func ExampleNew_withValue() {
// 传入值 - 会创建深度克隆,修改不会影响原始数据
rfx := New(person)
fmt.Printf("After New: %+v\n", rfx.Any())
rfx.Set("Name", "Bob")
rfx.Set("Age", 35)
@@ -83,6 +85,7 @@ func ExampleNew_withValue() {
rfx.Get("Age").Int())
// Output:
// After New: {Name:Alice Age:30}
// Original - Name: Alice, Age: 30
// Clone - Name: Bob, Age: 35
}
@@ -97,6 +100,8 @@ func ExampleNew_withValue_map() {
// 传入 map 值 - 会创建深度克隆
rfx := New(config)
fmt.Printf("After New: %v\n", rfx.Any())
rfx.Set("host", "127.0.0.1")
rfx.Set("port", 9090)
@@ -109,6 +114,7 @@ func ExampleNew_withValue_map() {
rfx.Get("port").Int())
// Output:
// After New: map[host:localhost port:8080]
// Original - Host: localhost, Port: 8080
// Clone - Host: 127.0.0.1, Port: 9090
}
@@ -120,6 +126,8 @@ func ExampleNew_withValue_slice() {
// 传入 slice 值 - 会创建深度克隆
rfx := New(items)
fmt.Printf("After New: %v\n", rfx.Any())
rfx.Set("0", "orange")
rfx.Set("2", "grape")
@@ -133,6 +141,7 @@ func ExampleNew_withValue_slice() {
rfx.Get("2").String())
// Output:
// After New: [apple banana cherry]
// Original: [apple banana cherry]
// Clone: [orange banana grape]
}
@@ -158,6 +167,8 @@ func ExampleNew_withValue_nestedStruct() {
// 传入值 - 会创建深度克隆,包括嵌套的结构体
rfx := New(person)
fmt.Printf("After New: %+v\n", rfx.Any())
rfx.Set("Name", "Bob")
rfx.Set("Address.City", "Shanghai")
@@ -170,6 +181,7 @@ func ExampleNew_withValue_nestedStruct() {
rfx.Get("Address", "City").String())
// Output:
// After New: {Name:Alice Address:{City:Beijing Street:Main St}}
// Original - Name: Alice, City: Beijing
// Clone - Name: Bob, City: Shanghai
}