x/params/types: make KeyTable.RegisterType fully dereference pointers (#6915)

Use a for loop to correctly and fully dereference and
extract the type for the type of ParamSetPair.Value.

Fixes #6785

Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
Emmanuel T Odeke 2020-08-02 13:18:28 -07:00 committed by GitHub
parent c7c66f8b36
commit 10d330e5b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -0,0 +1,27 @@
package types
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestKeyTableUnfurlsPointers(t *testing.T) {
tbl := NewKeyTable()
validator := func(_ interface{}) error {
return nil
}
tbl = tbl.RegisterType(ParamSetPair{
Key: []byte("key"),
Value: (*****string)(nil),
ValidatorFn: validator,
})
got := tbl.m["key"]
want := attribute{
vfn: validator,
ty: reflect.ValueOf("").Type(),
}
require.Equal(t, got.ty, want.ty)
}

View File

@ -48,7 +48,7 @@ func (t KeyTable) RegisterType(psp ParamSetPair) KeyTable {
rty := reflect.TypeOf(psp.Value)
// indirect rty if it is a pointer
if rty.Kind() == reflect.Ptr {
for rty.Kind() == reflect.Ptr {
rty = rty.Elem()
}