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: case xml.StartElement:
level++ level++
if level <= 1 { if level <= 1 {
if iqExt := typeRegistry.GetIQExtension(tt.Name); iqExt != nil { if iqExt := TypeRegistry.GetIQExtension(tt.Name); iqExt != nil {
// Decode payload extension // Decode payload extension
err = d.DecodeElement(iqExt, &tt) err = d.DecodeElement(iqExt, &tt)
if err != nil { if err != nil {
@ -318,8 +318,8 @@ type DiscoItem struct {
} }
func init() { func init() {
typeRegistry.MapExtension(PKTIQ, xml.Name{NSDiscoInfo, "query"}, DiscoInfo{}) TypeRegistry.MapExtension(PKTIQ, xml.Name{NSDiscoInfo, "query"}, DiscoInfo{})
typeRegistry.MapExtension(PKTIQ, xml.Name{NSDiscoItems, "query"}, DiscoItems{}) 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:ietf:params:xml:ns:xmpp-bind", "bind"}, BindBind{})
typeRegistry.MapExtension(PKTIQ, xml.Name{"urn:xmpp:iot:control", "set"}, ControlSet{}) 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) { switch tt := t.(type) {
case xml.StartElement: case xml.StartElement:
if msgExt := typeRegistry.GetMsgExtension(tt.Name); msgExt != nil { if msgExt := TypeRegistry.GetMsgExtension(tt.Name); msgExt != nil {
// Decode message extension // Decode message extension
err = d.DecodeElement(msgExt, &tt) err = d.DecodeElement(msgExt, &tt)
if err != nil { if err != nil {

View file

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

View file

@ -15,5 +15,5 @@ type OOB struct {
} }
func init() { 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() { func init() {
typeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgReceipts, "request"}, ReceiptRequest{}) TypeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgReceipts, "request"}, ReceiptRequest{})
typeRegistry.MapExtension(PKTMessage, xml.Name{NSMsgReceipts, "received"}, ReceiptReceived{}) 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. // 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). // 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 ( const (
PKTPresence packetType = iota PKTPresence PacketType = iota
PKTMessage PKTMessage
PKTIQ PKTIQ
) )
var typeRegistry = newRegistry() var TypeRegistry = newRegistry()
// We store different registries per packet type and namespace. // We store different registries per packet type and namespace.
type registryKey struct { type registryKey struct {
packetType packetType packetType PacketType
namespace string namespace string
} }
@ -46,10 +46,10 @@ func newRegistry() *registry {
} }
// MapExtension stores extension type for packet payload. // 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 // You can use the alias "*" as local XML name to be able to match all unknown tag name for that
// packet type and namespace. // 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} key := registryKey{pktType, name.Space}
r.msgTypesLock.RLock() r.msgTypesLock.RLock()
store := r.msgTypes[key] 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. // 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} key := registryKey{pktType, name.Space}
r.msgTypesLock.RLock() r.msgTypesLock.RLock()