Skip to content

Commit

Permalink
UnknownTags() fix
Browse files Browse the repository at this point in the history
Handle slice with one member correctly.
  • Loading branch information
clbanning committed Nov 27, 2018
1 parent 1f1a1c2 commit 696448e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
5 changes: 4 additions & 1 deletion unknowntags.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,10 @@ func checkAllTags(mv interface{}, val reflect.Value, s *[]string, key string) {
sval := reflect.New(tval)
slice, ok := mv.([]interface{})
if !ok {
*s = append(*s, key)
// See if there's a singleton, not a slice, in XML object.
// If it's not there, this call will put it there.
// Thanks to: zhengfang.sun <[email protected]>,
checkAllTags(mv, sval, s, key)
return
}
// 2.1. Check members of XML data
Expand Down
33 changes: 33 additions & 0 deletions unknowntags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,36 @@ func TestUnknownXMLTagsWithSubelemTag(t *testing.T) {
}
}
}

// ===================== 11/27/18: handle single member slices correctly =============
// thanks to: zhengfang.sun sunsun314 (github)

func TestUnknownTagsSingletonList(t *testing.T) {
var b = []byte(`<yy>
<xx>1</xx>
</yy>`)

var d = []byte(`<yy>
<zz>1</zz>
</yy>`)

type Y struct {
Property []string `xml:"xx"`
}

y := Y{}
tags, _, err := UnknownXMLTags(b, y)
if err != nil {
t.Fatal("err on b:", err.Error())
} else if len(tags) != 0 {
t.Fatal("reported tags for b", tags)
}

y = Y{}
tags, _, err = UnknownXMLTags(d, y)
if err != nil {
t.Fatal(err.Error())
} else if tags[0] != "zz" {
t.Fatal("didn't report 'zz' for d:", tags)
}
}

0 comments on commit 696448e

Please sign in to comment.