]> Cypherpunks repositories - gostls13.git/commitdiff
Allow underscores in XML element names (except for leading characters)
authorMichael Hoisie <hoisie@gmail.com>
Thu, 28 Jan 2010 05:13:22 +0000 (21:13 -0800)
committerRob Pike <r@golang.org>
Thu, 28 Jan 2010 05:13:22 +0000 (21:13 -0800)
Fixes #569

R=rsc, r
CC=golang-dev
https://golang.org/cl/194121

src/pkg/xml/read.go
src/pkg/xml/xml_test.go

index 4865c064a4a2cd05f2040abd39134171053a321d..4f944038e878e7bd5629fef6e929f98c73734463 100644 (file)
@@ -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.
index 43d418c1ea93b51be3f8c603c3dc4e1f65e2277b..f228dfba3709e549685672bcdf22548185a48bc6 100644 (file)
@@ -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 := `<item><field_a>abcd</field_a></item>`
+       var i item
+       buf := bytes.NewBufferString(data)
+       err := Unmarshal(buf, &i)
+
+       if err != nil || i.Field_a != "abcd" {
+               t.Fatalf("Expecting abcd")
+       }
+}