Comments (4)
Here is a live demo on the Go playground with a simplified test case: type [1][]int
.
from deepcopy.
`
case reflect.Array:
nv := reflect.New(original.Type())
arrCopyValue := nv.Elem()
num := original.Len()
for i := 0; i < num; i++ {
itemValue := original.Index(i)
copyValue := reflect.New(itemValue.Type()).Elem()
copyRecursive(original.Index(i), copyValue)
arrCopyValue.Index(i).Set(copyValue)
}
cpy.Set(arrCopyValue)
`
from deepcopy.
The author treats the array behavior wrongly, which relies on its deep copy entirely. Any reference type array will be affected by this case. Here are more test cases.
@HarutakaMatsumoto I will try to create a fix recently.
func TestArrayOfSomething(t *testing.T) {
verifyCases := map[string]struct {
modifyAndVerify func(t *testing.T)
}{
// failed because the map(underlying is a pointer) will be copied directly
"array of map": {
modifyAndVerify: func(t *testing.T) {
origin := [1]map[string]int{
{
"1": 1,
},
}
copied := deepcopy.Copy(&origin)
assert.NotSame(t, origin, copied)
assert.NotSame(t, origin[0], copied.(*[1]map[string]int)[0])
origin[0]["1"] = 999
assert.Equal(t, 1, copied.(*[1]map[string]int)[0]["1"])
},
},
// failed because the pointer of map will be copied directly
"array of *map": {
modifyAndVerify: func(t *testing.T) {
origin := [1]*map[string]int{
{
"1": 1,
},
}
copied := deepcopy.Copy(&origin)
assert.NotSame(t, origin, copied)
assert.NotSame(t, origin[0], copied.(*[1]*map[string]int)[0])
(*origin[0])["1"] = 999
assert.Equal(t, 1, (*copied.(*[1]*map[string]int)[0])["1"])
},
},
// failed because the pointer of int will be copied directly
"array of *int": {
modifyAndVerify: func(t *testing.T) {
intp := func(i int) *int {
return &i
}
arrayOfInt := [3]*int{intp(1), intp(2)}
copied := deepcopy.Copy(&arrayOfInt)
assert.NotSame(t, &arrayOfInt, copied)
assert.NotSame(t, arrayOfInt[0], copied.(*[3]*int)[0])
arrayOfInt[0] = intp(999)
assert.Equal(t, 1, *copied.(*[3]*int)[0])
},
},
// succeed because int will be copied by value
"array of int": {
modifyAndVerify: func(t *testing.T) {
arrayOfInt := [3]int{1, 2}
copied := deepcopy.Copy(&arrayOfInt)
assert.NotSame(t, &arrayOfInt, copied)
assert.NotSame(t, &arrayOfInt[0], copied.(*[3]int)[0])
arrayOfInt[0] = 999
assert.Equal(t, 1, copied.(*[3]int)[0])
},
},
}
for key, tt := range verifyCases {
t.Run(key, tt.modifyAndVerify)
}
}
from deepcopy.
Hello @xieyuschen .
Oh! Thank you for your fixing!
I try to use it.
from deepcopy.
Related Issues (15)
- Format strings in test file are invalid HOT 1
- Could Copy(src, dst) be supported? HOT 1
- DeepCopy interface implementation doesn't improve performance
- proposed fix for a nil case HOT 3
- Not an issue, this repo just saved my day.
- Custom copy HOT 7
- Does this repo still being maintained? HOT 2
- Can you provide the performance test data
- panic: reflect: slice index out of range HOT 6
- error can not be copyed HOT 1
- facing issue while call deepcopy HOT 2
- Make public copyRecursive(original, cpy reflect.Value) HOT 2
- copy unexported field HOT 2
- Map keys aren't deep-copied HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deepcopy.