-
Notifications
You must be signed in to change notification settings - Fork 31
/
decoder.go
51 lines (44 loc) · 1.16 KB
/
decoder.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
package thrifter
import (
"github.com/thrift-iterator/go/general"
"github.com/thrift-iterator/go/protocol"
"github.com/thrift-iterator/go/spi"
"io"
"reflect"
)
type Decoder struct {
cfg *frozenConfig
iter spi.Iterator
}
func (decoder *Decoder) Decode(val interface{}) error {
cfg := decoder.cfg
valType := reflect.TypeOf(val)
valDecoder := cfg.getGenDecoder(valType)
if valDecoder == nil {
valDecoder = cfg.decoderOf(valType)
cfg.addGenDecoder(valType, valDecoder)
}
valDecoder.Decode(val, decoder.iter)
if decoder.iter.Error() != nil {
return decoder.iter.Error()
}
return nil
}
func (decoder *Decoder) DecodeMessage() (general.Message, error) {
var msg general.Message
err := decoder.Decode(&msg)
return msg, err
}
func (decoder *Decoder) DecodeMessageHeader() (protocol.MessageHeader, error) {
var msgHeader protocol.MessageHeader
err := decoder.Decode(&msgHeader)
return msgHeader, err
}
func (decoder *Decoder) DecodeMessageArguments() (general.Struct, error) {
var msgArgs general.Struct
err := decoder.Decode(&msgArgs)
return msgArgs, err
}
func (decoder *Decoder) Reset(reader io.Reader, buf []byte) {
decoder.iter.Reset(reader, buf)
}