-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add amino compatibility layer for proto Any (#6151)
* WIP on Any amino compatibility layer * Add tests & JSON * Refactor Marshal/UnmarshalAny * remove extra test * Add support for nested Any's * Add docs * Update codec/any_test.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
- Loading branch information
Showing
14 changed files
with
939 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package codec | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec/types" | ||
) | ||
|
||
// MarshalAny is a convenience function for packing the provided value in an | ||
// Any and then proto marshaling it to bytes | ||
func MarshalAny(m Marshaler, x interface{}) ([]byte, error) { | ||
msg, ok := x.(proto.Message) | ||
if !ok { | ||
return nil, fmt.Errorf("can't proto marshal %T", x) | ||
} | ||
|
||
any := &types.Any{} | ||
err := any.Pack(msg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return m.MarshalBinaryBare(any) | ||
} | ||
|
||
// UnmarshalAny is a convenience function for proto unmarshaling an Any from | ||
// bz and then unpacking it to the interface pointer passed in as iface using | ||
// the provided AnyUnpacker or returning an error | ||
// | ||
// Ex: | ||
// var x MyInterface | ||
// err := UnmarshalAny(unpacker, &x, bz) | ||
func UnmarshalAny(m Marshaler, iface interface{}, bz []byte) error { | ||
any := &types.Any{} | ||
|
||
err := m.UnmarshalBinaryBare(bz, any) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return m.UnpackAny(any, iface) | ||
} |
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,54 @@ | ||
package codec | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec/testdata" | ||
"github.com/cosmos/cosmos-sdk/codec/types" | ||
) | ||
|
||
func NewTestInterfaceRegistry() types.InterfaceRegistry { | ||
registry := types.NewInterfaceRegistry() | ||
registry.RegisterInterface("Animal", (*testdata.Animal)(nil)) | ||
registry.RegisterImplementations( | ||
(*testdata.Animal)(nil), | ||
&testdata.Dog{}, | ||
&testdata.Cat{}, | ||
) | ||
return registry | ||
} | ||
|
||
func TestMarshalAny(t *testing.T) { | ||
registry := types.NewInterfaceRegistry() | ||
|
||
cdc := NewProtoCodec(registry) | ||
|
||
kitty := &testdata.Cat{Moniker: "Kitty"} | ||
bz, err := MarshalAny(cdc, kitty) | ||
require.NoError(t, err) | ||
|
||
var animal testdata.Animal | ||
|
||
// empty registry should fail | ||
err = UnmarshalAny(cdc, &animal, bz) | ||
require.Error(t, err) | ||
|
||
// wrong type registration should fail | ||
registry.RegisterImplementations((*testdata.Animal)(nil), &testdata.Dog{}) | ||
err = UnmarshalAny(cdc, &animal, bz) | ||
require.Error(t, err) | ||
|
||
// should pass | ||
registry = NewTestInterfaceRegistry() | ||
cdc = NewProtoCodec(registry) | ||
err = UnmarshalAny(cdc, &animal, bz) | ||
require.NoError(t, err) | ||
require.Equal(t, kitty, animal) | ||
|
||
// nil should fail | ||
registry = NewTestInterfaceRegistry() | ||
err = UnmarshalAny(cdc, nil, bz) | ||
require.Error(t, err) | ||
} |
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.