Makes parsing of inner IQ XML generic

This commit is contained in:
Mickael Remond 2018-01-13 19:27:46 +01:00
parent e14f58d9a9
commit 94815de173
No known key found for this signature in database
GPG Key ID: E6F6045D79965AA3
2 changed files with 13 additions and 10 deletions

View File

@ -19,9 +19,11 @@ func main() {
switch p := packet.(type) {
case xmpp.IQ:
switch p.Payload.(type) {
case *xmpp.Query:
fmt.Println("Received query:", p.Type)
switch inner := p.Payload.(type) {
case *xmpp.Node:
fmt.Println("Node:", inner.XMLName.Space, inner.XMLName.Local)
default:
fmt.Println("default")
}
default:
fmt.Println("Packet unhandled packet:", packet)

15
iq.go
View File

@ -73,11 +73,10 @@ func (iq *IQ) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
switch tt.Name.Space + " " + tt.Name.Local {
case "urn:ietf:params:xml:ns:xmpp-bind bind":
p = new(bindBind)
case "http://jabber.org/protocol/disco#items query":
p = new(Query)
case "urn:xmpp:iot:control set":
p = new(iot.ControlSet)
// TODO: Add a default Type that passes RawXML
default:
p = new(Node)
}
if p != nil {
err = d.DecodeElement(p, &tt)
@ -118,10 +117,12 @@ func (iq *IQ) XMPPFormat() string {
}
// ============================================================================
// IQ Query subelement
// Genery IQ Node
type Query struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#items query"`
type Node struct {
XMLName xml.Name
Content []byte `xml:",innerxml"`
Nodes []Node `xml:",any"`
}
func (*Query) IsIQPayload() {}
func (*Node) IsIQPayload() {}