1
0
mirror of https://github.com/golang/go synced 2024-11-27 05:01:19 -07:00

encoding/xml: Makes XML Marshaler take into account XMLName field from anonymous field

Fixes #7614.

LGTM=rsc
R=golang-codereviews, r, rsc, dan.kortschak, applezinc
CC=golang-codereviews
https://golang.org/cl/79210044
This commit is contained in:
Alexander Zhavnerchik 2014-04-08 11:12:51 -04:00 committed by Russ Cox
parent 3b570d0a8c
commit 4b42ad2559
2 changed files with 44 additions and 0 deletions

View File

@ -314,6 +314,31 @@ type MarshalerStruct struct {
Foo MyMarshalerAttrTest `xml:",attr"`
}
type InnerStruct struct {
XMLName Name `xml:"testns outer"`
}
type OuterStruct struct {
InnerStruct
IntAttr int `xml:"int,attr"`
}
type OuterNamedStruct struct {
InnerStruct
XMLName Name `xml:"outerns test"`
IntAttr int `xml:"int,attr"`
}
type OuterNamedOrderedStruct struct {
XMLName Name `xml:"outerns test"`
InnerStruct
IntAttr int `xml:"int,attr"`
}
type OuterOuterStruct struct {
OuterStruct
}
func ifaceptr(x interface{}) interface{} {
return &x
}
@ -883,6 +908,22 @@ var marshalTests = []struct {
ExpectXML: `<MarshalerStruct Foo="hello world"></MarshalerStruct>`,
Value: &MarshalerStruct{},
},
{
ExpectXML: `<outer xmlns="testns" int="10"></outer>`,
Value: &OuterStruct{IntAttr: 10},
},
{
ExpectXML: `<test xmlns="outerns" int="10"></test>`,
Value: &OuterNamedStruct{XMLName: Name{Space: "outerns", Local: "test"}, IntAttr: 10},
},
{
ExpectXML: `<test xmlns="outerns" int="10"></test>`,
Value: &OuterNamedOrderedStruct{XMLName: Name{Space: "outerns", Local: "test"}, IntAttr: 10},
},
{
ExpectXML: `<outer xmlns="testns" int="10"></outer>`,
Value: &OuterOuterStruct{OuterStruct{IntAttr: 10}},
},
}
func TestMarshal(t *testing.T) {

View File

@ -75,6 +75,9 @@ func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
if err != nil {
return nil, err
}
if tinfo.xmlname == nil {
tinfo.xmlname = inner.xmlname
}
for _, finfo := range inner.fields {
finfo.idx = append([]int{i}, finfo.idx...)
if err := addFieldInfo(typ, tinfo, &finfo); err != nil {