Skip to content

Commit 696448e

Browse files
committed
UnknownTags() fix
Handle slice with one member correctly.
1 parent 1f1a1c2 commit 696448e

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

unknowntags.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,10 @@ func checkAllTags(mv interface{}, val reflect.Value, s *[]string, key string) {
249249
sval := reflect.New(tval)
250250
slice, ok := mv.([]interface{})
251251
if !ok {
252-
*s = append(*s, key)
252+
// See if there's a singleton, not a slice, in XML object.
253+
// If it's not there, this call will put it there.
254+
// Thanks to: zhengfang.sun <[email protected]>,
255+
checkAllTags(mv, sval, s, key)
253256
return
254257
}
255258
// 2.1. Check members of XML data

unknowntags_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,36 @@ func TestUnknownXMLTagsWithSubelemTag(t *testing.T) {
270270
}
271271
}
272272
}
273+
274+
// ===================== 11/27/18: handle single member slices correctly =============
275+
// thanks to: zhengfang.sun sunsun314 (github)
276+
277+
func TestUnknownTagsSingletonList(t *testing.T) {
278+
var b = []byte(`<yy>
279+
<xx>1</xx>
280+
</yy>`)
281+
282+
var d = []byte(`<yy>
283+
<zz>1</zz>
284+
</yy>`)
285+
286+
type Y struct {
287+
Property []string `xml:"xx"`
288+
}
289+
290+
y := Y{}
291+
tags, _, err := UnknownXMLTags(b, y)
292+
if err != nil {
293+
t.Fatal("err on b:", err.Error())
294+
} else if len(tags) != 0 {
295+
t.Fatal("reported tags for b", tags)
296+
}
297+
298+
y = Y{}
299+
tags, _, err = UnknownXMLTags(d, y)
300+
if err != nil {
301+
t.Fatal(err.Error())
302+
} else if tags[0] != "zz" {
303+
t.Fatal("didn't report 'zz' for d:", tags)
304+
}
305+
}

0 commit comments

Comments
 (0)