-
Notifications
You must be signed in to change notification settings - Fork 31
/
config.go
293 lines (258 loc) · 7.52 KB
/
config.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package thrifter
import (
"encoding/json"
"errors"
"github.com/thrift-iterator/go/binding/codegen"
"github.com/thrift-iterator/go/binding/reflection"
"github.com/thrift-iterator/go/general"
"github.com/thrift-iterator/go/protocol"
"github.com/thrift-iterator/go/protocol/binary"
"github.com/thrift-iterator/go/protocol/compact"
"github.com/thrift-iterator/go/raw"
"github.com/thrift-iterator/go/spi"
"github.com/v2pro/wombat/generic"
"io"
"reflect"
"sync"
)
type frozenConfig struct {
extension spi.Extension
protocol Protocol
genDecoders sync.Map
genEncoders sync.Map
extDecoders sync.Map
extEncoders sync.Map
staticCodegen bool
}
func (cfg Config) AddExtension(extension spi.Extension) Config {
cfg.Extensions = append(cfg.Extensions, extension)
return cfg
}
func (cfg Config) Froze() API {
extensions := append(cfg.Extensions, &general.Extension{})
extensions = append(extensions, &raw.Extension{})
api := &frozenConfig{
extension: extensions,
protocol: cfg.Protocol,
staticCodegen: cfg.StaticCodegen,
}
api.extDecoders = sync.Map{}
api.genDecoders = sync.Map{}
api.extEncoders = sync.Map{}
api.genEncoders = sync.Map{}
return api
}
func (cfg *frozenConfig) addGenDecoder(cacheKey reflect.Type, decoder spi.ValDecoder) {
cfg.genDecoders.Store(cacheKey, decoder)
}
func (cfg *frozenConfig) addExtDecoder(cacheKey string, decoder spi.ValDecoder) {
cfg.extDecoders.Store(cacheKey, decoder)
}
func (cfg *frozenConfig) addGenEncoder(cacheKey reflect.Type, encoder spi.ValEncoder) {
cfg.genEncoders.Store(cacheKey, encoder)
}
func (cfg *frozenConfig) addExtEncoder(cacheKey string, encoder spi.ValEncoder) {
cfg.extEncoders.Store(cacheKey, encoder)
}
func (cfg *frozenConfig) PrepareDecoder(valType reflect.Type) {
cacheKey := valType.String()
if cfg.GetDecoder(cacheKey) != nil {
return
}
decoder := cfg.extension.DecoderOf(valType)
cfg.addExtDecoder(cacheKey, decoder)
cfg.addGenDecoder(valType, decoder)
}
func (cfg *frozenConfig) GetDecoder(cacheKey string) spi.ValDecoder {
decoder, found := cfg.extDecoders.Load(cacheKey)
if found {
return decoder.(spi.ValDecoder)
}
return nil
}
func (cfg *frozenConfig) getGenDecoder(cacheKey reflect.Type) spi.ValDecoder {
decoder, found := cfg.genDecoders.Load(cacheKey)
if found {
return decoder.(spi.ValDecoder)
}
return nil
}
func (cfg *frozenConfig) PrepareEncoder(valType reflect.Type) {
cacheKey := valType.String()
if cfg.GetEncoder(cacheKey) != nil {
return
}
encoder := cfg.extension.EncoderOf(valType)
cfg.addExtEncoder(cacheKey, encoder)
cfg.addGenEncoder(valType, encoder)
}
func (cfg *frozenConfig) GetEncoder(cacheKey string) spi.ValEncoder {
encoder, found := cfg.extEncoders.Load(cacheKey)
if found {
return encoder.(spi.ValEncoder)
}
return nil
}
func (cfg *frozenConfig) getGenEncoder(cacheKey reflect.Type) spi.ValEncoder {
encoder, found := cfg.genEncoders.Load(cacheKey)
if found {
return encoder.(spi.ValEncoder)
}
return nil
}
func (cfg *frozenConfig) NewStream(writer io.Writer, buf []byte) spi.Stream {
switch cfg.protocol {
case ProtocolBinary:
return binary.NewStream(cfg, writer, buf)
case ProtocolCompact:
return compact.NewStream(cfg, writer, buf)
}
panic("unsupported protocol")
}
func (cfg *frozenConfig) NewIterator(reader io.Reader, buf []byte) spi.Iterator {
switch cfg.protocol {
case ProtocolBinary:
return binary.NewIterator(cfg, reader, buf)
case ProtocolCompact:
return compact.NewIterator(cfg, reader, buf)
}
panic("unsupported protocol")
}
func (cfg *frozenConfig) WillDecodeFromBuffer(samples ...interface{}) {
if !cfg.staticCodegen {
panic("this config is using dynamic codegen, can not do static codegen")
}
for _, sample := range samples {
cfg.staticDecoderOf(reflect.TypeOf(sample))
}
}
func (cfg *frozenConfig) WillDecodeFromReader(samples ...interface{}) {
if !cfg.staticCodegen {
panic("this config is using dynamic codegen, can not do static codegen")
}
for _, sample := range samples {
cfg.staticDecoderOf(reflect.TypeOf(sample))
}
}
func (cfg *frozenConfig) WillEncode(samples ...interface{}) {
if !cfg.staticCodegen {
panic("this config is using dynamic codegen, can not do static codegen")
}
for _, sample := range samples {
cfg.staticEncoderOf(reflect.TypeOf(sample))
}
}
func (cfg *frozenConfig) decoderOf(valType reflect.Type) spi.ValDecoder {
if cfg.staticCodegen {
return cfg.staticDecoderOf(valType)
}
return reflection.DecoderOf(cfg.extension, valType)
}
func (cfg *frozenConfig) staticDecoderOf(valType reflect.Type) spi.ValDecoder {
iteratorType := reflect.TypeOf((*binary.Iterator)(nil))
if cfg.protocol == ProtocolCompact {
iteratorType = reflect.TypeOf((*compact.Iterator)(nil))
}
funcObj := generic.Expand(codegen.Decode,
"EXT", &codegen.Extension{Extension: cfg.extension},
"ST", iteratorType,
"DT", valType)
f := funcObj.(func(interface{}, interface{}))
return &funcDecoder{f}
}
func (cfg *frozenConfig) encoderOf(valType reflect.Type) spi.ValEncoder {
if cfg.staticCodegen {
return cfg.staticEncoderOf(valType)
}
return reflection.EncoderOf(cfg.extension, valType)
}
func (cfg *frozenConfig) staticEncoderOf(valType reflect.Type) spi.ValEncoder {
streamType := reflect.TypeOf((*binary.Stream)(nil))
if cfg.protocol == ProtocolCompact {
streamType = reflect.TypeOf((*compact.Stream)(nil))
}
funcObj := generic.Expand(codegen.Encode,
"EXT", &codegen.Extension{Extension: cfg.extension},
"ST", valType,
"DT", streamType)
f := funcObj.(func(interface{}, interface{}))
return &funcEncoder{f}
}
type funcDecoder struct {
f func(dst interface{}, src interface{})
}
func (decoder *funcDecoder) Decode(val interface{}, iter spi.Iterator) {
decoder.f(val, iter)
}
type funcEncoder struct {
f func(dst interface{}, src interface{})
}
func (encoder *funcEncoder) Encode(val interface{}, stream spi.Stream) {
encoder.f(stream, val)
}
func (encoder *funcEncoder) ThriftType() protocol.TType {
panic("funcEncoder is not composable")
}
func (cfg *frozenConfig) Unmarshal(buf []byte, val interface{}) error {
valType := reflect.TypeOf(val)
decoder := cfg.getGenDecoder(valType)
if decoder == nil {
decoder = cfg.decoderOf(valType)
cfg.addGenDecoder(valType, decoder)
}
if buf == nil {
return errors.New("empty input")
}
iter := cfg.NewIterator(nil, buf)
decoder.Decode(val, iter)
if iter.Error() != nil {
return iter.Error()
}
return nil
}
func (cfg *frozenConfig) Marshal(val interface{}) ([]byte, error) {
valType := reflect.TypeOf(val)
encoder := cfg.getGenEncoder(valType)
if encoder == nil {
encoder = cfg.encoderOf(valType)
cfg.addGenEncoder(valType, encoder)
}
stream := cfg.NewStream(nil, nil)
encoder.Encode(val, stream)
if stream.Error() != nil {
return nil, stream.Error()
}
buf := stream.Buffer()
return buf, nil
}
func (cfg *frozenConfig) NewDecoder(reader io.Reader, buf []byte) *Decoder {
return &Decoder{
cfg: cfg,
iter: cfg.NewIterator(reader, buf),
}
}
func (cfg *frozenConfig) NewEncoder(writer io.Writer) *Encoder {
return &Encoder{
cfg: cfg,
stream: cfg.NewStream(writer, nil),
}
}
func (cfg *frozenConfig) ToJSON(buf []byte) (string, error) {
msg, err := UnmarshalMessage(buf)
if err != nil {
return "", err
}
jsonEncoded, err := json.MarshalIndent(msg, "", " ")
if err != nil {
return "", err
}
return string(jsonEncoded), nil
}
func (cfg *frozenConfig) MarshalMessage(msg general.Message) ([]byte, error) {
return cfg.Marshal(msg)
}
func (cfg *frozenConfig) UnmarshalMessage(buf []byte) (general.Message, error) {
var msg general.Message
err := cfg.Unmarshal(buf, &msg)
return msg, err
}