-
Notifications
You must be signed in to change notification settings - Fork 460
/
Copy pathprotobuilder.go
73 lines (62 loc) · 1.33 KB
/
protobuilder.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
package youtube
import (
"bytes"
"encoding/base64"
"net/url"
)
type ProtoBuilder struct {
byteBuffer bytes.Buffer
}
func (pb *ProtoBuilder) ToBytes() []byte {
return pb.byteBuffer.Bytes()
}
func (pb *ProtoBuilder) ToURLEncodedBase64() string {
b64 := base64.URLEncoding.EncodeToString(pb.ToBytes())
return url.QueryEscape(b64)
}
func (pb *ProtoBuilder) writeVarint(val int64) error {
if val == 0 {
_, err := pb.byteBuffer.Write([]byte{0})
return err
}
for {
b := byte(val & 0x7F)
val >>= 7
if val != 0 {
b |= 0x80
}
_, err := pb.byteBuffer.Write([]byte{b})
if err != nil {
return err
}
if val == 0 {
break
}
}
return nil
}
func (pb *ProtoBuilder) field(field int, wireType byte) error {
val := int64(field<<3) | int64(wireType&0x07)
return pb.writeVarint(val)
}
func (pb *ProtoBuilder) Varint(field int, val int64) error {
err := pb.field(field, 0)
if err != nil {
return err
}
return pb.writeVarint(val)
}
func (pb *ProtoBuilder) String(field int, stringVal string) error {
strBts := []byte(stringVal)
return pb.Bytes(field, strBts)
}
func (pb *ProtoBuilder) Bytes(field int, bytesVal []byte) error {
if err := pb.field(field, 2); err != nil {
return err
}
if err := pb.writeVarint(int64(len(bytesVal))); err != nil {
return err
}
_, err := pb.byteBuffer.Write(bytesVal)
return err
}