Skip to content

Commit

Permalink
Fix empty struct meta check (#2)
Browse files Browse the repository at this point in the history
* fix zero check

* doubly safe

* add tests

* remove commented portions of example test
  • Loading branch information
taustgen-wework authored Apr 21, 2021
1 parent 8e076d9 commit 243966e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
16 changes: 12 additions & 4 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,23 @@ func (e *encoder) structv(tag string, in reflect.Value, comment comments) {

commentsArr := makeEmptyComments(len(fieldsIndex))

if fIndex := getYamlMeta(in, fieldsIndex); fIndex.IsValid() {
meta := fIndex.Elem().Interface().(StructMeta)
fieldsIndex = meta.GetFieldsIndex()
commentsArr = meta.GetComments()
// ensure valid non nil struct meta before using
if fIndex := getYamlMeta(in, fieldsIndex); (fIndex.IsValid() && fIndex.Elem() != reflect.Value{}) {
meta, ok := fIndex.Elem().Interface().(StructMeta)
metaFieldsIndex := meta.GetFieldsIndex()
metaCommentsArr := meta.GetComments()
if ok && len(metaFieldsIndex) == len(metaCommentsArr) {
fieldsIndex = metaFieldsIndex
commentsArr = metaCommentsArr
}
}

e.mappingv(tag, comment, func() {
processed := map[int]bool{}
for i, info := range fieldsIndex {
if info.Key == yamlMeta {
continue
}
var value reflect.Value
if info.Inline == nil {
value = in.Field(info.Num)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
54 changes: 54 additions & 0 deletions structmeta_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,50 @@ var structMetaTests = []string{
"a: ant #ant\n# a\nb: #beeline\n c: cockroach #cockroach\n #c\n #d\n d: dragonfly\n #dragonfly\n",
}

var structMetaNilTests = []struct {
expected string
test testStruct
}{
{
"b:\n d: d\n c: c\na: a\n",
testStruct{
A: "a",
B: smChildStruct{
C: "c",
D: "d",
},
},
},
{
"b:\n d: d\n c: c\na: a\n",
testStruct{
A: "a",
B: smChildStruct{
C: "c",
D: "d",
},
Meta: StructMeta(&structMeta{nil, nil}),
},
},
{
"b:\n d: d\n c: c\na: a\n",
testStruct{
A: "a",
B: smChildStruct{
C: "c",
D: "d",
},
Meta: StructMeta(&structMeta{
[]fieldInfo{{
Key: "a",
Num: 2,
}},
[][]comments{},
}),
},
},
}

func (s *S) TestStructMeta(c *C) {
for _, expected := range structMetaTests {
c.Logf("test %s.", expected)
Expand All @@ -43,3 +87,13 @@ func (s *S) TestStructMeta(c *C) {
c.Assert(string(actual), Equals, expected)
}
}

func (s *S) TestStructMetaNil(c *C) {
for _, in := range structMetaNilTests {
c.Logf("test %s", in.expected)

actual, err := Marshal(in.test)
c.Assert(err, Equals, nil)
c.Assert(string(actual), Equals, in.expected)
}
}

0 comments on commit 243966e

Please sign in to comment.