mirror of
https://github.com/FluuxIO/go-xmpp.git
synced 2024-11-21 18:12:00 -08:00
Do not marshal 'empty' error elements
This commit is contained in:
parent
ad6e09a0f6
commit
266ed9b1e4
20
iq.go
20
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) {
|
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{
|
code := xml.Attr{
|
||||||
Name: xml.Name{Local: "code"},
|
Name: xml.Name{Local: "code"},
|
||||||
Value: strconv.Itoa(x.Code),
|
Value: strconv.Itoa(x.Code),
|
||||||
}
|
}
|
||||||
typ := xml.Attr{
|
start.Attr = append(start.Attr, code)
|
||||||
Name: xml.Name{Local: "type"},
|
|
||||||
Value: x.Type,
|
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)
|
err = e.EncodeToken(start)
|
||||||
|
|
||||||
// SubTags
|
// SubTags
|
||||||
|
@ -2,6 +2,7 @@ package xmpp // import "fluux.io/xmpp"
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
@ -51,6 +52,10 @@ func TestGenerateIq(t *testing.T) {
|
|||||||
t.Errorf("cannot marshal xml structure")
|
t.Errorf("cannot marshal xml structure")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.Contains(string(data), "<error ") {
|
||||||
|
t.Error("empty error should not be serialized")
|
||||||
|
}
|
||||||
|
|
||||||
parsedIQ := IQ{}
|
parsedIQ := IQ{}
|
||||||
if err = xml.Unmarshal(data, &parsedIQ); err != nil {
|
if err = xml.Unmarshal(data, &parsedIQ); err != nil {
|
||||||
t.Errorf("Unmarshal(%s) returned error", data)
|
t.Errorf("Unmarshal(%s) returned error", data)
|
||||||
|
Loading…
Reference in New Issue
Block a user