/
binary.go
38 lines (32 loc) · 1.01 KB
/
binary.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
package data
import (
"github.com/pkg/errors"
wire "github.com/tendermint/go-wire"
)
type binaryMapper struct {
base interface{}
impls []wire.ConcreteType
}
func newBinaryMapper(base interface{}) *binaryMapper {
return &binaryMapper{
base: base,
}
}
// registerImplementation allows you to register multiple concrete types.
//
// We call wire.RegisterInterface with the entire (growing list) each time,
// as we do not know when the end is near.
func (m *binaryMapper) registerImplementation(data interface{}, kind string, b byte) {
m.impls = append(m.impls, wire.ConcreteType{O: data, Byte: b})
wire.RegisterInterface(m.base, m.impls...)
}
// ToWire is a convenience method to serialize with go-wire
// error is there to keep the same interface as json, but always nil
func ToWire(o interface{}) ([]byte, error) {
return wire.BinaryBytes(o), nil
}
// FromWire is a convenience method to deserialize with go-wire
func FromWire(d []byte, o interface{}) error {
return errors.WithStack(
wire.ReadBinaryBytes(d, o))
}