diff options
author | Gustavo Niemeyer <gustavo@niemeyer.net> | 2012-01-19 20:15:55 -0200 |
---|---|---|
committer | Gustavo Niemeyer <gustavo@niemeyer.net> | 2012-01-19 20:15:55 -0200 |
commit | ca3e6d1367a365ec29020e3f16c7732b4240cf67 (patch) | |
tree | b56de35f6fe91fa78f5980d9a48f99450566f623 | |
parent | fec7aa952f37ad5aa2cfdfe44bdc0e7a02fe8783 (diff) | |
download | go-ca3e6d1367a365ec29020e3f16c7732b4240cf67.tar.gz go-ca3e6d1367a365ec29020e3f16c7732b4240cf67.zip |
encoding/xml: marshal/unmarshal xml.Name in field
R=rsc
CC=golang-dev
https://golang.org/cl/5542052
-rw-r--r-- | src/pkg/encoding/xml/marshal_test.go | 17 | ||||
-rw-r--r-- | src/pkg/encoding/xml/read.go | 4 | ||||
-rw-r--r-- | src/pkg/encoding/xml/typeinfo.go | 4 |
3 files changed, 24 insertions, 1 deletions
diff --git a/src/pkg/encoding/xml/marshal_test.go b/src/pkg/encoding/xml/marshal_test.go index bec53761e1..f23b2cb7e0 100644 --- a/src/pkg/encoding/xml/marshal_test.go +++ b/src/pkg/encoding/xml/marshal_test.go @@ -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{ diff --git a/src/pkg/encoding/xml/read.go b/src/pkg/encoding/xml/read.go index dde68de3e7..4419ed1e47 100644 --- a/src/pkg/encoding/xml/read.go +++ b/src/pkg/encoding/xml/read.go @@ -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 diff --git a/src/pkg/encoding/xml/typeinfo.go b/src/pkg/encoding/xml/typeinfo.go index 8f79c4e78b..36b35ed2ee 100644 --- a/src/pkg/encoding/xml/typeinfo.go +++ b/src/pkg/encoding/xml/typeinfo.go @@ -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) |