diff --git a/decode_test.go b/decode_test.go index 0364b0bb..5dd02fac 100644 --- a/decode_test.go +++ b/decode_test.go @@ -802,6 +802,12 @@ var unmarshalTests = []struct { "c": []interface{}{"d", "e"}, }, }, + + // Unexported embedded type + { + "a: exported\nunexportedb: null\n", + map[string]interface{}{"a": "exported", "unexportedb": interface{}(nil)}, + }, } type M map[string]interface{} @@ -820,6 +826,15 @@ type inlineD struct { D int } +type UnexportedA struct { + A string + unexportedB +} + +type unexportedB struct { + B string +} + func (s *S) TestUnmarshal(c *C) { for i, item := range unmarshalTests { c.Logf("test %d: %q", i, item.data) @@ -947,7 +962,7 @@ var unmarshalErrorTests = []struct { {"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"}, {"a:\n 1:\nb\n 2:", ".*could not find expected ':'"}, {"a: 1\nb: 2\nc 2\nd: 3\n", "^yaml: line 3: could not find expected ':'$"}, - {"#\n-\n{", "yaml: line 3: could not find expected ':'"}, // Issue #665 + {"#\n-\n{", "yaml: line 3: could not find expected ':'"}, // Issue #665 {"0: [:!00 \xef", "yaml: incomplete UTF-8 octet sequence"}, // Issue #666 { "a: &a [00,00,00,00,00,00,00,00,00]\n" + @@ -1482,7 +1497,7 @@ func (s *S) TestMergeNestedStruct(c *C) { // 2) A simple implementation might attempt to handle the key skipping // directly by iterating over the merging map without recursion, but // there are more complex cases that require recursion. - // + // // Quick summary of the fields: // // - A must come from outer and not overriden @@ -1498,7 +1513,7 @@ func (s *S) TestMergeNestedStruct(c *C) { A, B, C int } type Outer struct { - D, E int + D, E int Inner Inner Inline map[string]int `yaml:",inline"` } @@ -1516,10 +1531,10 @@ func (s *S) TestMergeNestedStruct(c *C) { // Repeat test with a map. var testm map[string]interface{} - var wantm = map[string]interface {} { - "f": 60, + var wantm = map[string]interface{}{ + "f": 60, "inner": map[string]interface{}{ - "a": 10, + "a": 10, }, "d": 40, "e": 50, diff --git a/encode.go b/encode.go index de9e72a3..fb2207c0 100644 --- a/encode.go +++ b/encode.go @@ -110,7 +110,7 @@ func (e *encoder) marshalDoc(tag string, in reflect.Value) { func (e *encoder) marshal(tag string, in reflect.Value) { tag = shortTag(tag) - if !in.IsValid() || in.Kind() == reflect.Ptr && in.IsNil() { + if !in.IsValid() || !in.CanInterface() || in.Kind() == reflect.Ptr && in.IsNil() { e.nilv() return } diff --git a/encode_test.go b/encode_test.go index 4a8bf2e2..da457eb3 100644 --- a/encode_test.go +++ b/encode_test.go @@ -494,6 +494,12 @@ var marshalTests = []struct { }, "value: !!seq []\n", }, + + // unexported embedded type + { + UnexportedA{A: "exported"}, + "a: exported\nunexportedb: null\n", + }, } func (s *S) TestMarshal(c *C) {