Add msg.Get method to match and extract message extensions
This commit is contained in:
parent
6ddfa781e5
commit
4a4c4850d1
32
message.go
32
message.go
|
@ -2,6 +2,7 @@ package xmpp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
@ -30,6 +31,37 @@ func NewMessage(a Attrs) Message {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get search and extracts a specific extension on a message.
|
||||||
|
// It receives a pointer to an MsgExtension. It will panic if the caller
|
||||||
|
// does not pass a pointer.
|
||||||
|
// It will return true if the passed extension is found and set the pointer
|
||||||
|
// to the extension passed as parameter to the found extension.
|
||||||
|
// It will return false if the extension is not found on the message.
|
||||||
|
//
|
||||||
|
// Example usage:
|
||||||
|
// var oob xmpp.OOB
|
||||||
|
// if ok := msg.Get(&oob); ok {
|
||||||
|
// // oob extension has been found
|
||||||
|
// }
|
||||||
|
func (msg *Message) Get(ext MsgExtension) bool {
|
||||||
|
target := reflect.ValueOf(ext)
|
||||||
|
if target.Kind() != reflect.Ptr {
|
||||||
|
panic("you must pass a pointer to the message Get method")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, e := range msg.Extensions {
|
||||||
|
if reflect.TypeOf(e) == target.Type() {
|
||||||
|
source := reflect.ValueOf(e)
|
||||||
|
if source.Kind() != reflect.Ptr {
|
||||||
|
source = source.Elem()
|
||||||
|
}
|
||||||
|
target.Elem().Set(source.Elem())
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
type messageDecoder struct{}
|
type messageDecoder struct{}
|
||||||
|
|
||||||
var message messageDecoder
|
var message messageDecoder
|
||||||
|
|
|
@ -47,3 +47,30 @@ func TestDecodeError(t *testing.T) {
|
||||||
t.Errorf("incorrect error type: %s", parsedMessage.Error.Type)
|
t.Errorf("incorrect error type: %s", parsedMessage.Error.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetOOB(t *testing.T) {
|
||||||
|
image := "https://localhost/image.png"
|
||||||
|
msg := xmpp.NewMessage(xmpp.Attrs{To: "test@localhost"})
|
||||||
|
ext := xmpp.OOB{
|
||||||
|
XMLName: xml.Name{Space: "jabber:x:oob", Local: "x"},
|
||||||
|
URL: image,
|
||||||
|
}
|
||||||
|
msg.Extensions = append(msg.Extensions, &ext)
|
||||||
|
|
||||||
|
// OOB can properly be found
|
||||||
|
var oob xmpp.OOB
|
||||||
|
// Try to find and
|
||||||
|
if ok := msg.Get(&oob); !ok {
|
||||||
|
t.Error("could not find oob extension")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if oob.URL != image {
|
||||||
|
t.Errorf("OOB URL was not properly extracted: ''%s", oob.URL)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Markable is not found
|
||||||
|
var m xmpp.Markable
|
||||||
|
if ok := msg.Get(&m); ok {
|
||||||
|
t.Error("we should not have found markable extension")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue