-
Notifications
You must be signed in to change notification settings - Fork 0
/
gogoProtoMarshalizer.go
36 lines (29 loc) · 1019 Bytes
/
gogoProtoMarshalizer.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 marshal
import (
"fmt"
)
var _ Marshalizer = (*GogoProtoMarshalizer)(nil)
// GogoProtoMarshalizer implements marshaling with protobuf
type GogoProtoMarshalizer struct {
}
// Marshal does the actual serialization of an object
// The object to be serialized must implement the gogoProtoObj interface
func (x *GogoProtoMarshalizer) Marshal(obj interface{}) ([]byte, error) {
if msg, ok := obj.(GogoProtoObj); ok {
return msg.Marshal()
}
return nil, fmt.Errorf("%T, %w", obj, ErrMarshallingProto)
}
// Unmarshal does the actual deserialization of an object
// The object to be deserialized must implement the gogoProtoObj interface
func (x *GogoProtoMarshalizer) Unmarshal(obj interface{}, buff []byte) error {
if msg, ok := obj.(GogoProtoObj); ok {
msg.Reset()
return msg.Unmarshal(buff)
}
return fmt.Errorf("%T, %w", obj, ErrUnmarshallingProto)
}
// IsInterfaceNil returns true if there is no value under the interface
func (x *GogoProtoMarshalizer) IsInterfaceNil() bool {
return x == nil
}