-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric.go
More file actions
71 lines (64 loc) · 1.59 KB
/
generic.go
File metadata and controls
71 lines (64 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package xmpp
import "encoding/xml"
// A Generic represents an unknown XML element
type Generic struct {
XMLName xml.Name
Attrs map[string]string `xml:"-"`
Text string `xml:",chardata"`
Container
}
// UnmarshalXML implements XML decoding
func (g *Generic) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
type RawGeneric Generic
type comboType struct {
RawGeneric
Proxies []proxy `xml:",any"`
}
combo := &comboType{}
if err := dec.DecodeElement(combo, &start); err != nil {
panic(err)
}
*g = Generic(combo.RawGeneric)
g.XMLName = start.Name
g.Children = proxyToInterface(combo.Proxies)
g.Attrs = xmlAttrToMap(start.Attr)
return nil
}
// MarshalXML implements XML encoding
func (g *Generic) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
type RawGeneric Generic
type combo struct {
RawGeneric
Children []interface{} `xml:",any"`
}
raw := &combo{}
raw.RawGeneric = RawGeneric(*g)
start.Name = g.XMLName
start.Attr = mapToXMLAttr(g.Attrs)
raw.Children = g.Container.Children
return enc.EncodeElement(raw, start)
}
func proxyToInterface(p []proxy) []interface{} {
list := make([]interface{}, 0)
for _, i := range p {
list = append(list, i.Object)
}
return list
}
func xmlAttrToMap(attrs []xml.Attr) (res map[string]string) {
res = make(map[string]string)
for _, attr := range attrs {
res[attr.Name.Local] = attr.Value
}
return
}
func mapToXMLAttr(m map[string]string) (attrs []xml.Attr) {
attrs = make([]xml.Attr, 0)
for k, v := range m {
attrs = append(attrs, xml.Attr{
Name: xml.Name{Local: k},
Value: v,
})
}
return
}