diff --git a/codec/types/any.go b/codec/types/any.go index eebf13ca6c..c33931f0e2 100644 --- a/codec/types/any.go +++ b/codec/types/any.go @@ -113,8 +113,3 @@ func (any *Any) pack(x proto.Message) error { func (any *Any) GetCachedValue() interface{} { return any.cachedValue } - -// ClearCachedValue clears the cached value from the Any -func (any *Any) ClearCachedValue() { - any.cachedValue = nil -} diff --git a/codec/types/any_internal_test.go b/codec/types/any_internal_test.go new file mode 100644 index 0000000000..fda205f591 --- /dev/null +++ b/codec/types/any_internal_test.go @@ -0,0 +1,53 @@ +package types + +import ( + "testing" + + "github.com/gogo/protobuf/proto" + "github.com/stretchr/testify/require" +) + +type Dog struct { + Name string `protobuf:"bytes,1,opt,name=size,proto3" json:"size,omitempty"` +} + +func (d Dog) Greet() string { return d.Name } + +// We implement a minimal proto.Message interface +func (d *Dog) Reset() { d.Name = "" } +func (d *Dog) String() string { return d.Name } +func (d *Dog) ProtoMessage() {} +func (d *Dog) XXX_MessageName() string { return "tests/dog" } + +type Animal interface { + Greet() string +} + +var _ Animal = (*Dog)(nil) +var _ proto.Message = (*Dog)(nil) + +func TestAnyPackUnpack(t *testing.T) { + registry := NewInterfaceRegistry() + registry.RegisterInterface("Animal", (*Animal)(nil)) + registry.RegisterImplementations( + (*Animal)(nil), + &Dog{}, + ) + + spot := &Dog{Name: "Spot"} + var animal Animal + + // with cache + any, err := NewAnyWithValue(spot) + require.NoError(t, err) + require.Equal(t, spot, any.GetCachedValue()) + err = registry.UnpackAny(any, &animal) + require.NoError(t, err) + require.Equal(t, spot, animal) + + // without cache + any.cachedValue = nil + err = registry.UnpackAny(any, &animal) + require.NoError(t, err) + require.Equal(t, spot, animal) +} diff --git a/codec/types/types_test.go b/codec/types/types_test.go index b2d0142695..43f8537550 100644 --- a/codec/types/types_test.go +++ b/codec/types/types_test.go @@ -9,16 +9,15 @@ import ( "github.com/gogo/protobuf/grpc" "github.com/gogo/protobuf/jsonpb" "github.com/gogo/protobuf/proto" - grpc2 "google.golang.org/grpc" - "github.com/stretchr/testify/require" + grpc2 "google.golang.org/grpc" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/testutil/testdata" ) -func TestPackUnpack(t *testing.T) { +func TestAnyPackUnpack(t *testing.T) { registry := testdata.NewTestInterfaceRegistry() spot := &testdata.Dog{Name: "Spot"} @@ -31,12 +30,6 @@ func TestPackUnpack(t *testing.T) { err = registry.UnpackAny(any, &animal) require.NoError(t, err) require.Equal(t, spot, animal) - - // without cache - any.ClearCachedValue() - err = registry.UnpackAny(any, &animal) - require.NoError(t, err) - require.Equal(t, spot, animal) } type TestI interface {