Skip to content

Commit

Permalink
use reflect to accept codec.ProtoMarshaler
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica committed Jun 15, 2022
1 parent 9eeb401 commit 6db3de3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
27 changes: 19 additions & 8 deletions types/query/filtered_pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package query

import (
"fmt"
"reflect"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/types"
Expand Down Expand Up @@ -116,11 +117,11 @@ func FilteredPaginate(
return res, nil
}

func GenericFilteredPaginate[V any, F codec.ProtoMarshaler](
func GenericFilteredPaginate[T codec.ProtoMarshaler, F codec.ProtoMarshaler](
cdc codec.BinaryCodec,
prefixStore types.KVStore,
pageRequest *PageRequest,
onResult func(key []byte, value V) (F, error),
onResult func(key []byte, value T) (F, error),
) ([]F, *PageResponse, error) {
// if the PageRequest is nil, use default PageRequest
if pageRequest == nil {
Expand Down Expand Up @@ -162,13 +163,18 @@ func GenericFilteredPaginate[V any, F codec.ProtoMarshaler](
return nil, nil, iterator.Error()
}

protoMsg := any(new(V))
err := cdc.Unmarshal(iterator.Value(), protoMsg.(codec.ProtoMarshaler))
// initialize the proto message
va := reflect.ValueOf(new(T)).Elem()
v := reflect.New(va.Type().Elem())
va.Set(v)
protoMsg := va.Interface().(codec.ProtoMarshaler)

err := cdc.Unmarshal(iterator.Value(), protoMsg)
if err != nil {
return nil, nil, err
}

val, err := onResult(iterator.Key(), *(protoMsg.(*V)))
val, err := onResult(iterator.Key(), protoMsg.(T))
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -197,13 +203,18 @@ func GenericFilteredPaginate[V any, F codec.ProtoMarshaler](
return nil, nil, iterator.Error()
}

protoMsg := any(new(V))
err := cdc.Unmarshal(iterator.Value(), protoMsg.(codec.ProtoMarshaler))
// initialize the proto message
va := reflect.ValueOf(new(T)).Elem()
v := reflect.New(va.Type().Elem())
va.Set(v)
protoMsg := va.Interface().(codec.ProtoMarshaler)

err := cdc.Unmarshal(iterator.Value(), protoMsg)
if err != nil {
return nil, nil, err
}

val, err := onResult(iterator.Key(), *(protoMsg.(*V)))
val, err := onResult(iterator.Key(), (protoMsg).(T))
if err != nil {
return nil, nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions x/authz/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (k Keeper) Grants(c context.Context, req *authz.QueryGrantsRequest) (*authz
key := grantStoreKey(grantee, granter, "")
grantsStore := prefix.NewStore(store, key)

authorizations, pageRes, err := query.GenericFilteredPaginate(k.cdc, grantsStore, req.Pagination, func(key []byte, auth authz.Grant) (*authz.Grant, error) {
authorizations, pageRes, err := query.GenericFilteredPaginate(k.cdc, grantsStore, req.Pagination, func(key []byte, auth *authz.Grant) (*authz.Grant, error) {
auth1, err := auth.GetAuthorization()
if err != nil {
return nil, err
Expand Down Expand Up @@ -101,7 +101,7 @@ func (k Keeper) GranterGrants(c context.Context, req *authz.QueryGranterGrantsRe
store := ctx.KVStore(k.storeKey)
authzStore := prefix.NewStore(store, grantStoreKey(nil, granter, ""))

grants, pageRes, err := query.GenericFilteredPaginate(k.cdc, authzStore, req.Pagination, func(key []byte, auth authz.Grant) (*authz.GrantAuthorization, error) {
grants, pageRes, err := query.GenericFilteredPaginate(k.cdc, authzStore, req.Pagination, func(key []byte, auth *authz.Grant) (*authz.GrantAuthorization, error) {
auth1, err := auth.GetAuthorization()
if err != nil {
return nil, err
Expand Down Expand Up @@ -146,7 +146,7 @@ func (k Keeper) GranteeGrants(c context.Context, req *authz.QueryGranteeGrantsRe
ctx := sdk.UnwrapSDKContext(c)
store := prefix.NewStore(ctx.KVStore(k.storeKey), GrantKey)

authorizations, pageRes, err := query.GenericFilteredPaginate(k.cdc, store, req.Pagination, func(key []byte, auth authz.Grant) (*authz.GrantAuthorization, error) {
authorizations, pageRes, err := query.GenericFilteredPaginate(k.cdc, store, req.Pagination, func(key []byte, auth *authz.Grant) (*authz.GrantAuthorization, error) {
auth1, err := auth.GetAuthorization()
if err != nil {
return nil, err
Expand Down

0 comments on commit 6db3de3

Please sign in to comment.