Commit 34f04a67 authored by Marcel van Lohuizen's avatar Marcel van Lohuizen

encoding/xml: check for exported fields in embedded structs

Addresses issue #12367.

Must be checked in before CL 14010.

Change-Id: I4523a1de112ed02371504e27882659bce8028a9f
Reviewed-on: https://go-review.googlesource.com/14012Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent a30dd9ce
...@@ -139,6 +139,7 @@ type EmbedA struct { ...@@ -139,6 +139,7 @@ type EmbedA struct {
EmbedC EmbedC
EmbedB EmbedB EmbedB EmbedB
FieldA string FieldA string
embedD
} }
type EmbedB struct { type EmbedB struct {
...@@ -153,6 +154,11 @@ type EmbedC struct { ...@@ -153,6 +154,11 @@ type EmbedC struct {
FieldC string FieldC string
} }
type embedD struct {
fieldD string
FieldE string // Promoted and visible when embedD is embedded.
}
type NameCasing struct { type NameCasing struct {
XMLName struct{} `xml:"casing"` XMLName struct{} `xml:"casing"`
Xy string Xy string
...@@ -711,6 +717,9 @@ var marshalTests = []struct { ...@@ -711,6 +717,9 @@ var marshalTests = []struct {
}, },
}, },
FieldA: "A.A", FieldA: "A.A",
embedD: embedD{
FieldE: "A.D.E",
},
}, },
ExpectXML: `<EmbedA>` + ExpectXML: `<EmbedA>` +
`<FieldB>A.C.B</FieldB>` + `<FieldB>A.C.B</FieldB>` +
...@@ -724,6 +733,7 @@ var marshalTests = []struct { ...@@ -724,6 +733,7 @@ var marshalTests = []struct {
`<FieldC>A.B.C.C</FieldC>` + `<FieldC>A.B.C.C</FieldC>` +
`</EmbedB>` + `</EmbedB>` +
`<FieldA>A.A</FieldA>` + `<FieldA>A.A</FieldA>` +
`<FieldE>A.D.E</FieldE>` +
`</EmbedA>`, `</EmbedA>`,
}, },
......
...@@ -60,7 +60,7 @@ func getTypeInfo(typ reflect.Type) (*typeInfo, error) { ...@@ -60,7 +60,7 @@ func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
n := typ.NumField() n := typ.NumField()
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
f := typ.Field(i) f := typ.Field(i)
if f.PkgPath != "" || f.Tag.Get("xml") == "-" { if (f.PkgPath != "" && !f.Anonymous) || f.Tag.Get("xml") == "-" {
continue // Private field continue // Private field
} }
......
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