fix: recurse into []*struct with Deep struct-to-map decoding (#196) - #199
Open
chiliec wants to merge 1 commit into
Open
fix: recurse into []*struct with Deep struct-to-map decoding (#196)#199chiliec wants to merge 1 commit into
chiliec wants to merge 1 commit into
Conversation
decodeMapFromStruct only mapped slices whose element Kind is reflect.Struct into []map[string]any under Deep. A slice of pointers to structs ([]*T) has element Kind reflect.Ptr, so it fell through to the default branch and was copied verbatim, leaving raw *T pointers in the output map while the equivalent []T field was correctly turned into []map[string]any. Treat []*T (pointer-to-struct element) the same as []T. Extends the existing TestDecode_structArrayDeepMap with a []*SourceChild field that fails before this change and passes after. Fixes go-viper#196.
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.
Fixes #196.
Problem
With
DecoderConfig.Deep(or a,deeptag), struct-to-map decoding recurses into[]MyStructand produces[]map[string]any, but a[]*MyStructfield is left as raw[]*MyStructin the output map. The two slice shapes decode inconsistently.Root cause is in
decodeMapFromStruct'sreflect.Slicebranch, which only recurses when the elementKindisreflect.Struct:For
[]*Tthe elementKindisreflect.Ptr, so it falls intodefaultand the pointer elements are copied as-is.Fix
Treat a slice whose element is a pointer-to-struct the same as a slice of structs, mapping both to
[]map[string]any. The existingd.decodecall already dereferences the pointer elements, so no other change is needed.Test
Extended the existing
TestDecode_structArrayDeepMapwith aChildrenC []*SourceChildfield alongside the current[]SourceChildand*[]SourceChildcases. It fails before the change (children-ccomes back as[]*mapstructure.SourceChild) and passes after ([]map[string]any).Verification (real runs, Go 1.24.6)
mapstructure.gochange →FAIL: TestDecode_structArrayDeepMap(children-c=[]*mapstructure.SourceChild{...}).PASS.go test -race -shuffle=on ./...→ok github.com/go-viper/mapstructure/v2.gofmt -s,gofumpt, andgo vet ./...all clean.AI assistance disclosure
Prepared with an AI coding assistant; the root-cause analysis, fix, extended test, and the RED→GREEN + race/lint verification above were run against a real Go 1.24.6 toolchain, not generated.