Skip to content

Commit

Permalink
feat: Better names for map/list query endpoints (#3962)
Browse files Browse the repository at this point in the history
* Move query methods to queryServer struct

* Rename show-x queries to get-x

* Fix map queries bug

* Update changelog

* More descriptive changelog

* Fix singleton cli bug
  • Loading branch information
Ehsan-saradar authored Feb 14, 2024
1 parent ee46153 commit f86fa97
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 62 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changes

- [#3959](https://github.com/ignite/cli/pull/3959) Remove app name prefix from the `.gitignore` file
- [#3962](https://github.com/ignite/cli/pull/3962) Rename all RPC endpoints and autocli commands generated for `map`/`list`/`single` types

### Fixes

Expand Down
8 changes: 4 additions & 4 deletions ignite/internal/plugin/testdata/execute_ok/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.etcd.io/bbolt v1.3.8 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.15.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ import (
"<%= modulePath %>/x/<%= moduleName %>/types"
)

var _ types.QueryServer = Keeper{}
var _ types.QueryServer = queryServer{}

type queryServer struct {
k Keeper
}

// NewQueryServerImpl returns an implementation of the QueryServer interface.
func NewQueryServerImpl(k Keeper) types.QueryServer {
return queryServer{k}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"<%= modulePath %>/x/<%= moduleName %>/types"
)

func (k Keeper) Params(goCtx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
func (s queryServer) Params(goCtx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)

return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil
return &types.QueryParamsResponse{Params: s.k.GetParams(ctx)}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import (
"github.com/stretchr/testify/require"

keepertest "<%= modulePath %>/testutil/keeper"
"<%= modulePath %>/x/<%= moduleName %>/keeper"
"<%= modulePath %>/x/<%= moduleName %>/types"
)

func TestParamsQuery(t *testing.T) {
keeper, ctx := keepertest.<%= title(moduleName) %>Keeper(t)
k, ctx := keepertest.<%= title(moduleName) %>Keeper(t)
qs := keeper.NewQueryServerImpl(k)
params := types.DefaultParams()
require.NoError(t, keeper.SetParams(ctx, params))
require.NoError(t, k.SetParams(ctx, params))

response, err := keeper.Params(ctx, &types.QueryParamsRequest{})
response, err := qs.Params(ctx, &types.QueryParamsRequest{})
require.NoError(t, err)
require.Equal(t, &types.QueryParamsResponse{Params: params}, response)
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func NewAppModule(
// RegisterServices registers a gRPC query service to respond to the module-specific gRPC queries
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))
}

// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"google.golang.org/grpc/status"
)

func (k Keeper) <%= QueryName.UpperCamel %>(goCtx context.Context, req *types.Query<%= QueryName.UpperCamel %>Request) (*types.Query<%= QueryName.UpperCamel %>Response, error) {
func (s queryServer) <%= QueryName.UpperCamel %>(goCtx context.Context, req *types.Query<%= QueryName.UpperCamel %>Request) (*types.Query<%= QueryName.UpperCamel %>Response, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import (
"google.golang.org/grpc/status"
)

func (k Keeper) <%= TypeName.UpperCamel %>All(ctx context.Context, req *types.QueryAll<%= TypeName.UpperCamel %>Request) (*types.QueryAll<%= TypeName.UpperCamel %>Response, error) {
func (s queryServer) List<%= TypeName.UpperCamel %>(ctx context.Context, req *types.QueryAll<%= TypeName.UpperCamel %>Request) (*types.QueryAll<%= TypeName.UpperCamel %>Response, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

var <%= TypeName.LowerCamel %>s []types.<%= TypeName.UpperCamel %>

store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := runtime.KVStoreAdapter(s.k.storeService.OpenKVStore(ctx))
<%= TypeName.LowerCamel %>Store := prefix.NewStore(store, types.KeyPrefix(types.<%= TypeName.UpperCamel %>Key))

pageRes, err := query.Paginate(<%= TypeName.LowerCamel %>Store, req.Pagination, func(key []byte, value []byte) error {
var <%= TypeName.LowerCamel %> types.<%= TypeName.UpperCamel %>
if err := k.cdc.Unmarshal(value, &<%= TypeName.LowerCamel %>); err != nil {
if err := s.k.cdc.Unmarshal(value, &<%= TypeName.LowerCamel %>); err != nil {
return err
}

Expand All @@ -39,12 +39,12 @@ func (k Keeper) <%= TypeName.UpperCamel %>All(ctx context.Context, req *types.Qu
return &types.QueryAll<%= TypeName.UpperCamel %>Response{<%= TypeName.UpperCamel %>: <%= TypeName.LowerCamel %>s, Pagination: pageRes}, nil
}

func (k Keeper) <%= TypeName.UpperCamel %>(ctx context.Context, req *types.QueryGet<%= TypeName.UpperCamel %>Request) (*types.QueryGet<%= TypeName.UpperCamel %>Response, error) {
func (s queryServer) Get<%= TypeName.UpperCamel %>(ctx context.Context, req *types.QueryGet<%= TypeName.UpperCamel %>Request) (*types.QueryGet<%= TypeName.UpperCamel %>Response, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

<%= TypeName.LowerCamel %>, found := k.Get<%= TypeName.UpperCamel %>(ctx, req.Id)
<%= TypeName.LowerCamel %>, found := s.k.Get<%= TypeName.UpperCamel %>(ctx, req.Id)
if !found {
return nil, sdkerrors.ErrKeyNotFound
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (

"<%= ModulePath %>/x/<%= ModuleName %>/types"
"<%= ModulePath %>/testutil/nullify"
"<%= ModulePath %>/x/<%= ModuleName %>/keeper"
keepertest "<%= ModulePath %>/testutil/keeper"
)

func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) {
keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t)
msgs := createN<%= TypeName.UpperCamel %>(keeper, ctx, 2)
k, ctx := keepertest.<%= title(ModuleName) %>Keeper(t)
qs := keeper.NewQueryServerImpl(k)
msgs := createN<%= TypeName.UpperCamel %>(k, ctx, 2)
tests := []struct {
desc string
request *types.QueryGet<%= TypeName.UpperCamel %>Request
Expand Down Expand Up @@ -45,7 +47,7 @@ func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
response, err := keeper.<%= TypeName.UpperCamel %>(ctx, tc.request)
response, err := qs.Get<%= TypeName.UpperCamel %>(ctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
Expand All @@ -60,8 +62,9 @@ func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) {
}

func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t)
msgs := createN<%= TypeName.UpperCamel %>(keeper, ctx, 5)
k, ctx := keepertest.<%= title(ModuleName) %>Keeper(t)
qs := keeper.NewQueryServerImpl(k)
msgs := createN<%= TypeName.UpperCamel %>(k, ctx, 5)

request := func(next []byte, offset, limit uint64, total bool) *types.QueryAll<%= TypeName.UpperCamel %>Request {
return &types.QueryAll<%= TypeName.UpperCamel %>Request{
Expand All @@ -76,7 +79,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
t.Run("ByOffset", func(t *testing.T) {
step := 2
for i := 0; i < len(msgs); i += step {
resp, err := keeper.<%= TypeName.UpperCamel %>All(ctx, request(nil, uint64(i), uint64(step), false))
resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(nil, uint64(i), uint64(step), false))
require.NoError(t, err)
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
Expand All @@ -89,7 +92,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(msgs); i += step {
resp, err := keeper.<%= TypeName.UpperCamel %>All(ctx, request(next, 0, uint64(step), false))
resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(next, 0, uint64(step), false))
require.NoError(t, err)
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
Expand All @@ -100,7 +103,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
}
})
t.Run("Total", func(t *testing.T) {
resp, err := keeper.<%= TypeName.UpperCamel %>All(ctx, request(nil, 0, 0, true))
resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(nil, 0, 0, true))
require.NoError(t, err)
require.Equal(t, len(msgs), int(resp.Pagination.Total))
require.ElementsMatch(t,
Expand All @@ -109,7 +112,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
)
})
t.Run("InvalidRequest", func(t *testing.T) {
_, err := keeper.<%= TypeName.UpperCamel %>All(ctx, nil)
_, err := qs.List<%= TypeName.UpperCamel %>(ctx, nil)
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
})
}
13 changes: 7 additions & 6 deletions ignite/templates/typed/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func protoQueryModify(opts *typed.Options) genny.RunFn {
appModulePath := gomodulepath.ExtractAppPath(opts.ModulePath)
typenameUpper := opts.TypeName.UpperCamel
rpcQueryGet := protoutil.NewRPC(
typenameUpper,
fmt.Sprintf("Get%s", typenameUpper),
fmt.Sprintf("QueryGet%sRequest", typenameUpper),
fmt.Sprintf("QueryGet%sResponse", typenameUpper),
protoutil.WithRPCOptions(
Expand All @@ -236,7 +236,7 @@ func protoQueryModify(opts *typed.Options) genny.RunFn {
protoutil.AttachComment(rpcQueryGet, fmt.Sprintf("Queries a %v by id.", typenameUpper))

rpcQueryAll := protoutil.NewRPC(
fmt.Sprintf("%sAll", typenameUpper),
fmt.Sprintf("List%s", typenameUpper),
fmt.Sprintf("QueryAll%sRequest", typenameUpper),
fmt.Sprintf("QueryAll%sResponse", typenameUpper),
protoutil.WithRPCOptions(
Expand Down Expand Up @@ -387,14 +387,15 @@ func clientCliQueryModify(replacer placeholder.Replacer, opts *typed.Options) ge
}

template := `{
RpcMethod: "%[2]vAll",
RpcMethod: "List%[2]v",
Use: "list-%[3]v",
Short: "List all %[4]v",
},
{
RpcMethod: "%[2]v",
Use: "show-%[3]v [id]",
Short: "Shows a %[4]v by id",
RpcMethod: "Get%[2]v",
Use: "get-%[3]v [id]",
Short: "Gets a %[4]v by id",
Alias: []string{"show-%[3]v"},
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "id"}},
},
%[1]v`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import (
"google.golang.org/grpc/status"
)

func (k Keeper) <%= TypeName.UpperCamel %>All(ctx context.Context, req *types.QueryAll<%= TypeName.UpperCamel %>Request) (*types.QueryAll<%= TypeName.UpperCamel %>Response, error) {
func (s queryServer) List<%= TypeName.UpperCamel %>(ctx context.Context, req *types.QueryAll<%= TypeName.UpperCamel %>Request) (*types.QueryAll<%= TypeName.UpperCamel %>Response, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

var <%= TypeName.LowerCamel %>s []types.<%= TypeName.UpperCamel %>

store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := runtime.KVStoreAdapter(s.k.storeService.OpenKVStore(ctx))
<%= TypeName.LowerCamel %>Store := prefix.NewStore(store, types.KeyPrefix(types.<%= TypeName.UpperCamel %>KeyPrefix))

pageRes, err := query.Paginate(<%= TypeName.LowerCamel %>Store, req.Pagination, func(key []byte, value []byte) error {
var <%= TypeName.LowerCamel %> types.<%= TypeName.UpperCamel %>
if err := k.cdc.Unmarshal(value, &<%= TypeName.LowerCamel %>); err != nil {
if err := s.k.cdc.Unmarshal(value, &<%= TypeName.LowerCamel %>); err != nil {
return err
}

Expand All @@ -38,12 +38,12 @@ func (k Keeper) <%= TypeName.UpperCamel %>All(ctx context.Context, req *types.Qu
return &types.QueryAll<%= TypeName.UpperCamel %>Response{<%= TypeName.UpperCamel %>: <%= TypeName.LowerCamel %>s, Pagination: pageRes}, nil
}

func (k Keeper) <%= TypeName.UpperCamel %>(ctx context.Context, req *types.QueryGet<%= TypeName.UpperCamel %>Request) (*types.QueryGet<%= TypeName.UpperCamel %>Response, error) {
func (s queryServer) Get<%= TypeName.UpperCamel %>(ctx context.Context, req *types.QueryGet<%= TypeName.UpperCamel %>Request) (*types.QueryGet<%= TypeName.UpperCamel %>Response, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

val, found := k.Get<%= TypeName.UpperCamel %>(
val, found := s.k.Get<%= TypeName.UpperCamel %>(
ctx,
<%= for (i, index) in Indexes { %>req.<%= index.Name.UpperCamel %>,
<% } %>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import (

"<%= ModulePath %>/x/<%= ModuleName %>/types"
"<%= ModulePath %>/testutil/nullify"
"<%= ModulePath %>/x/<%= ModuleName %>/keeper"
keepertest "<%= ModulePath %>/testutil/keeper"
)

// Prevent strconv unused error
var _ = strconv.IntSize

func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) {
keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t)
msgs := createN<%= TypeName.UpperCamel %>(keeper, ctx, 2)
k, ctx := keepertest.<%= title(ModuleName) %>Keeper(t)
qs := keeper.NewQueryServerImpl(k)
msgs := createN<%= TypeName.UpperCamel %>(k, ctx, 2)
tests := []struct {
desc string
request *types.QueryGet<%= TypeName.UpperCamel %>Request
Expand Down Expand Up @@ -57,7 +59,7 @@ func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
response, err := keeper.<%= TypeName.UpperCamel %>(ctx, tc.request)
response, err := qs.Get<%= TypeName.UpperCamel %>(ctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
Expand All @@ -72,8 +74,9 @@ func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) {
}

func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t)
msgs := createN<%= TypeName.UpperCamel %>(keeper, ctx, 5)
k, ctx := keepertest.<%= title(ModuleName) %>Keeper(t)
qs := keeper.NewQueryServerImpl(k)
msgs := createN<%= TypeName.UpperCamel %>(k, ctx, 5)

request := func(next []byte, offset, limit uint64, total bool) *types.QueryAll<%= TypeName.UpperCamel %>Request {
return &types.QueryAll<%= TypeName.UpperCamel %>Request{
Expand All @@ -88,7 +91,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
t.Run("ByOffset", func(t *testing.T) {
step := 2
for i := 0; i < len(msgs); i += step {
resp, err := keeper.<%= TypeName.UpperCamel %>All(ctx, request(nil, uint64(i), uint64(step), false))
resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(nil, uint64(i), uint64(step), false))
require.NoError(t, err)
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
Expand All @@ -101,7 +104,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(msgs); i += step {
resp, err := keeper.<%= TypeName.UpperCamel %>All(ctx, request(next, 0, uint64(step), false))
resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(next, 0, uint64(step), false))
require.NoError(t, err)
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
Expand All @@ -112,7 +115,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
}
})
t.Run("Total", func(t *testing.T) {
resp, err := keeper.<%= TypeName.UpperCamel %>All(ctx, request(nil, 0, 0, true))
resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(nil, 0, 0, true))
require.NoError(t, err)
require.Equal(t, len(msgs), int(resp.Pagination.Total))
require.ElementsMatch(t,
Expand All @@ -121,7 +124,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
)
})
t.Run("InvalidRequest", func(t *testing.T) {
_, err := keeper.<%= TypeName.UpperCamel %>All(ctx, nil)
_, err := qs.List<%= TypeName.UpperCamel %>(ctx, nil)
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
})
}
15 changes: 8 additions & 7 deletions ignite/templates/typed/map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func protoRPCModify(opts *typed.Options) genny.RunFn {
}
typenameUpper, typenameSnake, typenameLower := opts.TypeName.UpperCamel, opts.TypeName.Snake, opts.TypeName.LowerCamel
rpcQueryGet := protoutil.NewRPC(
typenameUpper,
fmt.Sprintf("Get%s", typenameUpper),
fmt.Sprintf("QueryGet%sRequest", typenameUpper),
fmt.Sprintf("QueryGet%sResponse", typenameUpper),
protoutil.WithRPCOptions(
Expand All @@ -168,7 +168,7 @@ func protoRPCModify(opts *typed.Options) genny.RunFn {
protoutil.AttachComment(rpcQueryGet, fmt.Sprintf("Queries a %v by index.", typenameUpper))

rpcQueryAll := protoutil.NewRPC(
fmt.Sprintf("%sAll", typenameUpper),
fmt.Sprintf("List%s", typenameUpper),
fmt.Sprintf("QueryAll%sRequest", typenameUpper),
fmt.Sprintf("QueryAll%sResponse", typenameUpper),
protoutil.WithRPCOptions(
Expand Down Expand Up @@ -254,15 +254,16 @@ func clientCliQueryModify(replacer placeholder.Replacer, opts *typed.Options) ge
}

template := `{
RpcMethod: "%[2]vAll",
RpcMethod: "List%[2]v",
Use: "list-%[3]v",
Short: "List all %[4]v",
},
{
RpcMethod: "%[2]v",
Use: "show-%[3]v [id]",
Short: "Shows a %[4]v",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{%s},
RpcMethod: "Get%[2]v",
Use: "get-%[3]v [id]",
Short: "Gets a %[4]v",
Alias: []string{"show-%[3]v"},
PositionalArgs: []*autocliv1.PositionalArgDescriptor{%[5]s},
},
%[1]v`
replacement := fmt.Sprintf(
Expand Down
Loading

0 comments on commit f86fa97

Please sign in to comment.