copier icon indicating copy to clipboard operation
copier copied to clipboard

Doesn't deep-copy structs provided via pointers

Open bryndin opened this issue 2 years ago • 2 comments

	var user *User = &User{Name: "Jinzhu", Age: 18, Role: "Admin", Salary: 200000}
	var newUser *User
	err := copier.CopyWithOption(&newUser, &user, copier.Option{DeepCopy: true})

Gives copy destination is invalid error.

https://goplay.tools/snippet/ITQqvdUwKWH

bryndin avatar Jun 06 '22 01:06 bryndin

var newUser *User = &User{}

voctior avatar Jun 10 '22 09:06 voctior

You should check the error message return

        err := copier.CopyWithOption(&newUser, &user, copier.Option{DeepCopy: true})
	if err != nil {
		panic(err)
	}

harrisonho99 avatar Jun 14 '22 02:06 harrisonho99

Sorry, but this isn't an error, and it is handled with an error message.

The problem exists in the second line of your example. This is messing up more with Golang than this library.

uded avatar Jan 06 '23 13:01 uded

@uded thanks. Would it be better to clarify why this error is happening, e.g. if the destination is nil => "copy destination requires a non-nil pointer to a struct" not just "invalid"

bryndin avatar Jan 06 '23 22:01 bryndin

@bryndin OK, valid point. Are you up to creating a PR?

uded avatar Jan 10 '23 03:01 uded

yes, but it may take me some time to get to it.

bryndin avatar Jan 17 '23 06:01 bryndin

Probably less than for us here :-) Will be waiting for your PR and reviewing it after we will get it ASAP...

uded avatar Jan 17 '23 10:01 uded

@uded here we go ^

bryndin avatar Jan 27 '23 08:01 bryndin