Expose type registry for custom user-defined payload and extensions

This commit is contained in:
Mickael Remond 2019-06-05 10:23:18 +02:00
parent b93a3a2550
commit 6cdadc95e9
No known key found for this signature in database
GPG Key ID: E6F6045D79965AA3
6 changed files with 20 additions and 20 deletions

10
iq.go
View File

@ -204,7 +204,7 @@ func (iq *IQ) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
case xml.StartElement:
level++
if level <= 1 {
if iqExt := typeRegistry.GetIQExtension(tt.Name); iqExt != nil {
if iqExt := TypeRegistry.GetIQExtension(tt.Name); iqExt != nil {
// Decode payload extension
err = d.DecodeElement(iqExt, &tt)
if err != nil {
@ -318,8 +318,8 @@ type DiscoItem struct {
}
func init() {
typeRegistry.MapExtension(PKTIQ, xml.Name{NSDiscoInfo, "query"}, DiscoInfo{})
typeRegistry.MapExtension(PKTIQ, xml.Name{NSDiscoItems, "query"}, DiscoItems{})
typeRegistry.MapExtension(PKTIQ, xml.Name{"urn:ietf:params:xml:ns:xmpp-bind", "bind"}, BindBind{})
typeRegistry.MapExtension(PKTIQ, xml.Name{"urn:xmpp:iot:control", "set"}, ControlSet{})
TypeRegistry.MapExtension(PKTIQ, xml.Name{NSDiscoInfo, "query"}, DiscoInfo{})
TypeRegistry.MapExtension(PKTIQ, xml.Name{NSDiscoItems, "query"}, DiscoItems{})
TypeRegistry.MapExtension(PKTIQ, xml.Name{"urn:ietf:params:xml:ns:xmpp-bind", "bind"}, BindBind{})
TypeRegistry.MapExtension(PKTIQ, xml.Name{"urn:xmpp:iot:control", "set"}, ControlSet{})
}

View File

@ -86,7 +86,7 @@ func (msg *Message) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
switch tt := t.(type) {
case xml.StartElement:
if msgExt := typeRegistry.GetMsgExtension(tt.Name); msgExt != nil {
if msgExt := TypeRegistry.GetMsgExtension(tt.Name); msgExt != nil {
// Decode message extension
err = d.DecodeElement(msgExt, &tt)
if err != nil {

View File

@ -33,8 +33,8 @@ type MarkAcknowledged struct {
}
func init() {
typeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgChatMarkers, "markable"}, Markable{})
typeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgChatMarkers, "received"}, MarkReceived{})
typeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgChatMarkers, "displayed"}, MarkDisplayed{})
typeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgChatMarkers, "acknowledged"}, MarkAcknowledged{})
TypeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgChatMarkers, "markable"}, Markable{})
TypeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgChatMarkers, "received"}, MarkReceived{})
TypeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgChatMarkers, "displayed"}, MarkDisplayed{})
TypeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgChatMarkers, "acknowledged"}, MarkAcknowledged{})
}

View File

@ -15,5 +15,5 @@ type OOB struct {
}
func init() {
typeRegistry.MapExtension(PKTMessage, xml.Name{"jabber:x:oob", "x"}, OOB{})
TypeRegistry.MapExtension(PKTMessage, xml.Name{"jabber:x:oob", "x"}, OOB{})
}

View File

@ -22,6 +22,6 @@ type ReceiptReceived struct {
}
func init() {
typeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgReceipts, "request"}, ReceiptRequest{})
typeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgReceipts, "received"}, ReceiptReceived{})
TypeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgReceipts, "request"}, ReceiptRequest{})
TypeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgReceipts, "received"}, ReceiptReceived{})
}

View File

@ -13,19 +13,19 @@ type MsgExtension interface{}
// That should make it possible to be able to share the decoder.
// TODO: Ensure that a client can add its own custom namespace to the registry (or overload existing ones).
type packetType uint8
type PacketType uint8
const (
PKTPresence packetType = iota
PKTPresence PacketType = iota
PKTMessage
PKTIQ
)
var typeRegistry = newRegistry()
var TypeRegistry = newRegistry()
// We store different registries per packet type and namespace.
type registryKey struct {
packetType packetType
packetType PacketType
namespace string
}
@ -46,10 +46,10 @@ func newRegistry() *registry {
}
// MapExtension stores extension type for packet payload.
// The match is done per packetType (iq, message, or presence) and XML tag name.
// The match is done per PacketType (iq, message, or presence) and XML tag name.
// You can use the alias "*" as local XML name to be able to match all unknown tag name for that
// packet type and namespace.
func (r *registry) MapExtension(pktType packetType, name xml.Name, extension MsgExtension) {
func (r *registry) MapExtension(pktType PacketType, name xml.Name, extension MsgExtension) {
key := registryKey{pktType, name.Space}
r.msgTypesLock.RLock()
store := r.msgTypes[key]
@ -65,7 +65,7 @@ func (r *registry) MapExtension(pktType packetType, name xml.Name, extension Msg
}
// GetExtensionType returns extension type for packet payload, based on packet type and tag name.
func (r *registry) GetExtensionType(pktType packetType, name xml.Name) reflect.Type {
func (r *registry) GetExtensionType(pktType PacketType, name xml.Name) reflect.Type {
key := registryKey{pktType, name.Space}
r.msgTypesLock.RLock()