-
Notifications
You must be signed in to change notification settings - Fork 31
/
encoder.go
50 lines (42 loc) · 1.11 KB
/
encoder.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
package thrifter
import (
"github.com/thrift-iterator/go/general"
"github.com/thrift-iterator/go/protocol"
"github.com/thrift-iterator/go/spi"
"io"
"reflect"
)
type Encoder struct {
cfg *frozenConfig
stream spi.Stream
}
func (encoder *Encoder) Encode(val interface{}) error {
cfg := encoder.cfg
valType := reflect.TypeOf(val)
valEncoder := cfg.getGenEncoder(valType)
if valEncoder == nil {
valEncoder = cfg.encoderOf(valType)
cfg.addGenEncoder(valType, valEncoder)
}
valEncoder.Encode(val, encoder.stream)
encoder.stream.Flush()
if encoder.stream.Error() != nil {
return encoder.stream.Error()
}
return nil
}
func (encoder *Encoder) EncodeMessage(msg general.Message) error {
return encoder.Encode(msg)
}
func (encoder *Encoder) EncodeMessageHeader(msgHeader protocol.MessageHeader) error {
return encoder.Encode(msgHeader)
}
func (encoder *Encoder) EncodeMessageArguments(msgArgs general.Struct) error {
return encoder.Encode(msgArgs)
}
func (encoder *Encoder) Reset(writer io.Writer) {
encoder.stream.Reset(writer)
}
func (encoder *Encoder) Buffer() []byte {
return encoder.stream.Buffer()
}