Fix unmarshaling of nested structs (#340)

Fixes #339
This commit is contained in:
Allen
2020-03-23 23:23:21 +08:00
committed by GitHub
parent a12e102214
commit 7ee1118b4b
2 changed files with 58 additions and 14 deletions
+38
View File
@@ -2399,3 +2399,41 @@ func TestMarshalLocalTime(t *testing.T) {
})
}
}
// test case for issue #339
func TestUnmarshalSameInnerField(t *testing.T) {
type InterStruct2 struct {
Test string
Name string
Age int
}
type Inter2 struct {
Name string
Age int
InterStruct2 InterStruct2
}
type Server struct {
Name string `toml:"name"`
Inter2 Inter2 `toml:"inter2"`
}
var server Server
if err := Unmarshal([]byte(`name = "123"
[inter2]
name = "inter2"
age = 222`), &server); err == nil {
expected := Server{
Name: "123",
Inter2: Inter2{
Name: "inter2",
Age: 222,
},
}
if !reflect.DeepEqual(server, expected) {
t.Errorf("Bad unmarshal: expected %v, got %v", expected, server)
}
} else {
t.Fatalf("unexpected error: %v", err)
}
}