Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add codec method to retrieve a Go type registered name #316

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,30 @@ func (cdc *Codec) PrintTypes(out io.Writer) error {
return nil
}

// ConcreteRegisteredName returns the name under which i has been registered.
func (cdc *Codec) ConcreteRegisteredName(i interface{}) (string, error) {
cdc.mtx.RLock()
defer cdc.mtx.RUnlock()

if i == nil {
return "", errors.New("argument must not be nil")
}

gt := reflect.TypeOf(i)

// if i is a non-nil pointer, dereference it and grab its inner Elem
if gt.Kind() == reflect.Ptr {
gt = gt.Elem()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if this a **SomeType? (pointer to pointer)

Copy link
Author

@gsora gsora May 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhm, **SomeType would return error even though the struct type has been registered previously.

I could add a check on the inner gt.Elem(), this way it would dereference 2 times and get to the concrete instance.

Thoughts?


Something like:

gt := reflect.TypeOf(i)

for gt.Kind() == reflect.Ptr {
	gt = gt.Elem()
}

}

ct, ok := cdc.typeInfos[gt]
if !ok {
return "", fmt.Errorf("no type registered for %s", gt.String())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be a sentinel error, such that callers can switch over a returned error to see if the type wasn't registered or if any other error occurred.

And independent of that: I think %s already calls String() on the type (no need to explicitly call it here).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would those two errors be okay for the purpose?

  • ErrNilPointer, when i is nil;
  • ErrTypeNotRegistered, when the concrete type of i is not registered;

Yup, String() will be removed :-)

}

return ct.Name, nil
}

// A heuristic to guess the size of a registered type and return it as a string.
// If the size is not fixed it returns "variable".
func getLengthStr(info *TypeInfo) string {
Expand Down
32 changes: 31 additions & 1 deletion codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

amino "github.com/tendermint/go-amino"
"github.com/tendermint/go-amino"
)

type SimpleStruct struct {
Expand Down Expand Up @@ -250,3 +250,33 @@ func TestCodecSeal(t *testing.T) {
assert.Panics(t, func() { cdc.RegisterInterface((*Bar)(nil), nil) })
assert.Panics(t, func() { cdc.RegisterConcrete(int(0), "int", nil) })
}

func TestCodec_RegisteredName(t *testing.T) {
aminoStructType := "tendermint/SimpleStruct"

// Struct is registered
cdc := amino.NewCodec()
cdc.RegisterConcrete(SimpleStruct{}, aminoStructType, nil)
rn, err := cdc.ConcreteRegisteredName(SimpleStruct{})
require.NoError(t, err)
require.Equal(t, aminoStructType, rn)

// Struct is not registered
cdc = amino.NewCodec()
rn, err = cdc.ConcreteRegisteredName(SimpleStruct{})
require.Error(t, err)
require.Empty(t, rn)

// Struct is pointer
cdc = amino.NewCodec()
cdc.RegisterConcrete(SimpleStruct{}, aminoStructType, nil)
rn, err = cdc.ConcreteRegisteredName(&SimpleStruct{})
require.NoError(t, err)
require.Equal(t, aminoStructType, rn)

// Struct is nil pointer
cdc = amino.NewCodec()
rn, err = cdc.ConcreteRegisteredName(nil)
require.Error(t, err)
require.Empty(t, rn)
}