-
Notifications
You must be signed in to change notification settings - Fork 18
/
stream_example_test.go
162 lines (138 loc) · 3.03 KB
/
stream_example_test.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package molecule
import (
"bytes"
"encoding/binary"
"fmt"
)
func ExampleNew() {
/* Encoding the following:
*
* message SearchRequest {
* string query = 1;
* int32 page_number = 2;
* int32 result_per_page = 3;
* }
*/
var err error
output := bytes.NewBuffer([]byte{})
ps := NewProtoStream(output)
// values copied from the .proto file
const fieldQuery int = 1
const fieldPageNumber int = 2
const fieldResultPerPage int = 3
err = ps.String(fieldQuery, "q=streaming+protobufs")
if err != nil {
panic(err)
}
err = ps.Int32(fieldPageNumber, 2)
if err != nil {
panic(err)
}
err = ps.Int32(fieldResultPerPage, 100)
if err != nil {
panic(err)
}
// The encoded result is in `output.Bytes()`.
}
func ExampleProtoStream_Embedded() {
/* Encoding the following:
*
* message MultiSearch {
* string api_key = 10;
* repeated SearchRequest request = 11;
* }
*
* message SearchRequest {
* string query = 1;
* int32 page_number = 2;
* int32 result_per_page = 3;
* }
*/
var err error
output := bytes.NewBuffer([]byte{})
ps := NewProtoStream(output)
// values copied from the .proto file
const fieldAPIKey = 10
const fieldRequest = 11
const fieldQuery int = 1
const fieldPageNumber int = 2
const fieldResultPerPage int = 3
err = ps.String(fieldAPIKey, "abc-123")
if err != nil {
panic(err)
}
err = ps.Embedded(fieldRequest, func(ps *ProtoStream) error {
err = ps.String(fieldQuery, "author=octavia+butler")
if err != nil {
return err
}
err = ps.Int32(fieldPageNumber, 2)
if err != nil {
return err
}
err = ps.Int32(fieldResultPerPage, 100)
if err != nil {
return err
}
return nil
})
if err != nil {
panic(err)
}
err = ps.Embedded(fieldRequest, func(ps *ProtoStream) error {
err = ps.String(fieldQuery, "author=margaret+atwood")
if err != nil {
return err
}
err = ps.Int32(fieldPageNumber, 0)
if err != nil {
return err
}
err = ps.Int32(fieldResultPerPage, 10)
if err != nil {
return err
}
return nil
})
if err != nil {
panic(err)
}
// The encoded result is in `output.Bytes()`.
}
func ExampleProtoStream_Sint32Packed() {
/* Encoding the following:
*
* message Numbers {
* repeated int32 number = 22;
* }
*/
var err error
output := bytes.NewBuffer([]byte{})
ps := NewProtoStream(output)
const fieldNumber = 22
numbers := []int32{20, -30, -31, 1999}
err = ps.Sint32Packed(fieldNumber, numbers)
if err != nil {
panic(err)
}
res := bytes.NewReader(output.Bytes())
key, _ := binary.ReadUvarint(res)
fmt.Printf("key: 0x%x = 22<<3 + 2\n", key)
leng, _ := binary.ReadUvarint(res)
fmt.Printf("length: 0x%x\n", leng)
v, _ := binary.ReadUvarint(res)
fmt.Printf("v[0]: 0x%x\n", v)
v, _ = binary.ReadUvarint(res)
fmt.Printf("v[1]: 0x%x\n", v)
v, _ = binary.ReadUvarint(res)
fmt.Printf("v[2]: 0x%x\n", v)
v, _ = binary.ReadUvarint(res)
fmt.Printf("v[3]: 0x%x\n", v)
// Output:
// key: 0xb2 = 22<<3 + 2
// length: 0x5
// v[0]: 0x28
// v[1]: 0x3b
// v[2]: 0x3d
// v[3]: 0xf9e
}