Implement disco#items parsing and marshaling

This commit is contained in:
Mickael Remond 2018-01-26 09:24:34 +01:00
parent 3e6cf2c8b0
commit 2cd8eed765
No known key found for this signature in database
GPG key ID: E6F6045D79965AA3
2 changed files with 42 additions and 1 deletions

21
iq.go
View file

@ -300,7 +300,8 @@ func (*Node) IsIQPayload() {}
// Disco // Disco
const ( const (
NSDiscoInfo = "http://jabber.org/protocol/disco#info" NSDiscoInfo = "http://jabber.org/protocol/disco#info"
NSDiscoItems = "http://jabber.org/protocol/disco#items"
) )
type DiscoInfo struct { type DiscoInfo struct {
@ -325,10 +326,28 @@ type Feature struct {
// ============================================================================ // ============================================================================
type DiscoItems struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#items query"`
Node string `xml:"node,attr,omitempty"`
Items []DiscoItem `xml:"item"`
}
func (*DiscoItems) IsIQPayload() {}
type DiscoItem struct {
XMLName xml.Name `xml:"item"`
Name string `xml:"name,attr,omitempty"`
JID string `xml:"jid,attr,omitempty"`
Node string `xml:"node,attr,omitempty"`
}
// ============================================================================
var typeRegistry = make(map[string]reflect.Type) var typeRegistry = make(map[string]reflect.Type)
func init() { func init() {
typeRegistry["http://jabber.org/protocol/disco#info query"] = reflect.TypeOf(DiscoInfo{}) typeRegistry["http://jabber.org/protocol/disco#info query"] = reflect.TypeOf(DiscoInfo{})
typeRegistry["http://jabber.org/protocol/disco#items query"] = reflect.TypeOf(DiscoItems{})
typeRegistry["urn:ietf:params:xml:ns:xmpp-bind bind"] = reflect.TypeOf(BindBind{}) typeRegistry["urn:ietf:params:xml:ns:xmpp-bind bind"] = reflect.TypeOf(BindBind{})
typeRegistry["urn:xmpp:iot:control set"] = reflect.TypeOf(iot.ControlSet{}) typeRegistry["urn:xmpp:iot:control set"] = reflect.TypeOf(iot.ControlSet{})
} }

View file

@ -85,6 +85,28 @@ func TestErrorTag(t *testing.T) {
} }
} }
func TestDiscoItems(t *testing.T) {
iq := NewIQ("get", "romeo@montague.net/orchard", "catalog.shakespeare.lit", "items3", "en")
payload := DiscoItems{
Node: "music",
}
iq.AddPayload(&payload)
data, err := xml.Marshal(iq)
if err != nil {
t.Errorf("cannot marshal xml structure")
}
parsedIQ := IQ{}
if err = xml.Unmarshal(data, &parsedIQ); err != nil {
t.Errorf("Unmarshal(%s) returned error", data)
}
if !xmlEqual(parsedIQ.Payload, iq.Payload) {
t.Errorf("non matching items\n%s", cmp.Diff(parsedIQ.Payload, iq.Payload))
}
}
// Compare iq structure but ignore empty namespace as they are set properly on // Compare iq structure but ignore empty namespace as they are set properly on
// marshal / unmarshal. There is no need to manage them on the manually // marshal / unmarshal. There is no need to manage them on the manually
// crafted structure. // crafted structure.