mirror of
https://github.com/golang/go
synced 2024-11-21 20:44:39 -07:00
Allow underscores in XML element names (except for leading characters)
Fixes #569 R=rsc, r CC=golang-dev https://golang.org/cl/194121
This commit is contained in:
parent
535e427272
commit
f2539b1417
@ -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 @@
|
|||||||
package xml
|
package xml
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user