Make JSONMarshaler methods require proto.Message (#7054)
* Make JSONMarshaler require proto.Message * Use &msg with MarshalJSON * Use *LegacyAmino in queriers instead of JSONMarshaler * Revert ABCIMessageLogs String() and coins tests * Use LegacyAmino in client/debug and fix subspace tests * Use LegacyAmino in all legacy queriers and adapt simulation * Make AminoCodec implement Marshaler and some godoc fixes * Test fixes * Remove unrelevant comment * Use TxConfig.TxJSONEncoder * Use encoding/json in genutil cli migrate/validate genesis cmds * Address simulation related comments * Use JSONMarshaler in cli tests * Use proto.Message as respType in cli tests * Use tmjson for tm GenesisDoc * Update types/module/simulation.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update types/module/module_test.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Add godoc comments * Remove unused InsertKeyJSON * Fix tests Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
co-authored by
Federico Kunze
Aaron Craelius
parent
b5bc864862
commit
7f59723d88
+25
-4
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/codec/types"
|
||||
|
||||
"github.com/gogo/protobuf/jsonpb"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
)
|
||||
|
||||
// ProtoCodec defines a codec that utilizes Protobuf for both binary and JSON
|
||||
@@ -18,14 +19,17 @@ type ProtoCodec struct {
|
||||
|
||||
var _ Marshaler = &ProtoCodec{}
|
||||
|
||||
// NewProtoCodec returns a reference to a new ProtoCodec
|
||||
func NewProtoCodec(anyUnpacker types.AnyUnpacker) *ProtoCodec {
|
||||
return &ProtoCodec{anyUnpacker: anyUnpacker}
|
||||
}
|
||||
|
||||
// MarshalBinaryBare implements BinaryMarshaler.MarshalBinaryBare method.
|
||||
func (pc *ProtoCodec) MarshalBinaryBare(o ProtoMarshaler) ([]byte, error) {
|
||||
return o.Marshal()
|
||||
}
|
||||
|
||||
// MustMarshalBinaryBare implements BinaryMarshaler.MustMarshalBinaryBare method.
|
||||
func (pc *ProtoCodec) MustMarshalBinaryBare(o ProtoMarshaler) []byte {
|
||||
bz, err := pc.MarshalBinaryBare(o)
|
||||
if err != nil {
|
||||
@@ -35,6 +39,7 @@ func (pc *ProtoCodec) MustMarshalBinaryBare(o ProtoMarshaler) []byte {
|
||||
return bz
|
||||
}
|
||||
|
||||
// MarshalBinaryLengthPrefixed implements BinaryMarshaler.MarshalBinaryLengthPrefixed method.
|
||||
func (pc *ProtoCodec) MarshalBinaryLengthPrefixed(o ProtoMarshaler) ([]byte, error) {
|
||||
bz, err := pc.MarshalBinaryBare(o)
|
||||
if err != nil {
|
||||
@@ -46,6 +51,7 @@ func (pc *ProtoCodec) MarshalBinaryLengthPrefixed(o ProtoMarshaler) ([]byte, err
|
||||
return append(sizeBuf[:n], bz...), nil
|
||||
}
|
||||
|
||||
// MustMarshalBinaryLengthPrefixed implements BinaryMarshaler.MustMarshalBinaryLengthPrefixed method.
|
||||
func (pc *ProtoCodec) MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte {
|
||||
bz, err := pc.MarshalBinaryLengthPrefixed(o)
|
||||
if err != nil {
|
||||
@@ -55,6 +61,7 @@ func (pc *ProtoCodec) MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte {
|
||||
return bz
|
||||
}
|
||||
|
||||
// UnmarshalBinaryBare implements BinaryMarshaler.UnmarshalBinaryBare method.
|
||||
func (pc *ProtoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error {
|
||||
err := ptr.Unmarshal(bz)
|
||||
if err != nil {
|
||||
@@ -67,12 +74,14 @@ func (pc *ProtoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MustUnmarshalBinaryBare implements BinaryMarshaler.MustUnmarshalBinaryBare method.
|
||||
func (pc *ProtoCodec) MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) {
|
||||
if err := pc.UnmarshalBinaryBare(bz, ptr); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalBinaryLengthPrefixed implements BinaryMarshaler.UnmarshalBinaryLengthPrefixed method.
|
||||
func (pc *ProtoCodec) UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) error {
|
||||
size, n := binary.Uvarint(bz)
|
||||
if n < 0 {
|
||||
@@ -89,13 +98,16 @@ func (pc *ProtoCodec) UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshale
|
||||
return pc.UnmarshalBinaryBare(bz, ptr)
|
||||
}
|
||||
|
||||
// MustUnmarshalBinaryLengthPrefixed implements BinaryMarshaler.MustUnmarshalBinaryLengthPrefixed method.
|
||||
func (pc *ProtoCodec) MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) {
|
||||
if err := pc.UnmarshalBinaryLengthPrefixed(bz, ptr); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (pc *ProtoCodec) MarshalJSON(o interface{}) ([]byte, error) {
|
||||
// MarshalJSON implements JSONMarshaler.MarshalJSON method,
|
||||
// it marshals to JSON using proto codec.
|
||||
func (pc *ProtoCodec) MarshalJSON(o proto.Message) ([]byte, error) {
|
||||
m, ok := o.(ProtoMarshaler)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cannot protobuf JSON encode unsupported type: %T", o)
|
||||
@@ -104,7 +116,9 @@ func (pc *ProtoCodec) MarshalJSON(o interface{}) ([]byte, error) {
|
||||
return ProtoMarshalJSON(m)
|
||||
}
|
||||
|
||||
func (pc *ProtoCodec) MustMarshalJSON(o interface{}) []byte {
|
||||
// MustMarshalJSON implements JSONMarshaler.MustMarshalJSON method,
|
||||
// it executes MarshalJSON except it panics upon failure.
|
||||
func (pc *ProtoCodec) MustMarshalJSON(o proto.Message) []byte {
|
||||
bz, err := pc.MarshalJSON(o)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -113,7 +127,9 @@ func (pc *ProtoCodec) MustMarshalJSON(o interface{}) []byte {
|
||||
return bz
|
||||
}
|
||||
|
||||
func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr interface{}) error {
|
||||
// UnmarshalJSON implements JSONMarshaler.UnmarshalJSON method,
|
||||
// it unmarshals from JSON using proto codec.
|
||||
func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error {
|
||||
m, ok := ptr.(ProtoMarshaler)
|
||||
if !ok {
|
||||
return fmt.Errorf("cannot protobuf JSON decode unsupported type: %T", ptr)
|
||||
@@ -127,12 +143,17 @@ func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr interface{}) error {
|
||||
return types.UnpackInterfaces(ptr, pc.anyUnpacker)
|
||||
}
|
||||
|
||||
func (pc *ProtoCodec) MustUnmarshalJSON(bz []byte, ptr interface{}) {
|
||||
// MustUnmarshalJSON implements JSONMarshaler.MustUnmarshalJSON method,
|
||||
// it executes UnmarshalJSON except it panics upon failure.
|
||||
func (pc *ProtoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message) {
|
||||
if err := pc.UnmarshalJSON(bz, ptr); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// UnpackAny implements AnyUnpacker.UnpackAny method,
|
||||
// it unpacks the value in any to the interface pointer passed in as
|
||||
// iface.
|
||||
func (pc *ProtoCodec) UnpackAny(any *types.Any, iface interface{}) error {
|
||||
return pc.anyUnpacker.UnpackAny(any, iface)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user