From 1479e1066396e0bdf2a345d019ed78baeaf9f943 Mon Sep 17 00:00:00 2001 From: AllenX2018 Date: Fri, 8 May 2020 18:43:08 +0800 Subject: [PATCH] fix issue #406 --- marshal.go | 3 ++- marshal_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/marshal.go b/marshal.go index 8357dac..db5a7b4 100644 --- a/marshal.go +++ b/marshal.go @@ -822,7 +822,8 @@ func (d *Decoder) valueFromTree(mtype reflect.Type, tval *Tree, mval1 *reflect.V if !mtypef.Anonymous { tmpTval = nil } - v, err := d.valueFromTree(mtypef.Type, tmpTval, nil) + fval := mval.Field(i) + v, err := d.valueFromTree(mtypef.Type, tmpTval, &fval) if err != nil { return v, err } diff --git a/marshal_test.go b/marshal_test.go index b4d38f6..c5a440d 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -3807,3 +3807,44 @@ func TestTextUnmarshalError(t *testing.T) { t.Fatalf("expected err, got none") } } + +// issue406 +func TestPreserveNotEmptyField(t *testing.T) { + toml := []byte(`Field1 = "ccc"`) + type Inner struct { + InnerField1 string + InnerField2 int + } + type TestStruct struct { + Field1 string + Field2 int + Field3 Inner + } + + actual := TestStruct{ + "aaa", + 100, + Inner{ + "bbb", + 200, + }, + } + + expected := TestStruct{ + "ccc", + 100, + Inner{ + "bbb", + 200, + }, + } + + err := Unmarshal(toml, &actual) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(actual, expected) { + t.Errorf("Bad unmarshal: expected %+v, got %+v", expected, actual) + } +}