-
Notifications
You must be signed in to change notification settings - Fork 800
/
thrift_util.go
117 lines (97 loc) · 3.44 KB
/
thrift_util.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package common
import (
"reflect"
"github.com/apache/thrift/lib/go/thrift"
)
// TSerialize is used to serialize thrift TStruct to []byte
func TSerialize(msg thrift.TStruct) (b []byte, err error) {
return thrift.NewTSerializer().Write(msg)
}
// TSerializeString is used to serialize thrift TStruct to string
func TSerializeString(msg thrift.TStruct) (s string, err error) {
return thrift.NewTSerializer().WriteString(msg)
}
// TListSerialize is used to serialize list of thrift TStruct to []byte
func TListSerialize(msgs []thrift.TStruct) (b []byte, err error) {
if msgs == nil {
return
}
t := thrift.NewTSerializer()
t.Transport.Reset()
if e := t.Protocol.WriteListBegin(thrift.STRING, len(msgs)); e != nil {
err = thrift.PrependError("error writing list begin: ", e)
return
}
for _, v := range msgs {
if e := v.Write(t.Protocol); e != nil {
err = thrift.PrependError("error writing TStruct: ", e)
return
}
}
if e := t.Protocol.WriteListEnd(); e != nil {
err = thrift.PrependError("error writing list end: ", e)
return
}
if err = t.Protocol.Flush(); err != nil {
return
}
if err = t.Transport.Flush(); err != nil {
return
}
b = append(b, t.Transport.Bytes()...)
return
}
// TDeserialize is used to deserialize []byte to thrift TStruct
func TDeserialize(msg thrift.TStruct, b []byte) (err error) {
return thrift.NewTDeserializer().Read(msg, b)
}
// TDeserializeString is used to deserialize string to thrift TStruct
func TDeserializeString(msg thrift.TStruct, s string) (err error) {
return thrift.NewTDeserializer().ReadString(msg, s)
}
// TListDeserialize is used to deserialize []byte to list of thrift TStruct
func TListDeserialize(msgType reflect.Type, b []byte) (msgs []thrift.TStruct, err error) {
t := thrift.NewTDeserializer()
err = nil
if _, err = t.Transport.Write(b); err != nil {
return
}
_, size, e := t.Protocol.ReadListBegin()
if e != nil {
err = thrift.PrependError("error reading list begin: ", e)
return
}
msgs = make([]thrift.TStruct, 0, size)
for i := 0; i < size; i++ {
msg := reflect.New(msgType).Interface().(thrift.TStruct)
if e := msg.Read(t.Protocol); e != nil {
err = thrift.PrependError("error reading TStruct: ", e)
return
}
msgs = append(msgs, msg)
}
if e := t.Protocol.ReadListEnd(); e != nil {
err = thrift.PrependError("error reading list end: ", e)
return
}
return
}