diff --git a/src/pkg/xml/read.go b/src/pkg/xml/read.go index 4865c064a4..4f944038e8 100644 --- a/src/pkg/xml/read.go +++ b/src/pkg/xml/read.go @@ -149,14 +149,20 @@ func (p *Parser) Unmarshal(val interface{}, start *StartElement) os.Error { // to create a valid Go struct name. It also converts the // name to lower case letters. func fieldName(original string) string { + + var i int + //remove leading underscores + for i = 0; i < len(original) && original[i] == '_'; i++ { + } + return strings.Map( func(x int) int { - if unicode.IsDigit(x) || unicode.IsLetter(x) { + if x == '_' || unicode.IsDigit(x) || unicode.IsLetter(x) { return unicode.ToLower(x) } return -1 }, - original) + original[i:]) } // Unmarshal a single XML element into val. diff --git a/src/pkg/xml/xml_test.go b/src/pkg/xml/xml_test.go index 43d418c1ea..f228dfba37 100644 --- a/src/pkg/xml/xml_test.go +++ b/src/pkg/xml/xml_test.go @@ -5,6 +5,7 @@ package xml import ( + "bytes" "io" "os" "reflect" @@ -212,3 +213,18 @@ func TestSyntax(t *testing.T) { } } } + +type item struct { + Field_a string +} + +func TestIssue569(t *testing.T) { + data := `abcd` + var i item + buf := bytes.NewBufferString(data) + err := Unmarshal(buf, &i) + + if err != nil || i.Field_a != "abcd" { + t.Fatalf("Expecting abcd") + } +}