~samwhited/xmpp

6ea225b4ebd2b67a0449a16932214d310ef0000c — Sam Whited 3 years ago 12cf040
Update usage of xmlstream.WriterTo
5 files changed, 38 insertions(+), 24 deletions(-)

M Gopkg.toml
M session.go
M stanza/error.go
M stanza/error_test.go
M stream/error.go
M Gopkg.toml => Gopkg.toml +1 -1
@@ 33,5 33,5 @@
  name = "mellium.im/sasl"

[[constraint]]
	version = "0.9"
	version = "0.10"
  name = "mellium.im/xmlstream"

M session.go => session.go +2 -2
@@ 189,7 189,7 @@ func (s *Session) Serve(h Handler) error {
func (s *Session) sendError(err error) (e error) {
	switch typErr := err.(type) {
	case stream.Error:
		if e = typErr.WriteXML(s, xml.StartElement{}); e != nil {
		if _, e = typErr.WriteXML(s); e != nil {
			return e
		}
		if e = s.Close(); e != nil {


@@ 203,7 203,7 @@ func (s *Session) sendError(err error) (e error) {
	//     The error condition is not one of those defined by the other
	//     conditions in this list; this error condition SHOULD NOT be used
	//     except in conjunction with an application-specific condition.
	if e = stream.UndefinedCondition.WriteXML(s, xml.StartElement{}); e != nil {
	if _, e = stream.UndefinedCondition.WriteXML(s); e != nil {
		return e
	}
	return err

M stanza/error.go => stanza/error.go +24 -11
@@ 265,8 265,9 @@ func (se Error) Error() string {
	return string(se.Condition)
}

// WriteXML is like MarshalXML except it writes tokens to w.
func (se Error) WriteXML(w xmlstream.TokenWriter, _ xml.StartElement) (err error) {
// WriteXML satisfies the xmlstream.WriterTo interface.
// It is like MarshalXML except it writes tokens to w.
func (se Error) WriteXML(w xmlstream.TokenWriter) (n int, err error) {
	start := xml.StartElement{
		Name: xml.Name{Space: ``, Local: "error"},
		Attr: []xml.Attr{},


@@ 279,17 280,20 @@ func (se Error) WriteXML(w xmlstream.TokenWriter, _ xml.StartElement) (err error
		start.Attr = append(start.Attr, a)
	}
	if err = w.EncodeToken(start); err != nil {
		return err
		return n, err
	}
	n++
	condition := xml.StartElement{
		Name: xml.Name{Space: ns.Stanza, Local: string(se.Condition)},
	}
	if err = w.EncodeToken(condition); err != nil {
		return err
		return n, err
	}
	n++
	if err = w.EncodeToken(condition.End()); err != nil {
		return err
		return n, err
	}
	n++
	if se.Text != "" {
		text := xml.StartElement{
			Name: xml.Name{Space: ns.Stanza, Local: "text"},


@@ 301,21 305,30 @@ func (se Error) WriteXML(w xmlstream.TokenWriter, _ xml.StartElement) (err error
			},
		}
		if err = w.EncodeToken(text); err != nil {
			return err
			return n, err
		}
		n++
		if err = w.EncodeToken(xml.CharData(se.Text)); err != nil {
			return err
			return n, err
		}
		n++
		if err = w.EncodeToken(text.End()); err != nil {
			return err
			return n, err
		}
		n++
	}
	err = w.EncodeToken(start.End())
	if err != nil {
		return n, err
	}
	return w.EncodeToken(start.End())
	n++
	return n, err
}

// MarshalXML satisfies the xml.Marshaler interface for Error.
func (se Error) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	return se.WriteXML(e, start)
func (se Error) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
	_, err := se.WriteXML(e)
	return err
}

// UnmarshalXML satisfies the xml.Unmarshaler interface for StanzaError.

M stanza/error_test.go => stanza/error_test.go +4 -4
@@ 15,10 15,10 @@ import (
)

var (
	_ error               = (*Error)(nil)
	_ error               = Error{}
	_ xmlstream.Marshaler = (*Error)(nil)
	_ xmlstream.Marshaler = Error{}
	_ error              = (*Error)(nil)
	_ error              = Error{}
	_ xmlstream.WriterTo = (*Error)(nil)
	_ xmlstream.WriterTo = Error{}
)

func TestErrorReturnsCondition(t *testing.T) {

M stream/error.go => stream/error.go +7 -6
@@ 221,17 221,18 @@ func (s *Error) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
// MarshalXML satisfies the xml package's Marshaler interface and allows
// StreamError's to be correctly marshaled back into XML.
func (s Error) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
	return s.WriteXML(e, xml.StartElement{})
	_, err := s.WriteXML(e)
	return err
}

// WriteXML satisfies the xmlstream.Marshaler interface.
// WriteXML satisfies the xmlstream.WriterTo interface.
// It is like MarshalXML except it writes tokens to w.
func (s Error) WriteXML(w xmlstream.TokenWriter, _ xml.StartElement) error {
	_, err := xmlstream.Copy(w, s.TokenReader(nil))
func (s Error) WriteXML(w xmlstream.TokenWriter) (n int, err error) {
	n, err = xmlstream.Copy(w, s.TokenReader(nil))
	if err != nil {
		return err
		return n, err
	}
	return w.Flush()
	return n, w.Flush()
}

// TokenReader returns a new xmlstream.TokenReader that returns an encoding of