Commit 5fde5cd5 authored by Gustavo Niemeyer's avatar Gustavo Niemeyer

encoding/xml: support ignoring fields with "-"

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/5564045
parent fd9c9951
...@@ -188,6 +188,10 @@ type PresenceTest struct { ...@@ -188,6 +188,10 @@ type PresenceTest struct {
Exists *struct{} Exists *struct{}
} }
type IgnoreTest struct {
PublicSecret string `xml:"-"`
}
type MyBytes []byte type MyBytes []byte
type Data struct { type Data struct {
...@@ -592,6 +596,22 @@ var marshalTests = []struct { ...@@ -592,6 +596,22 @@ var marshalTests = []struct {
}, },
ExpectXML: `<RecurseA><A>a1</A><B><A><A>a2</A></A><B>b1</B></B></RecurseA>`, ExpectXML: `<RecurseA><A>a1</A><B><A><A>a2</A></A><B>b1</B></B></RecurseA>`,
}, },
// Test ignoring fields via "-" tag
{
ExpectXML: `<IgnoreTest></IgnoreTest>`,
Value: &IgnoreTest{},
},
{
ExpectXML: `<IgnoreTest></IgnoreTest>`,
Value: &IgnoreTest{PublicSecret: "can't tell"},
MarshalOnly: true,
},
{
ExpectXML: `<IgnoreTest><PublicSecret>ignore me</PublicSecret></IgnoreTest>`,
Value: &IgnoreTest{},
UnmarshalOnly: true,
},
} }
func TestMarshal(t *testing.T) { func TestMarshal(t *testing.T) {
......
...@@ -37,7 +37,6 @@ const ( ...@@ -37,7 +37,6 @@ const (
fAny fAny
// TODO: // TODO:
//fIgnore
//fOmitEmpty //fOmitEmpty
fMode = fElement | fAttr | fCharData | fInnerXml | fComment | fAny fMode = fElement | fAttr | fCharData | fInnerXml | fComment | fAny
...@@ -62,7 +61,7 @@ func getTypeInfo(typ reflect.Type) (*typeInfo, error) { ...@@ -62,7 +61,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 != "" { if f.PkgPath != "" || 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