diff --git a/marshal.go b/marshal.go index 29ea0fb..c370ab7 100644 --- a/marshal.go +++ b/marshal.go @@ -344,13 +344,26 @@ func valueFromToml(mtype reflect.Type, tval interface{}) (reflect.Value, error) if mtype.Kind() == reflect.Ptr { return unwrapPointer(mtype, tval) } - switch { - case isTree(mtype): - return valueFromTree(mtype, tval.(*Tree)) - case isTreeSlice(mtype): - return valueFromTreeSlice(mtype, tval.([]*Tree)) - case isOtherSlice(mtype): - return valueFromOtherSlice(mtype, tval.([]interface{})) + + switch tval.(type) { + case *Tree: + if isTree(mtype) { + return valueFromTree(mtype, tval.(*Tree)) + } else { + return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a tree", tval, tval) + } + case []*Tree: + if isTreeSlice(mtype) { + return valueFromTreeSlice(mtype, tval.([]*Tree)) + } else { + return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to trees", tval, tval) + } + case []interface{}: + if isOtherSlice(mtype) { + return valueFromOtherSlice(mtype, tval.([]interface{})) + } else { + return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a slice", tval, tval) + } default: switch mtype.Kind() { case reflect.Bool: @@ -444,7 +457,7 @@ func valueFromToml(mtype reflect.Type, tval interface{}) (reflect.Value, error) } return reflect.ValueOf(val), nil default: - return reflect.ValueOf(nil), fmt.Errorf("Unmarshal can't handle %v(%v)", mtype, mtype.Kind()) + return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v(%v)", tval, tval, mtype, mtype.Kind()) } } } diff --git a/marshal_test.go b/marshal_test.go index f9196cd..127b68a 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "reflect" + "strings" "testing" "time" ) @@ -508,6 +509,14 @@ func TestPointerUnmarshal(t *testing.T) { } } +func TestUnmarshalTypeMismatch(t *testing.T) { + result := pointerMarshalTestStruct{} + err := Unmarshal([]byte("List = 123"), &result) + if !strings.HasPrefix(err.Error(), "(1, 1): Can't convert 123(int64) to []string(slice)") { + t.Errorf("Type mismatch must be reported: got %v", err.Error()) + } +} + type nestedMarshalTestStruct struct { String [][]string //Struct [][]basicMarshalTestSubStruct