Giter VIP home page Giter VIP logo

Comments (4)

dolmen avatar dolmen commented on August 22, 2024 2

Here is a live demo on the Go playground with a simplified test case: type [1][]int.

from deepcopy.

DenisYin66 avatar DenisYin66 commented on August 22, 2024

`
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.

xieyuschen avatar xieyuschen commented on August 22, 2024

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.

HarutakaMatsumoto avatar HarutakaMatsumoto commented on August 22, 2024

Hello @xieyuschen .
Oh! Thank you for your fixing!
I try to use it.

from deepcopy.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.