Make it possible to extract unknown iq payload, in field Any
This commit is contained in:
parent
1542110f1b
commit
def9629a0b
10
iq.go
10
iq.go
|
@ -23,7 +23,8 @@ type IQ struct { // Info/Query
|
|||
// request."
|
||||
Payload IQPayload `xml:",omitempty"`
|
||||
Error Err `xml:"error,omitempty"`
|
||||
RawXML string `xml:",innerxml"`
|
||||
// Any is used to decode unknown payload as a generique structure
|
||||
Any *Node `xml:",any"`
|
||||
}
|
||||
|
||||
type IQPayload interface {
|
||||
|
@ -114,7 +115,12 @@ func (iq *IQ) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|||
continue
|
||||
}
|
||||
// TODO: If unknown decode as generic node
|
||||
return fmt.Errorf("unexpected element in iq: %s %s", tt.Name.Space, tt.Name.Local)
|
||||
node := new(Node)
|
||||
err = d.DecodeElement(node, &tt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
iq.Any = node
|
||||
case xml.EndElement:
|
||||
if tt == start.End() {
|
||||
return nil
|
||||
|
|
16
iq_test.go
16
iq_test.go
|
@ -153,3 +153,19 @@ func TestPayloadWithError(t *testing.T) {
|
|||
t.Errorf("incorrect error value: '%s'", parsedIQ.Error.Reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownPayload(t *testing.T) {
|
||||
iq := `<iq type="get" to="service.localhost" id="1" >
|
||||
<query xmlns="unknown:ns"/>
|
||||
</iq>`
|
||||
parsedIQ := xmpp.IQ{}
|
||||
err := xml.Unmarshal([]byte(iq), &parsedIQ)
|
||||
if err != nil {
|
||||
t.Errorf("Unmarshal error: %#v (%s)", err, iq)
|
||||
return
|
||||
}
|
||||
|
||||
if parsedIQ.Any.XMLName.Space != "unknown:ns" {
|
||||
t.Errorf("could not extract namespace: '%s'", parsedIQ.Any.XMLName.Space)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue