mirror of
https://github.com/golang/go
synced 2024-11-20 10:44:41 -07:00
encoding/xml: marshal/unmarshal xml.Name in field
R=rsc CC=golang-dev https://golang.org/cl/5542052
This commit is contained in:
parent
fec7aa952f
commit
ca3e6d1367
@ -150,6 +150,10 @@ type XMLNameWithoutTag struct {
|
||||
Value string ",chardata"
|
||||
}
|
||||
|
||||
type NameInField struct {
|
||||
Foo Name `xml:"ns foo"`
|
||||
}
|
||||
|
||||
type AttrTest struct {
|
||||
Int int `xml:",attr"`
|
||||
Lower int `xml:"int,attr"`
|
||||
@ -483,6 +487,19 @@ var marshalTests = []struct {
|
||||
UnmarshalOnly: true,
|
||||
},
|
||||
|
||||
// xml.Name works in a plain field as well.
|
||||
{
|
||||
Value: &NameInField{Name{Space: "ns", Local: "foo"}},
|
||||
ExpectXML: `<NameInField><foo xmlns="ns"></foo></NameInField>`,
|
||||
},
|
||||
|
||||
// Marshaling zero xml.Name uses the tag or field name.
|
||||
{
|
||||
Value: &NameInField{},
|
||||
ExpectXML: `<NameInField><foo xmlns="ns"></foo></NameInField>`,
|
||||
MarshalOnly: true,
|
||||
},
|
||||
|
||||
// Test attributes
|
||||
{
|
||||
Value: &AttrTest{
|
||||
|
@ -271,6 +271,10 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) error {
|
||||
case reflect.Struct:
|
||||
sv = v
|
||||
typ := sv.Type()
|
||||
if typ == nameType {
|
||||
v.Set(reflect.ValueOf(start.Name))
|
||||
break
|
||||
}
|
||||
tinfo, err = getTypeInfo(typ)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -46,6 +46,8 @@ const (
|
||||
var tinfoMap = make(map[reflect.Type]*typeInfo)
|
||||
var tinfoLock sync.RWMutex
|
||||
|
||||
var nameType = reflect.TypeOf(Name{})
|
||||
|
||||
// getTypeInfo returns the typeInfo structure with details necessary
|
||||
// for marshalling and unmarshalling typ.
|
||||
func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
|
||||
@ -56,7 +58,7 @@ func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
|
||||
return tinfo, nil
|
||||
}
|
||||
tinfo = &typeInfo{}
|
||||
if typ.Kind() == reflect.Struct {
|
||||
if typ.Kind() == reflect.Struct && typ != nameType {
|
||||
n := typ.NumField()
|
||||
for i := 0; i < n; i++ {
|
||||
f := typ.Field(i)
|
||||
|
Loading…
Reference in New Issue
Block a user