/
protobuf.go
43 lines (35 loc) · 1.13 KB
/
protobuf.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
// Package protobuf contains a codec to encode and decode entities in Protocol Buffer
package protobuf
import (
"errors"
"github.com/asdine/storm/v3/codec/json"
"github.com/golang/protobuf/proto"
)
const name = "protobuf"
// More details on Protocol Buffers https://github.com/golang/protobuf
var (
Codec = new(protobufCodec)
errNotProtocolBufferMessage = errors.New("value isn't a Protocol Buffers Message")
)
type protobufCodec int
// Encode value with protocol buffer.
// If type isn't a Protocol buffer Message, json encoder will be used instead.
func (c protobufCodec) Marshal(v interface{}) ([]byte, error) {
message, ok := v.(proto.Message)
if !ok {
// toBytes() may need to encode non-protobuf type, if that occurs use json
return json.Codec.Marshal(v)
}
return proto.Marshal(message)
}
func (c protobufCodec) Unmarshal(b []byte, v interface{}) error {
message, ok := v.(proto.Message)
if !ok {
// toBytes() may have encoded non-protobuf type, if that occurs use json
return json.Codec.Unmarshal(b, v)
}
return proto.Unmarshal(b, message)
}
func (c protobufCodec) Name() string {
return name
}