blob: 771222999091601a903a9da198c7bb034fa27e65 [file] [log] [blame]
Prince Pereirac1c21d62021-04-22 08:38:15 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package thrift
21
22type TSerializer struct {
23 Transport *TMemoryBuffer
24 Protocol TProtocol
25}
26
27type TStruct interface {
28 Write(p TProtocol) error
29 Read(p TProtocol) error
30}
31
32func NewTSerializer() *TSerializer {
33 transport := NewTMemoryBufferLen(1024)
34 protocol := NewTBinaryProtocolFactoryDefault().GetProtocol(transport)
35
36 return &TSerializer{
37 transport,
38 protocol}
39}
40
41func (t *TSerializer) WriteString(msg TStruct) (s string, err error) {
42 t.Transport.Reset()
43
44 if err = msg.Write(t.Protocol); err != nil {
45 return
46 }
47
48 if err = t.Protocol.Flush(); err != nil {
49 return
50 }
51 if err = t.Transport.Flush(); err != nil {
52 return
53 }
54
55 return t.Transport.String(), nil
56}
57
58func (t *TSerializer) Write(msg TStruct) (b []byte, err error) {
59 t.Transport.Reset()
60
61 if err = msg.Write(t.Protocol); err != nil {
62 return
63 }
64
65 if err = t.Protocol.Flush(); err != nil {
66 return
67 }
68
69 if err = t.Transport.Flush(); err != nil {
70 return
71 }
72
73 b = append(b, t.Transport.Bytes()...)
74 return
75}