-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend ContractInfo for custom data (#492)
* Extend ContractInfo with custom data * Review comments
- Loading branch information
Showing
21 changed files
with
750 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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.Marshaler = (*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.Marshaler | ||
interfaceRegistry types.InterfaceRegistry | ||
} | ||
|
||
func NewProtoCodec(marshaler codec.Marshaler, registry types.InterfaceRegistry) *ProtoCodec { | ||
return &ProtoCodec{Marshaler: 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.