2018-09-13 11:17:32 -07:00
|
|
|
package codec
|
2018-03-02 01:24:07 -08:00
|
|
|
|
|
|
|
import (
|
2020-01-24 07:32:00 -08:00
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
2018-04-09 10:32:19 -07:00
|
|
|
|
2020-05-05 07:28:20 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec/types"
|
|
|
|
|
2020-01-24 07:32:00 -08:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-03-02 01:24:07 -08:00
|
|
|
)
|
|
|
|
|
2020-01-24 07:32:00 -08:00
|
|
|
type (
|
|
|
|
// Marshaler defines the interface module codecs must implement in order to support
|
|
|
|
// backwards compatibility with Amino while allowing custom Protobuf-based
|
|
|
|
// serialization. Note, Amino can still be used without any dependency on
|
|
|
|
// Protobuf. There are three typical implementations that fulfill this contract:
|
|
|
|
//
|
|
|
|
// 1. AminoCodec: Provides full Amino serialization compatibility.
|
|
|
|
// 2. ProtoCodec: Provides full Protobuf serialization compatibility.
|
|
|
|
Marshaler interface {
|
2020-07-24 12:04:29 -07:00
|
|
|
BinaryMarshaler
|
|
|
|
JSONMarshaler
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryMarshaler interface {
|
2020-01-24 07:32:00 -08:00
|
|
|
MarshalBinaryBare(o ProtoMarshaler) ([]byte, error)
|
|
|
|
MustMarshalBinaryBare(o ProtoMarshaler) []byte
|
|
|
|
|
|
|
|
MarshalBinaryLengthPrefixed(o ProtoMarshaler) ([]byte, error)
|
|
|
|
MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte
|
|
|
|
|
|
|
|
UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error
|
|
|
|
MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler)
|
|
|
|
|
|
|
|
UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) error
|
|
|
|
MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler)
|
|
|
|
|
2020-05-05 07:28:20 -07:00
|
|
|
types.AnyUnpacker
|
2020-02-06 11:21:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
JSONMarshaler interface {
|
2020-04-29 19:36:34 -07:00
|
|
|
MarshalJSON(o interface{}) ([]byte, error)
|
2020-01-24 07:32:00 -08:00
|
|
|
MustMarshalJSON(o interface{}) []byte
|
|
|
|
|
2020-04-29 19:36:34 -07:00
|
|
|
UnmarshalJSON(bz []byte, ptr interface{}) error
|
2020-01-24 07:32:00 -08:00
|
|
|
MustUnmarshalJSON(bz []byte, ptr interface{})
|
2018-04-09 10:32:19 -07:00
|
|
|
}
|
|
|
|
|
2020-01-24 07:32:00 -08:00
|
|
|
// ProtoMarshaler defines an interface a type must implement as protocol buffer
|
|
|
|
// defined message.
|
|
|
|
ProtoMarshaler interface {
|
|
|
|
proto.Message // for JSON serialization
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2020-01-24 07:32:00 -08:00
|
|
|
Marshal() ([]byte, error)
|
|
|
|
MarshalTo(data []byte) (n int, err error)
|
|
|
|
MarshalToSizedBuffer(dAtA []byte) (int, error)
|
|
|
|
Size() int
|
|
|
|
Unmarshal(data []byte) error
|
2019-06-08 13:55:47 -07:00
|
|
|
}
|
2020-01-24 07:32:00 -08:00
|
|
|
)
|
2019-06-08 13:55:47 -07:00
|
|
|
|
2020-01-24 07:32:00 -08:00
|
|
|
func encodeUvarint(w io.Writer, u uint64) (err error) {
|
|
|
|
var buf [10]byte
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2020-01-24 07:32:00 -08:00
|
|
|
n := binary.PutUvarint(buf[:], u)
|
|
|
|
_, err = w.Write(buf[0:n])
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2020-01-24 07:32:00 -08:00
|
|
|
return err
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|