Move and update codec.MarshalAny functions to codec.Marshaler interface (#8080)
* Changelog update * Rename codec.MarshalAny * move codec.MarshalInterface to codec.Marshaler * fix tests * Update amino_codec for compliance with MarshalerInterface * update tests and comments * add tests * change order of args in UnmarshalInterface to a canonical one * uplift MarshalInterface to take ProtoMessage as an argument * wip * add nil check * make tests working * tests cleanup * add support for *JSON methods * Update changelog * linter fixes * fix test types * update evidence genesis_test * adding test * review updates Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
mergify[bot]
parent
da6b7f7755
commit
b219c54c2d
+71
-2
@@ -2,13 +2,14 @@ package codec
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec/types"
|
||||
|
||||
"github.com/gogo/protobuf/jsonpb"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec/types"
|
||||
)
|
||||
|
||||
// ProtoCodecMarshaler defines an interface for codecs that utilize Protobuf for both
|
||||
@@ -160,6 +161,67 @@ func (pc *ProtoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message) {
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalInterface is a convenience function for proto marshalling interfaces. It packs
|
||||
// the provided value, which must be an interface, in an Any and then marshals it to bytes.
|
||||
// NOTE: to marshal a concrete type, you should use MarshalBinaryBare instead
|
||||
func (pc *ProtoCodec) MarshalInterface(i proto.Message) ([]byte, error) {
|
||||
if err := assertNotNil(i); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
any, err := types.NewAnyWithValue(i)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pc.MarshalBinaryBare(any)
|
||||
}
|
||||
|
||||
// UnmarshalInterface is a convenience function for proto unmarshaling interfaces. It
|
||||
// unmarshals an Any from bz bytes and then unpacks it to the `ptr`, which must
|
||||
// be a pointer to a non empty interface with registered implementations.
|
||||
// NOTE: to unmarshal a concrete type, you should use UnmarshalBinaryBare instead
|
||||
//
|
||||
// Example:
|
||||
// var x MyInterface
|
||||
// err := cdc.UnmarshalInterface(bz, &x)
|
||||
func (pc *ProtoCodec) UnmarshalInterface(bz []byte, ptr interface{}) error {
|
||||
any := &types.Any{}
|
||||
err := pc.UnmarshalBinaryBare(bz, any)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return pc.UnpackAny(any, ptr)
|
||||
}
|
||||
|
||||
// MarshalInterfaceJSON is a convenience function for proto marshalling interfaces. It
|
||||
// packs the provided value in an Any and then marshals it to bytes.
|
||||
// NOTE: to marshal a concrete type, you should use MarshalJSON instead
|
||||
func (pc *ProtoCodec) MarshalInterfaceJSON(x proto.Message) ([]byte, error) {
|
||||
any, err := types.NewAnyWithValue(x)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pc.MarshalJSON(any)
|
||||
}
|
||||
|
||||
// UnmarshalInterfaceJSON is a convenience function for proto unmarshaling interfaces.
|
||||
// It unmarshals an Any from bz bytes and then unpacks it to the `iface`, which must
|
||||
// be a pointer to a non empty interface, implementing proto.Message with registered implementations.
|
||||
// NOTE: to unmarshal a concrete type, you should use UnmarshalJSON instead
|
||||
//
|
||||
// Example:
|
||||
// var x MyInterface // must implement proto.Message
|
||||
// err := cdc.UnmarshalInterfaceJSON(&x, bz)
|
||||
func (pc *ProtoCodec) UnmarshalInterfaceJSON(bz []byte, iface interface{}) error {
|
||||
any := &types.Any{}
|
||||
err := pc.UnmarshalJSON(bz, any)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return pc.UnpackAny(any, iface)
|
||||
}
|
||||
|
||||
// UnpackAny implements AnyUnpacker.UnpackAny method,
|
||||
// it unpacks the value in any to the interface pointer passed in as
|
||||
// iface.
|
||||
@@ -170,3 +232,10 @@ func (pc *ProtoCodec) UnpackAny(any *types.Any, iface interface{}) error {
|
||||
func (pc *ProtoCodec) InterfaceRegistry() types.InterfaceRegistry {
|
||||
return pc.interfaceRegistry
|
||||
}
|
||||
|
||||
func assertNotNil(i interface{}) error {
|
||||
if i == nil {
|
||||
return errors.New("can't marshal <nil> value")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user