Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panics for embedded unexported types #1059

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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)
Expand Down Expand Up @@ -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" +
Expand Down Expand Up @@ -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
Expand All @@ -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"`
}
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down