Commit 4b42ad25 authored by Alexander Zhavnerchik's avatar Alexander Zhavnerchik Committed by Russ Cox

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
parent 3b570d0a
......@@ -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) {
......
......@@ -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 {
......
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