-
Notifications
You must be signed in to change notification settings - Fork 34
/
tsfm_html.go
57 lines (43 loc) · 1.27 KB
/
tsfm_html.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
package transformer
import (
"context"
"io"
"net/textproto"
"reflect"
"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/httpx"
"github.com/machinefi/w3bstream/pkg/depends/x/textx"
"github.com/machinefi/w3bstream/pkg/depends/x/typesx"
)
const HTMLTsfName = "text/html"
func init() { DefaultFactory.Register(&HTMLText{}) }
type HTMLText struct{}
func (t *HTMLText) String() string { return HTMLTsfName }
func (HTMLText) Names() []string { return []string{HTMLTsfName, "html"} }
func (HTMLText) NamedByTag() string { return "" }
func (HTMLText) New(context.Context, typesx.Type) (Transformer, error) { return &HTMLText{}, nil }
func (t *HTMLText) EncodeTo(ctx context.Context, w io.Writer, v interface{}) error {
rv, ok := v.(reflect.Value)
if !ok {
rv = reflect.ValueOf(v)
}
httpx.MaybeWriteHeader(ctx, w, t.String(), map[string]string{
"charset": "utf-8",
})
data, err := textx.MarshalText(rv, true)
if err != nil {
return err
}
_, err = w.Write(data)
return err
}
func (HTMLText) DecodeFrom(_ context.Context, r io.Reader, v interface{}, _ ...textproto.MIMEHeader) error {
rv, ok := v.(reflect.Value)
if !ok {
rv = reflect.ValueOf(v)
}
data, err := io.ReadAll(r)
if err != nil {
return err
}
return textx.UnmarshalText(rv, data, true)
}