-
Notifications
You must be signed in to change notification settings - Fork 34
/
tsfm.go
173 lines (148 loc) · 3.65 KB
/
tsfm.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package transformer
import (
"context"
"io"
"net/textproto"
"net/url"
"reflect"
"sort"
"github.com/pkg/errors"
"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
"github.com/machinefi/w3bstream/pkg/depends/x/mapx"
"github.com/machinefi/w3bstream/pkg/depends/x/typesx"
)
type Transformer interface {
Names() []string
New(context.Context, typesx.Type) (Transformer, error)
EncodeTo(context.Context, io.Writer, any) error
DecodeFrom(context.Context, io.Reader, any, ...textproto.MIMEHeader) error
}
type Option struct {
Name string
MIME string
CommonOption
}
func (o Option) String() string {
values := url.Values{}
if o.Name != "" {
values.Add("Name", o.Name)
}
if o.MIME != "" {
values.Add("MIME", o.MIME)
}
if o.Omitempty {
values.Add("Omitempty", "true")
}
if o.Explode {
values.Add("Explode", "true")
}
return values.Encode()
}
type CommonOption struct {
Omitempty bool // Omitempty should be ignored when value is empty
Explode bool // Explode for raw uint8/byte slice/array
}
type TsfmAndOption struct {
Transformer
Option Option
}
type Factory interface {
NewTransformer(context.Context, typesx.Type, Option) (Transformer, error)
}
type ckFactory struct{}
func ContextWithFactory(ctx context.Context, f Factory) context.Context {
return contextx.WithValue(ctx, ckFactory{}, f)
}
func FactoryFromContext(ctx context.Context) Factory {
if f, ok := ctx.Value(ckFactory{}).(Factory); ok {
return f
}
return DefaultFactory
}
func NewTransformer(ctx context.Context, t typesx.Type, opt Option) (Transformer, error) {
return FactoryFromContext(ctx).NewTransformer(ctx, t, opt)
}
type factory struct {
set map[string]Transformer
cache *mapx.Map[string, Transformer]
}
var DefaultFactory = &factory{}
func NewFactory() *factory {
return &factory{
set: make(map[string]Transformer),
cache: mapx.New[string, Transformer](),
}
}
func (f *factory) Register(tsfms ...Transformer) {
if f.set == nil {
f.set = map[string]Transformer{}
}
if f.cache == nil {
f.cache = mapx.New[string, Transformer]()
}
for _, t := range tsfms {
for _, name := range t.Names() {
f.set[name] = t
}
}
}
func (f *factory) NewTransformer(ctx context.Context, t typesx.Type, opt Option) (Transformer, error) {
if ctx == nil {
ctx = context.Background()
}
key := typesx.FullTypeName(t) + opt.String()
if v, ok := f.cache.Load(key); ok {
return v.(Transformer), nil
}
if opt.MIME == "" {
indirect := typesx.DeRef(t)
switch indirect.Kind() {
case reflect.Slice:
elem := indirect.Elem()
if elem.PkgPath() == "" && elem.Kind() == reflect.Uint8 {
opt.MIME = "plain" // bytes
} else {
opt.MIME = "json"
}
case reflect.Struct:
// *mime/multipart.FileHeader
if indirect.PkgPath() == "mime/multipart" && indirect.Name() == "FileHeader" {
opt.MIME = "octet-stream"
} else {
opt.MIME = "json"
}
case reflect.Map, reflect.Array:
opt.MIME = "json"
default:
opt.MIME = "plain"
}
if _, ok := typesx.EncodingTextMarshalerTypeReplacer(t); ok {
opt.MIME = "plain"
}
}
if ct, ok := f.set[opt.MIME]; ok {
tsf, err := ct.New(ContextWithFactory(ctx, f), t)
if err != nil {
return nil, err
}
f.cache.Store(key, tsf)
return tsf, nil
}
return nil, errors.Errorf("fmt %s is not supported for content transformer", key)
}
// Transformers returns all tsfms registered to DefaultFactory
func Transformers() (ret []string) {
names := make(map[string]bool)
for _, tf := range DefaultFactory.set {
name := tf.Names()[0]
if s, ok := tf.(CanString); ok {
name = s.String()
}
if _, ok := names[name]; !ok {
names[name] = true
ret = append(ret, name)
}
}
sort.Strings(ret)
return ret
}