fix(decode): let the later field win an equal-depth name clash

Assisted-by: GLM 5.3
This commit is contained in:
2026-09-17 23:07:27 +02:00
parent 54c6032a9a
commit bb238c98c3
3 changed files with 19 additions and 1 deletions
+16
View File
@@ -586,6 +586,22 @@ func TestUnmarshalEmbeddedNameClashShallowerWins(t *testing.T) {
}
}
func TestUnmarshalNameClashEqualDepthLaterWins(t *testing.T) {
// At equal depth the field declared later resolves the name, matching the
// documented rule.
type C struct {
First string `toml:"v"`
Second int `toml:"v"`
}
var c C
if err := Unmarshal([]byte("v = 1\n"), &c); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if c.Second != 1 {
t.Fatalf("decoded: %+v, want the later field to take the value", c)
}
}
func TestUnmarshalUnknownKeyWithoutEmbeddedMap(t *testing.T) {
var cfg RoundTripDerived
if err := Unmarshal([]byte("rogue = 1\n"), &cfg); err != nil {