fix: honor field shadowing in squashed embedded structs - #209
Open
AdamMagued wants to merge 1 commit into
Open
AdamMagued wants to merge 1 commit into
AdamMagued wants to merge 1 commit into
Conversation
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 #81
Problem
When decoding a map into a struct with embedded, squashed structs, fields in an embedded struct that share a key name with an outer struct field are not shadowed. Instead, both fields are added to the decode queue and matched against the same map key.
For example:
When decoding
{"obj_type": "b", "obj_id": 2, "version": "defg"}withSquash: true:B.Versioncorrectly decodes"defg".B.A.Versionis also evaluated against"version": "defg", attempting to parse"defg"into anint, returning an unconvertible type/parsing error.In Go struct embedding rules, outer struct fields at shallower embedding depth shadow fields with identical names or matching tags in embedded structs at deeper levels.
Root Cause
In
decodeStructFromMap, embedded structs are flattened via breadth-first search into a flatfieldsslice. During collection, no record of embedding depth was retained, and all fields were queued indiscriminately. During iteration overfields, each field performed a map key lookup ondataVal, so both outer and embedded fields matched the same map key.Fix
depth: 0for outer struct, incrementing for nested squashed structs).currentDepth > sf.depth) matches a field name already claimed at a shallower depth viaMatchName, skip it as shadowed.Verification
TestDecode_EmbeddedStructFieldShadowingreproducing the exact failure from Decode with embedded struct wrong set value into child struct #81 (fails prior to fix with parsing error onA.Version, passes withB.Version = "defg"andB.A.Version = 0).TestDecode_EmbeddedStructFieldShadowing_MultiLevelverifying multi-level squashing where depth 0 shadows depths 1 and 2, and depth 1 shadows depth 2 while unconflicting fields remain populated.go test -v -race -shuffle=on ./...passed.gofmt -s -w .andgo vet ./...cleanly.