FAQ
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com,

I'd like you to review this change to
https://code.google.com/p/go


Description:
encoding/xml: Marshal ",any" fields

Fixes issue 3559.

This makes Marshal handle fields marked ",any" instead of ignoring
them. That makes Marshal more symmetrical with Unmarshal, which seems
to have been a design goal.

Note some test cases were changed, because this patch changes
marshalling behavior. I think the previous behavior was buggy, but
there's still a backward-compatibility question to consider.

Please review this at https://codereview.appspot.com/6938068/

Affected files:
src/pkg/encoding/xml/marshal.go
src/pkg/encoding/xml/marshal_test.go
src/pkg/encoding/xml/read.go
src/pkg/encoding/xml/typeinfo.go


Index: src/pkg/encoding/xml/marshal.go
===================================================================
--- a/src/pkg/encoding/xml/marshal.go
+++ b/src/pkg/encoding/xml/marshal.go
@@ -273,7 +273,7 @@
s := parentStack{printer: p}
for i := range tinfo.fields {
finfo := &tinfo.fields[i]
- if finfo.flags&(fAttr|fAny) != 0 {
+ if finfo.flags&(fAttr) != 0 {
continue
}
vf := finfo.value(val)
@@ -340,7 +340,7 @@
continue
}

- case fElement:
+ case fElement, fElement | fAny:
s.trim(finfo.parents)
if len(finfo.parents) > len(s.stack) {
if vf.Kind() != reflect.Ptr && vf.Kind() != reflect.Interface |
!vf.IsNil() {
Index: src/pkg/encoding/xml/marshal_test.go
===================================================================
--- a/src/pkg/encoding/xml/marshal_test.go
+++ b/src/pkg/encoding/xml/marshal_test.go
@@ -652,12 +652,15 @@
XML: "<sub>unknown</sub>",
},
},
- UnmarshalOnly: true,
},
{
- Value: &AnyTest{Nested: "known", AnyField:
AnyHolder{XML: "<unknown/>"}},
- ExpectXML: `<a><nested><value>known</value></nested></a>`,
- MarshalOnly: true,
+ Value: &AnyTest{Nested: "known",
+ AnyField: AnyHolder{
+ XML: "<unknown/>",
+ XMLName: Name{Local: "AnyField"},
+ },
+ },
+ ExpectXML:
`<a><nested><value>known</value></nested><AnyField><unknown/></AnyField></a>`,
},

// Test recursive types.
@@ -690,15 +693,17 @@

// Test escaping.
{
- ExpectXML: `<a><nested><value>dquote: &#34;; squote: &#39;; ampersand:
&amp;; less: &lt;; greater: &gt;;</value></nested></a>`,
+ ExpectXML: `<a><nested><value>dquote: &#34;; squote: &#39;; ampersand:
&amp;; less: &lt;; greater: &gt;;</value></nested><empty></empty></a>`,
Value: &AnyTest{
- Nested: `dquote: "; squote: '; ampersand: &; less: <; greater: >;`,
+ Nested: `dquote: "; squote: '; ampersand: &; less: <; greater: >;`,
+ AnyField: AnyHolder{XMLName: Name{Local: "empty"}},
},
},
{
- ExpectXML: `<a><nested><value>newline: &#xA;; cr: &#xD;; tab:
&#x9;;</value></nested></a>`,
+ ExpectXML: `<a><nested><value>newline: &#xA;; cr: &#xD;; tab:
&#x9;;</value></nested><AnyField></AnyField></a>`,
Value: &AnyTest{
- Nested: "newline: \n; cr: \r; tab: \t;",
+ Nested: "newline: \n; cr: \r; tab: \t;",
+ AnyField: AnyHolder{XMLName: Name{Local: "AnyField"}},
},
},
{
Index: src/pkg/encoding/xml/read.go
===================================================================
--- a/src/pkg/encoding/xml/read.go
+++ b/src/pkg/encoding/xml/read.go
@@ -279,7 +279,7 @@
saveComment = finfo.value(sv)
}

- case fAny:
+ case fAny, fAny | fElement:
if !saveAny.IsValid() {
saveAny = finfo.value(sv)
}
Index: src/pkg/encoding/xml/typeinfo.go
===================================================================
--- a/src/pkg/encoding/xml/typeinfo.go
+++ b/src/pkg/encoding/xml/typeinfo.go
@@ -154,6 +154,9 @@
// This will also catch multiple modes in a single field.
valid = false
}
+ if finfo.flags&fMode == fAny {
+ finfo.flags |= fElement
+ }
if finfo.flags&fOmitEmpty != 0 && finfo.flags&(fElement|fAttr) == 0 {
valid = false
}

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 1 | next ›
Discussion Overview
groupgolang-dev @
categoriesgo
postedDec 16, '12 at 5:30a
activeDec 16, '12 at 5:30a
posts1
users1
websitegolang.org

1 user in discussion

Chris Jones Yar: 1 post

People

Translate

site design / logo © 2022 Grokbase