You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Run this test:
import (
"strings"
"testing"
"xml"
)
var cr =
`<customer><id>xxxx</id><parent-dash><child-dash><name>xxx</name></child-dash><child-dash><name>yyy</name></child-dash></parent-dash></customer>`
type RespBug struct {
ID string `xml:"id"`
Children []Child `xml:"parent-dash>child-dash"`
}
type RespWorks struct {
ID string `xml:"id"`
Children []Child `xml:"parentdash>childdash"`
}
type Child struct {
Name string `xml:"name"`
}
func TestBug(t *testing.T) {
r := new(RespBug)
err := xml.Unmarshal(strings.NewReader(cr), r)
if err != nil {
t.Errorf("didn't expect error: %s", err)
}
if len(r.Children) != 2 {
t.Errorf("expected 2 children, got %d", len(r.Children))
}
}
func TestWorks(t *testing.T) {
r := new(RespWorks)
err := xml.Unmarshal(strings.NewReader(cr), r)
if err != nil {
t.Errorf("didn't expect error: %s", err)
}
if len(r.Children) != 2 {
t.Errorf("expected 2 children, got %d", len(r.Children))
}
}
/* end of code */
TestBug will fail, TestWorks will pass. It took a lot of investigation to find out that
the xml package was stripping the dashes.
What is the expected output?
expected TestBug to pass. The xml tag in RespBug has dashes in it, just like the xml it
is parsing, thus it would make sense that the fieldPath should work with dashes in it.
What do you see instead?
TestBug fails yet TestWorks passes.
Which compiler are you using (5g, 6g, 8g, gccgo)?
6g, 8g
Which operating system are you using?
mac and linux
Which revision are you using? (hg identify)
Please provide any additional information below.
The text was updated successfully, but these errors were encountered:
mikioh
changed the title
xml.Unmarshal nested field tags only work with dashes removed
encoding/xml: Unmarshal nested field tags only work with dashes removed
Jan 9, 2015
by pcrosby:
The text was updated successfully, but these errors were encountered: