diff --git a/marshal.go b/marshal.go index 37d6bbd..b33f54a 100644 --- a/marshal.go +++ b/marshal.go @@ -692,6 +692,16 @@ func (d *Decoder) valueFromToml(mtype reflect.Type, tval interface{}, mval1 *ref if isTree(mtype) { return d.valueFromTree(mtype, t, mval11) } + + if mtype.Kind() == reflect.Interface && + mval1.Elem().Kind() == reflect.Ptr && + reflect.TypeOf(mval1.Elem()).Kind() == reflect.Struct { + + mval111 := reflect.ValueOf(mval1.Interface()).Elem() + mval11 = &mval111 + return d.valueFromTree(reflect.TypeOf(mval1.Interface()).Elem(), t, mval11) + } + return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a tree", tval, tval) case []*Tree: if isTreeSequence(mtype) { diff --git a/marshal_test.go b/marshal_test.go index 793fce5..56dbacf 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -157,6 +157,44 @@ var mashalOrderPreserveMapToml = []byte(`title = "TOML Marshal Testing" j9 = "10" `) +type Conf struct { + Name string + Age int + Inter interface{} +} + +type NestedStruct struct { + FirstName string + LastName string + Age int +} + +var doc = []byte(`Name = "rui" +Age = 18 + +[Inter] + FirstName = "wang" + LastName = "jl" + Age = 100`) + +func TestInterface(t *testing.T) { + var config Conf + config.Inter = &NestedStruct{} + err := Unmarshal(doc, &config) + expected := Conf{ + Name: "rui", + Age: 18, + Inter: NestedStruct{ + FirstName: "wang", + LastName: "jl", + Age: 100, + }, + } + if err != nil || !reflect.DeepEqual(config, expected) { + t.Errorf("Bad unmarshal: expected %v, got %v", expected, config) + } +} + func TestBasicMarshal(t *testing.T) { result, err := Marshal(basicTestData) if err != nil {