From 266ed9b1e40b5f50ca543624f5e59db73eca4a33 Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Fri, 26 Jan 2018 11:16:04 +0100 Subject: [PATCH] Do not marshal 'empty' error elements --- iq.go | 20 +++++++++++++++----- iq_test.go | 5 +++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/iq.go b/iq.go index d41e7f6..fe09d50 100644 --- a/iq.go +++ b/iq.go @@ -77,16 +77,26 @@ func (x *Err) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { } func (x Err) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) { + if x.Code == 0 { + return nil + } + + // Encode start element and attributes + start.Name = xml.Name{Local: "error"} + code := xml.Attr{ Name: xml.Name{Local: "code"}, Value: strconv.Itoa(x.Code), } - typ := xml.Attr{ - Name: xml.Name{Local: "type"}, - Value: x.Type, + start.Attr = append(start.Attr, code) + + if len(x.Type) > 0 { + typ := xml.Attr{ + Name: xml.Name{Local: "type"}, + Value: x.Type, + } + start.Attr = append(start.Attr, typ) } - start.Name = xml.Name{Local: "error"} - start.Attr = append(start.Attr, code, typ) err = e.EncodeToken(start) // SubTags diff --git a/iq_test.go b/iq_test.go index 1993dd0..439a862 100644 --- a/iq_test.go +++ b/iq_test.go @@ -2,6 +2,7 @@ package xmpp // import "fluux.io/xmpp" import ( "encoding/xml" + "strings" "testing" "github.com/google/go-cmp/cmp" @@ -51,6 +52,10 @@ func TestGenerateIq(t *testing.T) { t.Errorf("cannot marshal xml structure") } + if strings.Contains(string(data), "