-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontainer.go
61 lines (49 loc) · 1.21 KB
/
container.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
package boltdb
import (
"bytes"
"fmt"
"reflect"
"encoding/gob"
gogoproto "github.com/gogo/protobuf/proto"
"github.com/golang/protobuf/proto"
)
type container struct {
T string
V []byte
}
func cMarshal(m proto.Message) ([]byte, error) {
typeName := gogoproto.MessageName(m)
bs, err := gogoproto.Marshal(m)
if err != nil {
return nil, fmt.Errorf("failed to marshal proto message: %v", err)
}
c := container{T: typeName, V: bs}
b := bytes.Buffer{}
g := gob.NewEncoder(&b)
err = g.Encode(c)
if err != nil {
return nil, fmt.Errorf("failed to marshal container: %v", err)
}
return b.Bytes(), nil
}
func cUnmarshal(b []byte) (proto.Message, error) {
r := bytes.NewReader(b)
g := gob.NewDecoder(r)
var c container
err := g.Decode(&c)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal container: %v", err)
}
protoType := gogoproto.MessageType(c.T)
if protoType == nil {
return nil, fmt.Errorf("unknown proto message type %s", c.T)
}
t := protoType.Elem()
intPtr := reflect.New(t)
instance := intPtr.Interface().(gogoproto.Message)
err = proto.Unmarshal(c.V, instance)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal proto message: %v", err)
}
return instance, nil
}