Making a little more use of XMLName for marshaling instead of having a
authorChris Jones <chris@cjones.org>
Sun, 01 Jan 2012 17:27:21 -0700
changeset 43 82e90aa25dad
parent 42 f6bb47ca12f2
child 44 dd6f5cc27d52
Making a little more use of XMLName for marshaling instead of having a custom MarshalXML function.
structs.go
--- a/structs.go	Sun Jan 01 17:22:48 2012 -0700
+++ b/structs.go	Sun Jan 01 17:27:21 2012 -0700
@@ -153,12 +153,12 @@
 
 // Describes an XMPP stanza error. See RFC 3920, Section 9.3.
 type Error struct {
+	XMLName xml.Name `xml:"error"`
 	// The error type attribute.
 	Type string `xml:"attr"`
 	// Any nested element, if present.
 	Any *Generic
 }
-var _ xml.Marshaler = &Error{}
 var _ os.Error = &Error{}
 
 // Used for resource binding as a nested element inside <iq/>.
@@ -249,9 +249,6 @@
 	return buf.Bytes(), nil
 }
 
-// BUG(cjyar) We can probably eliminate this if we use an
-// appropriately-tagged XMLName field.
-
 func (e *errText) MarshalXML() ([]byte, os.Error) {
 	buf := bytes.NewBuffer(nil)
 	buf.WriteString("<text")
@@ -318,23 +315,10 @@
 	return buf.Bytes(), nil
 }
 
-// BUG(cjyar) We can probably eliminate this if we add an XMLName
-// field with an appropriate tag.
-func (er *Error) MarshalXML() ([]byte, os.Error) {
+func (er *Error) String() string {
 	buf := bytes.NewBuffer(nil)
-	buf.WriteString("<error")
-	writeField(buf, "type", er.Type)
-	buf.WriteString(">")
-	if er.Any != nil {
-		xml.Marshal(buf, er.Any)
-	}
-	buf.WriteString("</error>")
-	return buf.Bytes(), nil
-}
-
-func (er *Error) String() string {
-	bytes, _ := er.MarshalXML()
-	return string(bytes)
+	xml.Marshal(buf, er)
+	return buf.String()
 }
 
 func (m *Message) GetName() string {