If data is zero type, decodeBasic should set the zero value
diff --git a/mapstructure.go b/mapstructure.go index 9ef46c9..7533b2c 100644 --- a/mapstructure.go +++ b/mapstructure.go
@@ -90,8 +90,9 @@ func decodeBasic(name string, data interface{}, val reflect.Value) error { dataVal := reflect.ValueOf(data) if !dataVal.IsValid() { - // This should never happen because upstream makes sure it is valid - panic("data is invalid") + // If the data isn't valid (the zero type), then we set the + // value to be the zero type of the field we want to set. + dataVal = reflect.Zero(val.Type()) } dataValType := dataVal.Type()
diff --git a/mapstructure_bugs_test.go b/mapstructure_bugs_test.go new file mode 100644 index 0000000..28a4940 --- /dev/null +++ b/mapstructure_bugs_test.go
@@ -0,0 +1,21 @@ +package mapstructure + +import "testing" + +// GH-1 +func TestDecode_NilValue(t *testing.T) { + input := map[string]interface{}{ + "vstring": nil, + } + + var result Basic + err := Decode(input, &result) + if err != nil { + t.Fatalf("should not error: %s", err) + } + + if result.Vstring != "" { + t.Fatalf("value should be default: %s", result.Vstring) + } +} +