Skip to content

Copy to nil empty interface#217

Open
mpittkin wants to merge 1 commit intojinzhu:masterfrom
mpittkin:copy-to-nil-empty-interface
Open

Copy to nil empty interface#217
mpittkin wants to merge 1 commit intojinzhu:masterfrom
mpittkin:copy-to-nil-empty-interface

Conversation

@mpittkin
Copy link
Copy Markdown

@mpittkin mpittkin commented Sep 5, 2024

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.

…, simply copy the value

This avoids a panic when attempting to examine the type of 'from'
@mpittkin mpittkin force-pushed the copy-to-nil-empty-interface branch from 5dccab7 to c163d6c Compare September 5, 2024 14:11
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

Successfully merging this pull request may close these issues.

1 participant