OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / go-wire / data / binary.go
1 package data
2
3 import (
4         "github.com/pkg/errors"
5         wire "github.com/tendermint/go-wire"
6 )
7
8 type binaryMapper struct {
9         base  interface{}
10         impls []wire.ConcreteType
11 }
12
13 func newBinaryMapper(base interface{}) *binaryMapper {
14         return &binaryMapper{
15                 base: base,
16         }
17 }
18
19 // registerImplementation allows you to register multiple concrete types.
20 //
21 // We call wire.RegisterInterface with the entire (growing list) each time,
22 // as we do not know when the end is near.
23 func (m *binaryMapper) registerImplementation(data interface{}, kind string, b byte) {
24         m.impls = append(m.impls, wire.ConcreteType{O: data, Byte: b})
25         wire.RegisterInterface(m.base, m.impls...)
26 }
27
28 // ToWire is a convenience method to serialize with go-wire
29 // error is there to keep the same interface as json, but always nil
30 func ToWire(o interface{}) ([]byte, error) {
31         return wire.BinaryBytes(o), nil
32 }
33
34 // FromWire is a convenience method to deserialize with go-wire
35 func FromWire(d []byte, o interface{}) error {
36         return errors.WithStack(
37                 wire.ReadBinaryBytes(d, o))
38 }