mirror of
https://github.com/golang/go
synced 2024-11-13 18:40:22 -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:
parent
fd9c99511e
commit
5fde5cd5cb
@ -188,6 +188,10 @@ type PresenceTest struct {
|
||||
Exists *struct{}
|
||||
}
|
||||
|
||||
type IgnoreTest struct {
|
||||
PublicSecret string `xml:"-"`
|
||||
}
|
||||
|
||||
type MyBytes []byte
|
||||
|
||||
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>`,
|
||||
},
|
||||
|
||||
// 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) {
|
||||
|
@ -37,7 +37,6 @@ const (
|
||||
fAny
|
||||
|
||||
// TODO:
|
||||
//fIgnore
|
||||
//fOmitEmpty
|
||||
|
||||
fMode = fElement | fAttr | fCharData | fInnerXml | fComment | fAny
|
||||
@ -62,7 +61,7 @@ func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
|
||||
n := typ.NumField()
|
||||
for i := 0; i < n; i++ {
|
||||
f := typ.Field(i)
|
||||
if f.PkgPath != "" {
|
||||
if f.PkgPath != "" || f.Tag.Get("xml") == "-" {
|
||||
continue // Private field
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user