Fix map initialization.
diff --git a/decode.go b/decode.go index 3d4b128..8d2349b 100644 --- a/decode.go +++ b/decode.go
@@ -213,9 +213,9 @@ // // It's a slightly convoluted case to handle properly: // -// - Nil pointers should be zeroed out, unless being set to nil -// - We don't know at this point yet what's the value to SetYAML() with. -// - We can't separate pointer deref/init and setter checking, because +// - nil pointers should be initialized, unless being set to nil +// - we don't know at this point yet what's the value to SetYAML() with. +// - we can't separate pointer deref/init and setter checking, because // a setter may be found while going down a pointer chain. // // Thus, here is how it takes care of it: @@ -417,6 +417,10 @@ if out.Kind() != reflect.Map { return false } + if out.IsNil() { + out.Set(reflect.MakeMap(out.Type())) + } + outt := out.Type() kt := outt.Key() et := outt.Elem()
diff --git a/decode_test.go b/decode_test.go index 36699d3..b25df8c 100644 --- a/decode_test.go +++ b/decode_test.go
@@ -81,16 +81,9 @@ // Structs and type conversions. {"hello: world", &struct{ Hello string }{"world"}}, - {"a: {b: c}", &struct { - A struct { - B string - } - }{struct{ B string }{"c"}}}, - {"a: {b: c}", &struct { - A *struct { - B string - } - }{&struct{ B string }{"c"}}}, + {"a: {b: c}", &struct{ A struct{ B string } }{struct{ B string }{"c"}}}, + {"a: {b: c}", &struct{ A *struct{ B string } }{&struct{ B string }{"c"}}}, + {"a: {b: c}", &struct{ A map[string]string }{map[string]string{"b": "c"}}}, {"a: 1", &struct{ A int }{1}}, {"a: [1, 2]", &struct{ A []int }{[]int{1, 2}}}, {"a: 1", &struct{ B int }{0}},