1
0
mirror of https://github.com/golang/go synced 2024-11-22 04:14:42 -07:00

encoding/xml: support ignoring fields with "-"

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/5564045
This commit is contained in:
Gustavo Niemeyer 2012-01-23 01:34:35 -02:00
parent fd9c99511e
commit 5fde5cd5cb
2 changed files with 21 additions and 2 deletions

View File

@ -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 {
}, },
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) {

View File

@ -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) {
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
} }