~samwhited/xmpp

6bc1589f83d9e12e39ff28c26689323761969294 — Sam Whited 4 years ago 25d92cd
Make all stanza types satisfy fmt.Stringer

Add "IQ" back to the end of the IQ types
10 files changed, 107 insertions(+), 59 deletions(-)

M bind.go
M doc.go
M errors_test.go
D errortype_string.go
M iq.go
M iq_test.go
D iqtype_string.go
M message_test.go
A presence_test.go
A stanzatype_string.go
M bind.go => bind.go +2 -2
@@ 78,9 78,9 @@ func BindResource() StreamFeature {
					// TODO: Do we actually care about this? Should this be a stanza error
					// instead?
					return mask, UndefinedCondition
				case resp.Type == Result:
				case resp.Type == ResultIQ:
					panic("Bind result processing not yet implemented")
				case resp.Type == Error:
				case resp.Type == ErrorIQ:
					panic("Bind error processing not yet implemented")
				}
				return mask, nil

M doc.go => doc.go +1 -2
@@ 8,5 8,4 @@
// Be advised: This API is still unstable and is subject to change.
package xmpp // import "mellium.im/xmpp"

//go:generate stringer -type=iqType
//go:generate stringer -type=errorType
//go:generate stringer -type=iqType,errorType,messageType,presenceType -output stanzatype_string.go

M errors_test.go => errors_test.go +10 -2
@@ 4,5 4,13 @@

package xmpp

var _ error = (*StanzaError)(nil)
var _ error = StanzaError{}
import (
	"fmt"
)

var (
	_ error        = (*StanzaError)(nil)
	_ error        = StanzaError{}
	_ fmt.Stringer = (*errorType)(nil)
	_ fmt.Stringer = Auth
)

D errortype_string.go => errortype_string.go +0 -16
@@ 1,16 0,0 @@
// Code generated by "stringer -type=errorType"; DO NOT EDIT

package xmpp

import "fmt"

const _errorType_name = "AuthCancelContinueModifyWait"

var _errorType_index = [...]uint8{0, 4, 10, 18, 24, 28}

func (i errorType) String() string {
	if i < 0 || i >= errorType(len(_errorType_index)-1) {
		return fmt.Sprintf("errorType(%d)", i)
	}
	return _errorType_name[_errorType_index[i]:_errorType_index[i+1]]
}

M iq.go => iq.go +14 -13
@@ 23,35 23,36 @@ type IQ struct {
type iqType int

const (
	// A Get IQ is used to query another entity for information.
	Get iqType = iota
	// A GetIQ is used to query another entity for information.
	GetIQ iqType = iota

	// A Set IQ is used to provide data to another entity, set new values, and
	// A SetIQ is used to provide data to another entity, set new values, and
	// replace existing values.
	Set
	SetIQ

	// A Result IQ is sent in response to a successful get or set IQ.
	Result
	// A ResultIQ is sent in response to a successful get or set IQ.
	ResultIQ

	// An Error IQ is sent to report that an error occured during the delivery or
	// An ErrorIQ is sent to report that an error occured during the delivery or
	// processing of a get or set IQ.
	Error
	ErrorIQ
)

func (t iqType) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
	return xml.Attr{Name: name, Value: strings.ToLower(t.String())}, nil
	s := t.String()
	return xml.Attr{Name: name, Value: strings.ToLower(s[:len(s)-2])}, nil
}

