-
Notifications
You must be signed in to change notification settings - Fork 199
/
capnpMarshalizer.go
47 lines (36 loc) · 1.08 KB
/
capnpMarshalizer.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
package marshal
import (
"bytes"
"github.com/ElrondNetwork/elrond-go/data"
)
// CapnpMarshalizer implements marshaling with capnproto
type CapnpMarshalizer struct {
}
// Marshal does the actual serialization of an object through capnproto
// The object to be serialized must implement the data.CapnpHelper interface
func (x *CapnpMarshalizer) Marshal(obj interface{}) ([]byte, error) {
out := bytes.NewBuffer(nil)
o := obj.(data.CapnpHelper)
// set the members to capnp struct
err := o.Save(out)
if err != nil {
return nil, err
}
return out.Bytes(), nil
}
// Unmarshal does the actual deserialization of an object through capnproto
// The object to be deserialized must implement the data.CapnpHelper interface
func (x *CapnpMarshalizer) Unmarshal(obj interface{}, buff []byte) error {
out := bytes.NewBuffer(buff)
o := obj.(data.CapnpHelper)
// set the members to capnp struct
err := o.Load(out)
return err
}
// IsInterfaceNil returns true if there is no value under the interface
func (x *CapnpMarshalizer) IsInterfaceNil() bool {
if x == nil {
return true
}
return false
}