Skip to content

Commit 243966e

Browse files
Fix empty struct meta check (#2)
* fix zero check * doubly safe * add tests * remove commented portions of example test
1 parent 8e076d9 commit 243966e

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

encode.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,23 @@ func (e *encoder) structv(tag string, in reflect.Value, comment comments) {
221221

222222
commentsArr := makeEmptyComments(len(fieldsIndex))
223223

224-
if fIndex := getYamlMeta(in, fieldsIndex); fIndex.IsValid() {
225-
meta := fIndex.Elem().Interface().(StructMeta)
226-
fieldsIndex = meta.GetFieldsIndex()
227-
commentsArr = meta.GetComments()
224+
// ensure valid non nil struct meta before using
225+
if fIndex := getYamlMeta(in, fieldsIndex); (fIndex.IsValid() && fIndex.Elem() != reflect.Value{}) {
226+
meta, ok := fIndex.Elem().Interface().(StructMeta)
227+
metaFieldsIndex := meta.GetFieldsIndex()
228+
metaCommentsArr := meta.GetComments()
229+
if ok && len(metaFieldsIndex) == len(metaCommentsArr) {
230+
fieldsIndex = metaFieldsIndex
231+
commentsArr = metaCommentsArr
232+
}
228233
}
229234

230235
e.mappingv(tag, comment, func() {
231236
processed := map[int]bool{}
232237
for i, info := range fieldsIndex {
238+
if info.Key == yamlMeta {
239+
continue
240+
}
233241
var value reflect.Value
234242
if info.Inline == nil {
235243
value = in.Field(info.Num)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

structmeta_internal_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,50 @@ var structMetaTests = []string{
3030
"a: ant #ant\n# a\nb: #beeline\n c: cockroach #cockroach\n #c\n #d\n d: dragonfly\n #dragonfly\n",
3131
}
3232

33+
var structMetaNilTests = []struct {
34+
expected string
35+
test testStruct
36+
}{
37+
{
38+
"b:\n d: d\n c: c\na: a\n",
39+
testStruct{
40+
A: "a",
41+
B: smChildStruct{
42+
C: "c",
43+
D: "d",
44+
},
45+
},
46+
},
47+
{
48+
"b:\n d: d\n c: c\na: a\n",
49+
testStruct{
50+
A: "a",
51+
B: smChildStruct{
52+
C: "c",
53+
D: "d",
54+
},
55+
Meta: StructMeta(&structMeta{nil, nil}),
56+
},
57+
},
58+
{
59+
"b:\n d: d\n c: c\na: a\n",
60+
testStruct{
61+
A: "a",
62+
B: smChildStruct{
63+
C: "c",
64+
D: "d",
65+
},
66+
Meta: StructMeta(&structMeta{
67+
[]fieldInfo{{
68+
Key: "a",
69+
Num: 2,
70+
}},
71+
[][]comments{},
72+
}),
73+
},
74+
},
75+
}
76+
3377
func (s *S) TestStructMeta(c *C) {
3478
for _, expected := range structMetaTests {
3579
c.Logf("test %s.", expected)
@@ -43,3 +87,13 @@ func (s *S) TestStructMeta(c *C) {
4387
c.Assert(string(actual), Equals, expected)
4488
}
4589
}
90+
91+
func (s *S) TestStructMetaNil(c *C) {
92+
for _, in := range structMetaNilTests {
93+
c.Logf("test %s", in.expected)
94+
95+
actual, err := Marshal(in.test)
96+
c.Assert(err, Equals, nil)
97+
c.Assert(string(actual), Equals, in.expected)
98+
}
99+
}

0 commit comments

Comments
 (0)