Copy to nil empty interface#217
Open
mpittkin wants to merge 1 commit intojinzhu:masterfrom
Open
Conversation
…, simply copy the value This avoids a panic when attempting to examine the type of 'from'
5dccab7 to
c163d6c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 intofunc indirectType, it attempts to call theKind()method on the nil value, causing the panic.The change I'm proposing is that in the case of the
fromvalue being a struct and thetovalue being an empty interface, to simply callto.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
fromtypes other than structs, or how to best honor things like theOptions.DeepCopyvalue.