~samwhited/xmpp

8e54a4f783c78ade3051f8244f111341fc97fc33 — Sam Whited 1 year, 11 months ago 93053ec
mux: add IQ option types that take a func

Previously there were only option shortcuts for regular handlers and not
for the function adapter. This adds more shortcuts for the function
adapter types to make registering handlers with inline functions easier.
2 files changed, 29 insertions(+), 4 deletions(-)

M mux/iq.go
M mux/iq_test.go
M mux/iq.go => mux/iq.go +25 -0
@@ 160,11 160,21 @@ func GetIQ(n xml.Name, h IQHandler) IQOption {
	return HandleIQ(stanza.GetIQ, n, h)
}

// GetIQFunc is a shortcut for HandleIQFunc with the type set to "get".
func GetIQFunc(n xml.Name, h IQHandlerFunc) IQOption {
	return GetIQ(n, h)
}

// SetIQ is a shortcut for HandleIQ with the type set to "set".
func SetIQ(n xml.Name, h IQHandler) IQOption {
	return HandleIQ(stanza.SetIQ, n, h)
}

// SetIQFunc is a shortcut for HandleIQ with the type set to "set".
func SetIQFunc(n xml.Name, h IQHandlerFunc) IQOption {
	return SetIQ(n, h)
}

// ErrorIQ is a shortcut for HandleIQ with the type set to "error" and a
// wildcard XML name.
//


@@ 177,6 187,14 @@ func ErrorIQ(h IQHandler) IQOption {
	return HandleIQ(stanza.ErrorIQ, xml.Name{}, h)
}

// ErrorIQFunc is a shortcut for HandleIQFunc with the type set to "error" and a
// wildcard XML name.
//
// For more information, see ErrorIQ.
func ErrorIQFunc(h IQHandlerFunc) IQOption {
	return ErrorIQ(h)
}

// ResultIQ is a shortcut for HandleIQ with the type set to "result".
//
// Unlike IQs of type get, set, and error, result type IQs may or may not


@@ 187,6 205,13 @@ func ResultIQ(n xml.Name, h IQHandler) IQOption {
	return HandleIQ(stanza.ResultIQ, n, h)
}

// ResultIQFunc is a shortcut for HandleIQFunc with the type set to "result".
//
// For more information, see ResultIQ.
func ResultIQFunc(n xml.Name, h IQHandlerFunc) IQOption {
	return ResultIQ(n, h)
}

// HandleIQFunc returns an option that matches the IQ payload by XML name and IQ
// type.
func HandleIQFunc(iqType stanza.IQType, n xml.Name, h IQHandlerFunc) IQOption {

M mux/iq_test.go => mux/iq_test.go +4 -4
@@ 47,7 47,7 @@ var iqTestCases = [...]struct {
		// If no exact match is available, fallback to the namespace wildcard
		// handler.
		m: mux.NewIQMux(
			mux.HandleIQ(stanza.GetIQ, xml.Name{Local: "test", Space: ""}, passIQHandler),
			mux.GetIQFunc(xml.Name{Local: "test", Space: ""}, passIQHandler),
			mux.HandleIQ(stanza.GetIQ, xml.Name{Local: "", Space: exampleNS}, failIQHandler),
		),
		p: xml.Name{Local: "test", Space: exampleNS},


@@ 64,8 64,8 @@ var iqTestCases = [...]struct {
		// If no exact match or localname/namespace wildcard is available, fallback
		// to just matching on type alone.
		m: mux.NewIQMux(
			mux.ResultIQ(xml.Name{Local: "test", Space: exampleNS}, failIQHandler),
			mux.ErrorIQ(passIQHandler),
			mux.ResultIQFunc(xml.Name{Local: "test", Space: exampleNS}, failIQHandler),
			mux.ErrorIQFunc(passIQHandler),
		),
		p:      xml.Name{Local: "test", Space: exampleNS},
		iqType: stanza.ErrorIQ,


@@ 74,7 74,7 @@ var iqTestCases = [...]struct {
		// IQs must be routed correctly by type.
		m: mux.NewIQMux(
			mux.GetIQ(xml.Name{Local: "test", Space: exampleNS}, failIQHandler),
			mux.SetIQ(xml.Name{Local: "test", Space: exampleNS}, failIQHandler),
			mux.SetIQFunc(xml.Name{Local: "test", Space: exampleNS}, failIQHandler),
			mux.ResultIQ(xml.Name{Local: "test", Space: exampleNS}, passIQHandler),
			mux.ErrorIQ(passIQHandler),
		),