diff --git a/rfx_example_test.go b/rfx_example_test.go index 04275b5..4d33541 100644 --- a/rfx_example_test.go +++ b/rfx_example_test.go @@ -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 } diff --git a/util.go b/util.go index af54cd3..1fdb6be 100644 --- a/util.go +++ b/util.go @@ -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 {