]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: support ignoring fields with "-"
authorGustavo Niemeyer <gustavo@niemeyer.net>
Mon, 23 Jan 2012 03:34:35 +0000 (01:34 -0200)
committerGustavo Niemeyer <gustavo@niemeyer.net>
Mon, 23 Jan 2012 03:34:35 +0000 (01:34 -0200)
R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/5564045

src/pkg/encoding/xml/marshal_test.go
src/pkg/encoding/xml/typeinfo.go

index 6beff1701946b831a1e91c450510fe0e8f8b6d73..10871fd129009308c4f6b6fec93bb2114df4b47f 100644 (file)
@@ -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) {
index 36b35ed2ee888920cf6ec75c68c7cc04144a21de..2bf2c6b3032e98d580e7fabc62c6b6dc3f9db16d 100644 (file)
@@ -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
                        }