Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

source with nil pointer replacing existing pointer in destination #167

Open
iDevoid opened this issue Sep 21, 2022 · 1 comment
Open

source with nil pointer replacing existing pointer in destination #167

iDevoid opened this issue Sep 21, 2022 · 1 comment
Assignees

Comments

@iDevoid
Copy link

iDevoid commented Sep 21, 2022

Reproducible Example

go playground

Description

expectation

Should not replace non-nil pointer with nil or non existing data

@kcchu
Copy link
Contributor

kcchu commented Sep 25, 2022

This is the line of code that lead to this behavior:

copier/copier.go

Lines 462 to 467 in 0e264e9

if to.Kind() == reflect.Ptr {
// set `to` to nil if from is nil
if from.Kind() == reflect.Ptr && from.IsNil() {
to.Set(reflect.Zero(to.Type()))
return true
} else if to.IsNil() {

But it seems to me that it is that intended behavior because a nil is also a value that should be copied as if other values. Setting IgnoreEmpty in copier.Option could achieve what you want. However, not only it would ignore nil pointers in source, but also ignore zero values such as empty strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants