jinzhu / copier

Copier for golang, copy value from struct to struct and more
MIT License
5.52k stars 485 forks source link

Copy to nil empty interface #217

Open mpittkin opened 1 month ago

mpittkin commented 1 month ago

We have been using an old version of this package, and our attempts to update it to a newer version have failed because when copying a struct value to an empty interface with a nil value, a panic is caused.

In copier.go#150, reflect.TypeOf(to.Interface()) returns nil because it's an empty interface, and when it passes that nil value into func indirectType, it attempts to call the Kind() method on the nil value, causing the panic.

The change I'm proposing is that in the case of the from value being a struct and the to value being an empty interface, to simply call to.Set(from) and then return.

I'm fairly confident this won't break any existing client code since the current behavior in this case is to panic. But I'm very open to suggestions on how this might better be accomplished, especially in the case of from types other than structs, or how to best honor things like the Options.DeepCopy value.