remove any.ClearCachedValue method (#8402)

* remove any.ClearCachedValue method

This method is in a public interface but used only for tests.
It shouldn't be used - `Any` values shouldn't be manipulated. This could lead to bugs.

* rename any_test.go

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
This commit is contained in:
Robert Zaremba 2021-01-21 13:35:40 +01:00 committed by GitHub
parent 57f5e96570
commit 78703f1b6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 14 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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 {