aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/encoding
diff options
context:
space:
mode:
authorDmitriy Shelenin <deemok@googlemail.com>2013-08-08 10:40:51 -0700
committerBrad Fitzpatrick <bradfitz@golang.org>2013-08-08 10:40:51 -0700
commit547f1a6fe7915193d6c28dac21648f08e2f67bd9 (patch)
treeb98df17b3a62038938e6d3081f685a3e8d9c07f0 /src/pkg/encoding
parent315511585481ccc93b6524b49d9f5c3f5b59e7fc (diff)
downloadgo-547f1a6fe7915193d6c28dac21648f08e2f67bd9.tar.xz
encoding/xml: allow attributes stored in pointers to be marshaled.
Fixes #5334. R=golang-dev, dave, rsc CC=golang-dev https://golang.org/cl/8653047
Diffstat (limited to 'src/pkg/encoding')
-rw-r--r--src/pkg/encoding/xml/marshal.go11
-rw-r--r--src/pkg/encoding/xml/marshal_test.go33
2 files changed, 43 insertions, 1 deletions
diff --git a/src/pkg/encoding/xml/marshal.go b/src/pkg/encoding/xml/marshal.go
index 47b0017634..fae0f6a732 100644
--- a/src/pkg/encoding/xml/marshal.go
+++ b/src/pkg/encoding/xml/marshal.go
@@ -271,7 +271,7 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo) error {
continue
}
fv := finfo.value(val)
- if finfo.flags&fOmitEmpty != 0 && isEmptyValue(fv) {
+ if (finfo.flags&fOmitEmpty != 0 || fv.Kind() == reflect.Ptr) && isEmptyValue(fv) {
continue
}
p.WriteByte(' ')
@@ -285,6 +285,11 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo) error {
}
p.WriteString(finfo.name)
p.WriteString(`="`)
+ // Handle pointer values by following the pointer,
+ // Pointer is known to be non-nil because we called isEmptyValue above.
+ if fv.Kind() == reflect.Ptr {
+ fv = fv.Elem()
+ }
if err := p.marshalSimple(fv.Type(), fv); err != nil {
return err
}
@@ -363,6 +368,10 @@ func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error {
continue
}
vf := finfo.value(val)
+ // Handle pointer values by following the pointer
+ if vf.Kind() == reflect.Ptr && !isEmptyValue(vf) {
+ vf = vf.Elem()
+ }
switch finfo.flags & fMode {
case fCharData:
var scratch [64]byte
diff --git a/src/pkg/encoding/xml/marshal_test.go b/src/pkg/encoding/xml/marshal_test.go
index ca14a1e53d..fa2ba52a8f 100644
--- a/src/pkg/encoding/xml/marshal_test.go
+++ b/src/pkg/encoding/xml/marshal_test.go
@@ -276,6 +276,25 @@ type Strings struct {
X []string `xml:"A>B,omitempty"`
}
+type PointerFieldsTest struct {
+ XMLName Name `xml:"dummy"`
+ Name *string `xml:"name,attr"`
+ Age *uint `xml:"age,attr"`
+ Empty *string `xml:"empty,attr"`
+ Contents *string `xml:",chardata"`
+}
+
+type ChardataEmptyTest struct {
+ XMLName Name `xml:"test"`
+ Contents *string `xml:",chardata"`
+}
+
+var (
+ nameAttr = "Sarah"
+ ageAttr = uint(12)
+ contentsAttr = "lorem ipsum"
+)
+
// Unless explicitly stated as such (or *Plain), all of the
// tests below are two-way tests. When introducing new tests,
// please try to make them two-way as well to ensure that
@@ -673,6 +692,20 @@ var marshalTests = []struct {
ExpectXML: `<OmitAttrTest></OmitAttrTest>`,
},
+ // pointer fields
+ {
+ Value: &PointerFieldsTest{Name: &nameAttr, Age: &ageAttr, Contents: &contentsAttr},
+ ExpectXML: `<dummy name="Sarah" age="12">lorem ipsum</dummy>`,
+ MarshalOnly: true,
+ },
+
+ // empty chardata pointer field
+ {
+ Value: &ChardataEmptyTest{},
+ ExpectXML: `<test></test>`,
+ MarshalOnly: true,
+ },
+
// omitempty on fields
{
Value: &OmitFieldTest{