-
Notifications
You must be signed in to change notification settings - Fork 376
/
lazy_message.go
45 lines (34 loc) · 923 Bytes
/
lazy_message.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
package grpcutil
import (
"encoding/base64"
"github.com/gogo/protobuf/proto"
)
// LazyMessage is basically a no-op `proto.Message` used to pass
// serialized message through grpc
type LazyMessage struct {
buf []byte
}
func NewLazyMessage() *LazyMessage { return &LazyMessage{} }
func (m *LazyMessage) Base64() string {
return base64.StdEncoding.EncodeToString(m.buf)
}
func (m *LazyMessage) FromBase64(b64 string) (lm *LazyMessage, err error) {
m.buf, err = base64.StdEncoding.DecodeString(b64)
lm = m
return
}
func (m *LazyMessage) Bytes() []byte {
return m.buf
}
func (m *LazyMessage) FromBytes(buf []byte) *LazyMessage {
if buf == nil {
buf = []byte{}
}
m.buf = buf
return m
}
// Message
var _ proto.Message = (*LazyMessage)(nil)
func (m *LazyMessage) Reset() { *m = LazyMessage{} }
func (m *LazyMessage) String() string { return string(m.buf) }
func (m *LazyMessage) ProtoMessage() {}