Fix map initialization.
R=rog
CC=
https://codereview.appspot.com/5503044
diff --git a/decode.go b/decode.go
index 3d4b128..9474fd5 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:
@@ -421,6 +421,9 @@
kt := outt.Key()
et := outt.Elem()
+ if out.IsNil() {
+ out.Set(reflect.MakeMap(outt))
+ }
l := len(n.children)
for i := 0; i < l; i += 2 {
k := reflect.New(kt).Elem()
diff --git a/decode_test.go b/decode_test.go
index 36699d3..7be94cb 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -81,16 +81,10 @@
// 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: {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}},