2020-01-24 07:32:00 -08:00
|
|
|
package codec
|
|
|
|
|
2020-12-08 01:27:08 -08:00
|
|
|
import (
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
)
|
2020-08-26 02:39:38 -07:00
|
|
|
|
2020-05-19 13:17:29 -07:00
|
|
|
// AminoCodec defines a codec that utilizes Codec for both binary and JSON
|
2020-01-24 07:32:00 -08:00
|
|
|
// encoding.
|
|
|
|
type AminoCodec struct {
|
2020-08-10 12:41:21 -07:00
|
|
|
*LegacyAmino
|
2020-05-06 13:47:03 -07:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
var _ Codec = &AminoCodec{}
|
2020-05-06 13:47:03 -07:00
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
// NewAminoCodec returns a reference to a new AminoCodec
|
2020-08-10 12:41:21 -07:00
|
|
|
func NewAminoCodec(codec *LegacyAmino) *AminoCodec {
|
|
|
|
return &AminoCodec{LegacyAmino: codec}
|
2020-05-06 13:47:03 -07:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
// Marshal implements BinaryMarshaler.Marshal method.
|
|
|
|
func (ac *AminoCodec) Marshal(o ProtoMarshaler) ([]byte, error) {
|
|
|
|
return ac.LegacyAmino.Marshal(o)
|
2020-01-24 07:32:00 -08:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
// MustMarshal implements BinaryMarshaler.MustMarshal method.
|
|
|
|
func (ac *AminoCodec) MustMarshal(o ProtoMarshaler) []byte {
|
|
|
|
return ac.LegacyAmino.MustMarshal(o)
|
2020-01-24 07:32:00 -08:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
// MarshalLengthPrefixed implements BinaryMarshaler.MarshalLengthPrefixed method.
|
|
|
|
func (ac *AminoCodec) MarshalLengthPrefixed(o ProtoMarshaler) ([]byte, error) {
|
|
|
|
return ac.LegacyAmino.MarshalLengthPrefixed(o)
|
2020-01-24 07:32:00 -08:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
// MustMarshalLengthPrefixed implements BinaryMarshaler.MustMarshalLengthPrefixed method.
|
|
|
|
func (ac *AminoCodec) MustMarshalLengthPrefixed(o ProtoMarshaler) []byte {
|
|
|
|
return ac.LegacyAmino.MustMarshalLengthPrefixed(o)
|
2020-01-24 07:32:00 -08:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
// Unmarshal implements BinaryMarshaler.Unmarshal method.
|
|
|
|
func (ac *AminoCodec) Unmarshal(bz []byte, ptr ProtoMarshaler) error {
|
|
|
|
return ac.LegacyAmino.Unmarshal(bz, ptr)
|
2020-01-24 07:32:00 -08:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
// MustUnmarshal implements BinaryMarshaler.MustUnmarshal method.
|
|
|
|
func (ac *AminoCodec) MustUnmarshal(bz []byte, ptr ProtoMarshaler) {
|
|
|
|
ac.LegacyAmino.MustUnmarshal(bz, ptr)
|
2020-01-24 07:32:00 -08:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
// UnmarshalLengthPrefixed implements BinaryMarshaler.UnmarshalLengthPrefixed method.
|
|
|
|
func (ac *AminoCodec) UnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) error {
|
|
|
|
return ac.LegacyAmino.UnmarshalLengthPrefixed(bz, ptr)
|
2020-01-24 07:32:00 -08:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
// MustUnmarshalLengthPrefixed implements BinaryMarshaler.MustUnmarshalLengthPrefixed method.
|
|
|
|
func (ac *AminoCodec) MustUnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) {
|
|
|
|
ac.LegacyAmino.MustUnmarshalLengthPrefixed(bz, ptr)
|
2020-05-05 07:28:20 -07:00
|
|
|
}
|
2020-08-26 02:39:38 -07:00
|
|
|
|
2021-05-03 07:45:47 -07:00
|
|
|
// MarshalJSON implements JSONCodec.MarshalJSON method,
|
2020-08-26 02:39:38 -07:00
|
|
|
// it marshals to JSON using legacy amino codec.
|
|
|
|
func (ac *AminoCodec) MarshalJSON(o proto.Message) ([]byte, error) {
|
|
|
|
return ac.LegacyAmino.MarshalJSON(o)
|
|
|
|
}
|
|
|
|
|
2021-05-03 07:45:47 -07:00
|
|
|
// MustMarshalJSON implements JSONCodec.MustMarshalJSON method,
|
2020-08-26 02:39:38 -07:00
|
|
|
// it executes MarshalJSON except it panics upon failure.
|
|
|
|
func (ac *AminoCodec) MustMarshalJSON(o proto.Message) []byte {
|
|
|
|
return ac.LegacyAmino.MustMarshalJSON(o)
|
|
|
|
}
|
|
|
|
|
2021-05-03 07:45:47 -07:00
|
|
|
// UnmarshalJSON implements JSONCodec.UnmarshalJSON method,
|
2020-08-26 02:39:38 -07:00
|
|
|
// it unmarshals from JSON using legacy amino codec.
|
|
|
|
func (ac *AminoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error {
|
|
|
|
return ac.LegacyAmino.UnmarshalJSON(bz, ptr)
|
|
|
|
}
|
|
|
|
|
2021-05-03 07:45:47 -07:00
|
|
|
// MustUnmarshalJSON implements JSONCodec.MustUnmarshalJSON method,
|
2020-08-26 02:39:38 -07:00
|
|
|
// it executes UnmarshalJSON except it panics upon failure.
|
|
|
|
func (ac *AminoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message) {
|
|
|
|
ac.LegacyAmino.MustUnmarshalJSON(bz, ptr)
|
|
|
|
}
|
2020-12-08 01:27:08 -08:00
|
|
|
|
|
|
|
// MarshalInterface is a convenience function for amino marshaling interfaces.
|
|
|
|
// The `i` must be an interface.
|
2021-04-29 03:46:22 -07:00
|
|
|
// NOTE: to marshal a concrete type, you should use Marshal instead
|
2020-12-08 01:27:08 -08:00
|
|
|
func (ac *AminoCodec) MarshalInterface(i proto.Message) ([]byte, error) {
|
|
|
|
if err := assertNotNil(i); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-29 03:46:22 -07:00
|
|
|
return ac.LegacyAmino.Marshal(i)
|
2020-12-08 01:27:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalInterface is a convenience function for amino unmarshaling interfaces.
|
|
|
|
// `ptr` must be a pointer to an interface.
|
2021-04-29 03:46:22 -07:00
|
|
|
// NOTE: to unmarshal a concrete type, you should use Unmarshal instead
|
2020-12-08 01:27:08 -08:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// var x MyInterface
|
|
|
|
// err := cdc.UnmarshalInterface(bz, &x)
|
|
|
|
func (ac *AminoCodec) UnmarshalInterface(bz []byte, ptr interface{}) error {
|
2021-04-29 03:46:22 -07:00
|
|
|
return ac.LegacyAmino.Unmarshal(bz, ptr)
|
2020-12-08 01:27:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalInterfaceJSON is a convenience function for amino marshaling interfaces.
|
|
|
|
// The `i` must be an interface.
|
|
|
|
// NOTE: to marshal a concrete type, you should use MarshalJSON instead
|
|
|
|
func (ac *AminoCodec) MarshalInterfaceJSON(i proto.Message) ([]byte, error) {
|
|
|
|
if err := assertNotNil(i); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ac.LegacyAmino.MarshalJSON(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalInterfaceJSON is a convenience function for amino unmarshaling interfaces.
|
|
|
|
// `ptr` must be a pointer to an interface.
|
|
|
|
// NOTE: to unmarshal a concrete type, you should use UnmarshalJSON instead
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// var x MyInterface
|
|
|
|
// err := cdc.UnmarshalInterfaceJSON(bz, &x)
|
|
|
|
func (ac *AminoCodec) UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error {
|
|
|
|
return ac.LegacyAmino.UnmarshalJSON(bz, ptr)
|
|
|
|
}
|