/
serialize.go
91 lines (73 loc) · 1.75 KB
/
serialize.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
package main
import (
"fmt"
"git.apache.org/thrift.git/lib/go/thrift"
"github.com/glycerine/golang-thrift-minimal-example/vendor/tutorial"
)
func main() {
binary()
compact()
}
func binary() {
fmt.Printf("\n ==== demo Thrift Binary serialization ====\n")
t := thrift.NewTMemoryBufferLen(1024)
p := thrift.NewTBinaryProtocolFactoryDefault().GetProtocol(t)
tser := &thrift.TSerializer{
Transport: t,
Protocol: p,
}
str := "hi there"
a := &tutorial.Work{
Num1: 12,
Num2: 24,
Comment: &str,
}
by, err := tser.Write(a)
panicOn(err)
fmt.Printf("by = '%v', length %v\n", string(by), len(by))
t2 := thrift.NewTMemoryBufferLen(1024)
p2 := thrift.NewTBinaryProtocolFactoryDefault().GetProtocol(t2)
deser := &thrift.TDeserializer{
Transport: t2,
Protocol: p2,
}
b := tutorial.NewWork()
deser.Transport.Close() // resets underlying bytes.Buffer
err = deser.Read(b, by)
panicOn(err)
fmt.Printf("b = '%#v'\n", b)
}
func compact() {
fmt.Printf("\n ==== demo Thrift Compact Binary serialization ====\n")
t := thrift.NewTMemoryBufferLen(1024)
p := thrift.NewTCompactProtocolFactory().GetProtocol(t)
tser := &thrift.TSerializer{
Transport: t,
Protocol: p,
}
str := "hi there"
a := &tutorial.Work{
Num1: 12,
Num2: 24,
Comment: &str,
}
by, err := tser.Write(a)
panicOn(err)
fmt.Printf("by = '%v', length %v\n", string(by), len(by))
t2 := thrift.NewTMemoryBufferLen(1024)
p2 := thrift.NewTCompactProtocolFactory().GetProtocol(t2)
deser := &thrift.TDeserializer{
Transport: t2,
Protocol: p2,
}
b := tutorial.NewWork()
deser.Transport.Close() // resets underlying bytes.Buffer
err = deser.Read(b, by)
panicOn(err)
fmt.Printf("b = '%#v'\n", b)
}
func panicOn(err error) {
if err != nil {
panic(err)
}
}