Commit f2539b14 authored by Michael Hoisie's avatar Michael Hoisie Committed by Rob Pike

Allow underscores in XML element names (except for leading characters)

Fixes #569

R=rsc, r
CC=golang-dev
https://golang.org/cl/194121
parent 535e4272
...@@ -149,14 +149,20 @@ func (p *Parser) Unmarshal(val interface{}, start *StartElement) os.Error { ...@@ -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 // to create a valid Go struct name. It also converts the
// name to lower case letters. // name to lower case letters.
func fieldName(original string) string { func fieldName(original string) string {
var i int
//remove leading underscores
for i = 0; i < len(original) && original[i] == '_'; i++ {
}
return strings.Map( return strings.Map(
func(x int) int { func(x int) int {
if unicode.IsDigit(x) || unicode.IsLetter(x) { if x == '_' || unicode.IsDigit(x) || unicode.IsLetter(x) {
return unicode.ToLower(x) return unicode.ToLower(x)
} }
return -1 return -1
}, },
original) original[i:])
} }
// Unmarshal a single XML element into val. // Unmarshal a single XML element into val.
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package xml package xml
import ( import (
"bytes"
"io" "io"
"os" "os"
"reflect" "reflect"
...@@ -212,3 +213,18 @@ func TestSyntax(t *testing.T) { ...@@ -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")
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment