Conversation
Validate the behaviour of returning an error instead of panicking when trying to set a value for an unexported variable. Decoding a map into a struct with an embedded pointer to an unexported type tagged with `mapstructure:",squash"` currently panics with: `reflect: reflect.Value.Set using value obtained using unexported field` Add TestDecode_EmbeddedUnexportedPointerSquash_FromMapToStruct to lock in the expected behaviour: Decode returns an error containing "unsupported type for squash" rather than panicking. This behaviour change was noticed by the `Test_EmbeddedPrivateStructPointer` in anchore/fangs: https://github.com/anchore/fangs/blob/d9f7358ffc5f9c1935fd3bbc2e5a16fb92ff5b6c/load_test.go#L731 Signed-off-by: Rafael Passos <rafael@rcpassos.me>
Guard the allocation with CanSet() and return the previous "unsupported type for squash" error for fields that cannot be set, rather than panicking. Signed-off-by: Rafael Passos <rafael@rcpassos.me>
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.
Hi,
I am a Debian contributor, and noticed a failure when running the tests for a package that uses mapstructure.
I added a regression test first (that fails), and the commit that fixes it after.
Decoding a map into a struct with an embedded pointer
to an unexported type tagged with
mapstructure:",squash"would panic with:
reflect: reflect.Value.Set using value obtained using unexported fieldAdd TestDecode_EmbeddedUnexportedPointerSquash_FromMapToStruct to
lock in the expected behaviour: Decode returns an error containing
"unsupported type for squash" rather than panicking.
This behaviour change was noticed by the
Test_EmbeddedPrivateStructPointerin anchore/fangs:https://github.com/anchore/fangs/blob/d9f7358ffc5f9c1935fd3bbc2e5a16fb92ff5b6c/load_test.go#L731