-
Notifications
You must be signed in to change notification settings - Fork 34
/
tsfm_xml.go
54 lines (42 loc) · 1.17 KB
/
tsfm_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
package transformer
import (
"context"
"encoding/xml"
"io"
"net/textproto"
"reflect"
"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/httpx"
"github.com/machinefi/w3bstream/pkg/depends/x/typesx"
)
func init() { DefaultFactory.Register(&XML{}) }
type XML struct{}
func (XML) Names() []string { return []string{httpx.MIME_XML, "xml"} }
func (t *XML) String() string { return httpx.MIME_XML }
func (XML) NamedByTag() string { return "xml" }
func (XML) New(context.Context, typesx.Type) (Transformer, error) {
return &XML{}, nil
}
func (t *XML) EncodeTo(ctx context.Context, w io.Writer, v interface{}) error {
if rv, ok := v.(reflect.Value); ok {
v = rv.Interface()
}
httpx.MaybeWriteHeader(ctx, w, t.String(), map[string]string{
"charset": "utf-8",
})
return xml.NewEncoder(w).Encode(v)
}
func (XML) DecodeFrom(_ context.Context, r io.Reader, v interface{}, _ ...textproto.MIMEHeader) error {
if rv, ok := v.(reflect.Value); ok {
if rv.Kind() != reflect.Ptr && rv.CanAddr() {
rv = rv.Addr()
}
v = rv.Interface()
}
d := xml.NewDecoder(r)
err := d.Decode(v)
if err != nil {
// TODO resolve field path by InputOffset()
return err
}
return nil
}