-
Notifications
You must be signed in to change notification settings - Fork 202
/
protobufMock.go
36 lines (30 loc) · 966 Bytes
/
protobufMock.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
package mock
import (
"errors"
"github.com/gogo/protobuf/proto"
)
// ProtobufMarshalizerMock implements marshaling with protobuf
type ProtobufMarshalizerMock struct {
}
// Marshal does the actual serialization of an object through protobuf
func (x *ProtobufMarshalizerMock) Marshal(obj interface{}) ([]byte, error) {
if msg, ok := obj.(proto.Message); ok {
enc, err := proto.Marshal(msg)
if err != nil {
return nil, err
}
return enc, nil
}
return nil, errors.New("can not serialize the object")
}
// Unmarshal does the actual deserialization of an object through protobuf
func (x *ProtobufMarshalizerMock) Unmarshal(obj interface{}, buff []byte) error {
if msg, ok := obj.(proto.Message); ok {
return proto.Unmarshal(buff, msg)
}
return errors.New("obj does not implement proto.Message")
}
// IsInterfaceNil returns true if there is no value under the interface
func (x *ProtobufMarshalizerMock) IsInterfaceNil() bool {
return x == nil
}