From 0fc0ac585c8dffb3c8635549a22de82d8500c23c Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Mon, 21 Feb 2022 10:31:36 +0100 Subject: [PATCH] Replace custom codec with sdk codec --- cmd/wasmd/root.go | 3 +- x/wasm/client/codec/marshaller.go | 79 ------------------------------- 2 files changed, 1 insertion(+), 81 deletions(-) delete mode 100644 x/wasm/client/codec/marshaller.go diff --git a/cmd/wasmd/root.go b/cmd/wasmd/root.go index 78fd947815..f0da69d07e 100644 --- a/cmd/wasmd/root.go +++ b/cmd/wasmd/root.go @@ -34,7 +34,6 @@ import ( "github.com/CosmWasm/wasmd/app" "github.com/CosmWasm/wasmd/app/params" "github.com/CosmWasm/wasmd/x/wasm" - clientcodec "github.com/CosmWasm/wasmd/x/wasm/client/codec" wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" ) @@ -52,7 +51,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { cfg.Seal() initClientCtx := client.Context{}. - WithCodec(clientcodec.NewProtoCodec(encodingConfig.Marshaler, encodingConfig.InterfaceRegistry)). + WithCodec(encodingConfig.Marshaler). WithInterfaceRegistry(encodingConfig.InterfaceRegistry). WithTxConfig(encodingConfig.TxConfig). WithLegacyAmino(encodingConfig.Amino). diff --git a/x/wasm/client/codec/marshaller.go b/x/wasm/client/codec/marshaller.go deleted file mode 100644 index 9f02a3f5f0..0000000000 --- a/x/wasm/client/codec/marshaller.go +++ /dev/null @@ -1,79 +0,0 @@ -package codec - -import ( - "bytes" - "fmt" - - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/gogo/protobuf/jsonpb" - "github.com/gogo/protobuf/proto" -) - -var _ codec.Codec = (*ProtoCodec)(nil) - -// ProtoCodec that omits empty values. -// This Marshaler can be used globally when setting up the client context or individually -// for each command via `clientCtx.WithJSONMarshaler(myMarshaler)`. -type ProtoCodec struct { - codec.Codec - interfaceRegistry types.InterfaceRegistry -} - -func NewProtoCodec(marshaler codec.Codec, registry types.InterfaceRegistry) *ProtoCodec { - return &ProtoCodec{Codec: marshaler, interfaceRegistry: registry} -} - -// MarshalJSON implements JSONMarshaler.MarshalJSON method, -// it marshals to JSON using proto codec. -func (pc *ProtoCodec) MarshalJSON(o proto.Message) ([]byte, error) { - m, ok := o.(codec.ProtoMarshaler) - if !ok { - return nil, fmt.Errorf("cannot protobuf JSON encode unsupported type: %T", o) - } - return ProtoMarshalJSON(m, pc.interfaceRegistry) -} - -// 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) - } - return bz -} - -// 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) -} - -// ProtoMarshalJSON provides an auxiliary function to return Proto3 JSON encoded -// bytes of a message. -func ProtoMarshalJSON(msg proto.Message, resolver jsonpb.AnyResolver) ([]byte, error) { - // method copied from sdk codec/json.go with EmitDefaults set to `false` - // so that empty fields are not rendered - - // We use the OrigName because camel casing fields just doesn't make sense. - // EmitDefaults is also often the more expected behavior for CLI users - jm := &jsonpb.Marshaler{OrigName: true, EmitDefaults: false, AnyResolver: resolver} - err := types.UnpackInterfaces(msg, types.ProtoJSONPacker{JSONPBMarshaler: jm}) - if err != nil { - return nil, err - } - - buf := new(bytes.Buffer) - - if err := jm.Marshal(buf, msg); err != nil { - return nil, err - } - - return buf.Bytes(), nil -}