func (t *iqType) UnmarshalXMLAttr(attr xml.Attr) error {
	switch attr.Value {
	case "get":
		*t = Get
		*t = GetIQ
	case "set":
		*t = Set
		*t = SetIQ
	case "result":
		*t = Result
		*t = ResultIQ
	case "error":
		*t = Error
		*t = ErrorIQ
	default:
		// TODO: This should be a stanza error with the bad-request condition.
		return errors.New("bad-request")

M iq_test.go => iq_test.go +11 -8
@@ 6,13 6,16 @@ package xmpp

import (
	"encoding/xml"
	"fmt"
	"testing"
)

var (
	_ xml.MarshalerAttr   = (*iqType)(nil)
	_ xml.MarshalerAttr   = Get
	_ xml.UnmarshalerAttr = (*iqType)(nil)
	_ xml.MarshalerAttr   = (*iqType)(nil)
	_ xml.MarshalerAttr   = GetIQ
	_ fmt.Stringer        = (*iqType)(nil)
	_ fmt.Stringer        = GetIQ
)

func TestMarshalIQTypeAttr(t *testing.T) {


@@ 20,7 23,7 @@ func TestMarshalIQTypeAttr(t *testing.T) {
	for _, test := range []struct {
		iqtype iqType
		value  string
	}{{Get, "get"}, {Set, "set"}, {Result, "result"}, {Error, "error"}} {
	}{{GetIQ, "get"}, {SetIQ, "set"}, {ResultIQ, "result"}, {ErrorIQ, "error"}} {
		attr, err := test.iqtype.MarshalXMLAttr(n)
		if err != nil {
			t.Error(err)


@@ 41,11 44,11 @@ func TestUnmarshalIQTypeAttr(t *testing.T) {
		iqtype iqType
		err    bool
	}{
		{xml.Attr{Name: xml.Name{}, Value: "get"}, Get, false},
		{xml.Attr{Name: xml.Name{Space: "", Local: "type"}, Value: "set"}, Set, false},
		{xml.Attr{Name: xml.Name{Space: "urn", Local: "loc"}, Value: "result"}, Result, false},
		{xml.Attr{Name: xml.Name{}, Value: "error"}, Error, false},
		{xml.Attr{Name: xml.Name{}, Value: "stuff"}, Error, true},
		{xml.Attr{Name: xml.Name{}, Value: "get"}, GetIQ, false},
		{xml.Attr{Name: xml.Name{Space: "", Local: "type"}, Value: "set"}, SetIQ, false},
		{xml.Attr{Name: xml.Name{Space: "urn", Local: "loc"}, Value: "result"}, ResultIQ, false},
		{xml.Attr{Name: xml.Name{}, Value: "error"}, ErrorIQ, false},
		{xml.Attr{Name: xml.Name{}, Value: "stuff"}, ErrorIQ, true},
	} {
		iqtype := iqType(0)
		switch err := (&iqtype).UnmarshalXMLAttr(test.attr); {

D iqtype_string.go => iqtype_string.go +0 -16
@@ 1,16 0,0 @@
// Code generated by "stringer -type=iqType"; DO NOT EDIT

package xmpp

import "fmt"

const _iqType_name = "GetSetResultError"

var _iqType_index = [...]uint8{0, 3, 6, 12, 17}

func (i iqType) String() string {
	if i < 0 || i >= iqType(len(_iqType_index)-1) {
		return fmt.Sprintf("iqType(%d)", i)
	}
	return _iqType_name[_iqType_index[i]:_iqType_index[i+1]]
}

M message_test.go => message_test.go +6 -0
@@ 6,11 6,17 @@ package xmpp

import (
	"encoding/xml"
	"fmt"
	"testing"

	"mellium.im/xmpp/jid"
)

var (
	_ fmt.Stringer = (*messageType)(nil)
	_ fmt.Stringer = NormalMessage
)

// TODO: Make this a table test and add some more complicated messages.
// TODO: How should we test marshalling? Probably don't want to assume that
//       attribute order will remain stable.

A presence_test.go => presence_test.go +14 -0
@@ 0,0 1,14 @@
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp

import (
	"fmt"
)

var (
	_ fmt.Stringer = (*presenceType)(nil)
	_ fmt.Stringer = ProbePresence
)

A stanzatype_string.go => stanzatype_string.go +49 -0
@@ 0,0 1,49 @@
// Code generated by "stringer -type=iqType,errorType,messageType,presenceType -output stanzatype_string.go"; DO NOT EDIT

package xmpp

import "fmt"

const _iqType_name = "GetIQSetIQResultIQErrorIQ"

var _iqType_index = [...]uint8{0, 5, 10, 18, 25}

func (i iqType) String() string {
	if i < 0 || i >= iqType(len(_iqType_index)-1) {
		return fmt.Sprintf("iqType(%d)", i)
	}
	return _iqType_name[_iqType_index[i]:_iqType_index[i+1]]
}

const _errorType_name = "AuthCancelContinueModifyWait"

var _errorType_index = [...]uint8{0, 4, 10, 18, 24, 28}

func (i errorType) String() string {
	if i < 0 || i >= errorType(len(_errorType_index)-1) {
		return fmt.Sprintf("errorType(%d)", i)
	}
	return _errorType_name[_errorType_index[i]:_errorType_index[i+1]]
}

const _messageType_name = "NormalMessageChatMessageErrorMessageGroupChatMessageHeadlineMessage"

var _messageType_index = [...]uint8{0, 13, 24, 36, 52, 67}

func (i messageType) String() string {
	if i < 0 || i >= messageType(len(_messageType_index)-1) {
		return fmt.Sprintf("messageType(%d)", i)
	}
	return _messageType_name[_messageType_index[i]:_messageType_index[i+1]]
}

const _presenceType_name = "NoTypePresenceErrorPresenceProbePresenceSubscribePresenceSubscribedPresenceUnavailablePresenceUnsubscribePresenceUnsubscribedPresence"

var _presenceType_index = [...]uint8{0, 14, 27, 40, 57, 75, 94, 113, 133}

func (i presenceType) String() string {
	if i < 0 || i >= presenceType(len(_presenceType_index)-1) {
		return fmt.Sprintf("presenceType(%d)", i)
	}
	return _presenceType_name[_presenceType_index[i]:_presenceType_index[i+1]]
}