Skip to content

Commit

Permalink
Any: update and test String method (#8854)
Browse files Browse the repository at this point in the history
* Any: update and test String method

* remove commented code

* disable Any proto String() and GoString() generation

* fix linter issues
  • Loading branch information
robert-zaremba authored Mar 12, 2021
1 parent b9f3db1 commit 0711e16
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 55 deletions.
25 changes: 25 additions & 0 deletions codec/types/any.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
fmt "fmt"

"github.com/gogo/protobuf/proto"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -116,3 +118,26 @@ func (any *Any) pack(x proto.Message) error {
func (any *Any) GetCachedValue() interface{} {
return any.cachedValue
}

// GoString returns a string representing valid go code to reproduce the current state of
// the struct.
func (any *Any) GoString() string {
if any == nil {
return "nil"
}
extra := ""
if any.XXX_unrecognized != nil {
extra = fmt.Sprintf(",\n XXX_unrecognized: %#v,\n", any.XXX_unrecognized)
}
return fmt.Sprintf("&Any{TypeUrl: %#v,\n Value: %#v%s\n}",
any.TypeUrl, any.Value, extra)
}

// String implements the stringer interface
func (any *Any) String() string {
if any == nil {
return "nil"
}
return fmt.Sprintf("&Any{TypeUrl:%v,Value:%v,XXX_unrecognized:%v}",
any.TypeUrl, any.Value, any.XXX_unrecognized)
}
67 changes: 12 additions & 55 deletions codec/types/any.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions codec/types/any_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ func TestAnyPackUnpack(t *testing.T) {
require.NoError(t, err)
require.Equal(t, spot, animal)
}

func TestString(t *testing.T) {
require := require.New(t)
spot := &Dog{Name: "Spot"}
any, err := NewAnyWithValue(spot)
require.NoError(err)

require.Equal("&Any{TypeUrl:/tests/dog,Value:[10 4 83 112 111 116],XXX_unrecognized:[]}", any.String())
require.Equal(`&Any{TypeUrl: "/tests/dog",
Value: []byte{0xa, 0x4, 0x53, 0x70, 0x6f, 0x74}
}`, any.GoString())
}
3 changes: 3 additions & 0 deletions third_party/proto/google/protobuf/any.proto
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ message Any {
bytes value = 2;

option (gogoproto.typedecl) = false;
option (gogoproto.goproto_stringer) = false;
option (gogoproto.gostring) = false;
option (gogoproto.stringer) = false;
}

option (gogoproto.goproto_registration) = false;

0 comments on commit 0711e16

Please sign in to comment.