From 2cd8eed765d75fd0ba3074de3a72b7c3b696968d Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Fri, 26 Jan 2018 09:24:34 +0100 Subject: [PATCH] Implement disco#items parsing and marshaling --- iq.go | 21 ++++++++++++++++++++- iq_test.go | 22 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/iq.go b/iq.go index 972f800..d41e7f6 100644 --- a/iq.go +++ b/iq.go @@ -300,7 +300,8 @@ func (*Node) IsIQPayload() {} // Disco 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 { @@ -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) func init() { 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:xmpp:iot:control set"] = reflect.TypeOf(iot.ControlSet{}) } diff --git a/iq_test.go b/iq_test.go index aaf3343..1993dd0 100644 --- a/iq_test.go +++ b/iq_test.go @@ -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 // marshal / unmarshal. There is no need to manage them on the manually // crafted structure.