-
Notifications
You must be signed in to change notification settings - Fork 11
/
xml.go
66 lines (54 loc) · 1.32 KB
/
xml.go
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
package xml
import (
"io"
"github.com/clbanning/mxj"
"github.com/luraproject/lura/v2/encoding"
"golang.org/x/net/html/charset"
)
// Register registers the xml decoder
func Register() error {
return encoding.GetRegister().Register(Name, NewDecoder)
}
// Name is the key for the xml encoding
const Name = "xml"
// NewDecoder return the right XML decoder
func NewDecoder(isCollection bool) func(io.Reader, *map[string]interface{}) error {
if isCollection {
return CollectionDecoder
}
return Decoder
}
// Decoder implements the Decoder interface
func Decoder(r io.Reader, v *map[string]interface{}) error {
mxj.XmlCharsetReader = charset.NewReaderLabel
mv, err := mxj.NewMapXmlReader(xmlReader{r: r})
if err != nil {
return err
}
*v = mv
return nil
}
// CollectionDecoder implements the Decoder interface over a collection
func CollectionDecoder(r io.Reader, v *map[string]interface{}) error {
mxj.XmlCharsetReader = charset.NewReaderLabel
mv, err := mxj.NewMapXmlReader(xmlReader{r: r})
if err != nil {
return err
}
*(v) = map[string]interface{}{"collection": mv}
return nil
}
type xmlReader struct {
r io.Reader
}
func (x xmlReader) Read(p []byte) (n int, err error) {
n, err = x.r.Read(p)
if err != io.EOF {
return n, err
}
if len(p) == n {
return n, nil
}
p[n] = ([]byte("\n"))[0]
return n + 1, err
}