Skip to content

Commit

Permalink
revert map due to high complexity to scaffold (map and indexmap and v…
Browse files Browse the repository at this point in the history
…alue codec)
  • Loading branch information
julienrbrt committed Mar 15, 2024
1 parent 24d3b83 commit 3f1c59d
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@ import (
"google.golang.org/grpc/status"
)

func (q queryServer) List<%= TypeName.UpperCamel %>(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 %>

// TODO use collections

store := runtime.KVStoreAdapter(q.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 := q.k.cdc.Unmarshal(value, &<%= TypeName.LowerCamel %>); err != nil {
if err := s.k.cdc.Unmarshal(value, &<%= TypeName.LowerCamel %>); err != nil {
return err
}

Expand All @@ -40,16 +38,16 @@ func (q queryServer) List<%= TypeName.UpperCamel %>(ctx context.Context, req *ty
return &types.QueryAll<%= TypeName.UpperCamel %>Response{<%= TypeName.UpperCamel %>: <%= TypeName.LowerCamel %>s, Pagination: pageRes}, nil
}

func (q queryServer) Get<%= 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, err := q.k.<%= TypeName.UpperCamel %>.Get(
val, found := s.k.Get<%= TypeName.UpperCamel %>(
ctx,
<%= for (i, index) in Indexes { %>req.<%= index.Name.UpperCamel %>,
<% } %>)
if err != nil {
if !found {
return nil, status.Error(codes.NotFound, "not found")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package keeper

import (
"context"

"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
"<%= ModulePath %>/x/<%= ModuleName %>/types"
)

// Set<%= TypeName.UpperCamel %> set a specific <%= TypeName.LowerCamel %> in the store from its index
func (k Keeper) Set<%= TypeName.UpperCamel %>(ctx context.Context, <%= TypeName.LowerCamel %> types.<%= TypeName.UpperCamel %>) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>KeyPrefix))
b := k.cdc.MustMarshal(&<%= TypeName.LowerCamel %>)
store.Set(types.<%= TypeName.UpperCamel %>Key(
<%= for (i, index) in Indexes { %><%= TypeName.LowerCamel %>.<%= index.Name.UpperCamel %>,
<% } %>), b)
}

// Get<%= TypeName.UpperCamel %> returns a <%= TypeName.LowerCamel %> from its index
func (k Keeper) Get<%= TypeName.UpperCamel %>(
ctx context.Context,
<%= for (i, index) in Indexes { %><%= index.Name.LowerCamel %> <%= index.DataType() %>,
<% } %>
) (val types.<%= TypeName.UpperCamel %>, found bool) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>KeyPrefix))

b := store.Get(types.<%= TypeName.UpperCamel %>Key(
<%= for (i, index) in Indexes { %><%= index.Name.LowerCamel %>,
<% } %>))
if b == nil {
return val, false
}

k.cdc.MustUnmarshal(b, &val)
return val, true
}

// Remove<%= TypeName.UpperCamel %> removes a <%= TypeName.LowerCamel %> from the store
func (k Keeper) Remove<%= TypeName.UpperCamel %>(
ctx context.Context,
<%= for (i, index) in Indexes { %><%= index.Name.LowerCamel %> <%= index.DataType() %>,
<% } %>
) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>KeyPrefix))
store.Delete(types.<%= TypeName.UpperCamel %>Key(
<%= for (i, index) in Indexes { %><%= index.Name.LowerCamel %>,
<% } %>))
}

// GetAll<%= TypeName.UpperCamel %> returns all <%= TypeName.LowerCamel %>
func (k Keeper) GetAll<%= TypeName.UpperCamel %>(ctx context.Context) (list []types.<%= TypeName.UpperCamel %>) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>KeyPrefix))
iterator := storetypes.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val types.<%= TypeName.UpperCamel %>
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}

return
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,47 @@ import (
"context"

"<%= ModulePath %>/x/<%= ModuleName %>/types"
"cosmossdk.io/collections"
errorsmod "cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
errorsmod "cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)


func (k msgServer) Create<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgCreate<%= TypeName.UpperCamel %>) (*types.MsgCreate<%= TypeName.UpperCamel %>Response, error) {
// Check if the value already exists
// TODO need to limit max nb of indexes
ok, err := k.<%= TypeName.UpperCamel %>.Has(
_, isFound := k.Get<%= TypeName.UpperCamel %>(
ctx,
<%= for (i, index) in Indexes { %>msg.<%= index.Name.UpperCamel %>,
<% } %>)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, err.Error())
} else if ok {
if isFound {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "index already set")
}

var <%= TypeName.LowerCamel %> = types.<%= TypeName.UpperCamel %>{
<%= MsgSigner.UpperCamel %>: msg.<%= MsgSigner.UpperCamel %>,
<%= for (i, index) in Indexes { %><%= index.Name.UpperCamel %>: msg.<%= index.Name.UpperCamel %>,
<%= MsgSigner.UpperCamel %>: msg.<%= MsgSigner.UpperCamel %>,
<%= for (i, index) in Indexes { %><%= index.Name.UpperCamel %>: msg.<%= index.Name.UpperCamel %>,
<% } %><%= for (field) in Fields { %><%= field.Name.UpperCamel %>: msg.<%= field.Name.UpperCamel %>,
<% } %>
}
<% } %>
}

if err := k.<%= TypeName.UpperCamel %>.Set(
k.Set<%= TypeName.UpperCamel %>(
ctx,
<%= for (i, index) in Indexes { %>msg.<%= index.Name.UpperCamel %>,
<% } %>
<%= TypeName.LowerCamel %>,
); err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, err.Error())
}

return &types.MsgCreate<%= TypeName.UpperCamel %>Response{}, nil
)
return &types.MsgCreate<%= TypeName.UpperCamel %>Response{}, nil
}

func (k msgServer) Update<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgUpdate<%= TypeName.UpperCamel %>) (*types.MsgUpdate<%= TypeName.UpperCamel %>Response, error) {
// Check if the value exists
val, err := k.<%= TypeName.UpperCamel %>.Get(
valFound, isFound := k.Get<%= TypeName.UpperCamel %>(
ctx,
<%= for (i, index) in Indexes { %>msg.<%= index.Name.UpperCamel %>,
<% } %>)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, "index not set")
}

return nil, errorsmod.Wrap(sdkerrors.ErrLogic, err.Error())
if !isFound {
return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, "index not set")
}

// Checks if the msg <%= MsgSigner.LowerCamel %> is the same as the current owner
if msg.<%= MsgSigner.UpperCamel %> != val.<%= MsgSigner.UpperCamel %> {
if msg.<%= MsgSigner.UpperCamel %> != valFound.<%= MsgSigner.UpperCamel %> {
return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
}

Expand All @@ -68,33 +55,27 @@ func (k msgServer) Update<%= TypeName.UpperCamel %>(ctx context.Context, msg *t
<% } %>
}

if err := k.<%= TypeName.UpperCamel %>.Set(ctx, <%= TypeName.LowerCamel %>.Index, <%= TypeName.LowerCamel %>); err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "failed to set <%= TypeName.LowerCamel %>")
}
k.Set<%= TypeName.UpperCamel %>(ctx, <%= TypeName.LowerCamel %>)

return &types.MsgUpdate<%= TypeName.UpperCamel %>Response{}, nil
}

func (k msgServer) Delete<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgDelete<%= TypeName.UpperCamel %>) (*types.MsgDelete<%= TypeName.UpperCamel %>Response, error) {
// Check if the value exists
val, err := k.<%= TypeName.UpperCamel %>.Get(
valFound, isFound := k.Get<%= TypeName.UpperCamel %>(
ctx,
<%= for (i, index) in Indexes { %>msg.<%= index.Name.UpperCamel %>,
<% } %>)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, "index not set")
}

return nil, errorsmod.Wrap(sdkerrors.ErrLogic, err.Error())
if !isFound {
return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, "index not set")
}

// Checks if the msg <%= MsgSigner.LowerCamel %> is the same as the current owner
if msg.<%= MsgSigner.UpperCamel %> != val.<%= MsgSigner.UpperCamel %> {
if msg.<%= MsgSigner.UpperCamel %> != valFound.<%= MsgSigner.UpperCamel %> {
return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
}

k.<%= TypeName.UpperCamel %>.Remove(
k.Remove<%= TypeName.UpperCamel %>(
ctx,
<%= for (i, index) in Indexes { %>msg.<%= index.Name.UpperCamel %>,
<% } %>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func SimulateMsgCreate<%= TypeName.UpperCamel %>(
<%= index.Name.UpperCamel %>: <%= index.ValueLoop() %>,<% } %>
}

found, err := k.<%= TypeName.UpperCamel %>.Has(ctx <%= for (index) in Indexes { %>, msg.<%= index.Name.UpperCamel %><% } %>)
if err == nil && found {
_, found := k.Get<%= TypeName.UpperCamel %>(ctx <%= for (index) in Indexes { %>, msg.<%= index.Name.UpperCamel %><% } %>)
if found {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "<%= TypeName.UpperCamel %> already exist"), nil, nil
}

Expand Down Expand Up @@ -64,18 +64,9 @@ func SimulateMsgUpdate<%= TypeName.UpperCamel %>(
simAccount = simtypes.Account{}
<%= TypeName.LowerCamel %> = types.<%= TypeName.UpperCamel %>{}
msg = &types.MsgUpdate<%= TypeName.UpperCamel %>{}
all<%= TypeName.UpperCamel %> = k.GetAll<%= TypeName.UpperCamel %>(ctx)
found = false
)

var all<%= TypeName.UpperCamel %> []types.<%= TypeName.UpperCamel %>
err := k.<%= TypeName.UpperCamel %>.Walk(ctx, nil, func(key string, value types.<%= TypeName.UpperCamel %>) (stop bool, err error) {
all<%= TypeName.UpperCamel %> = append(all<%= TypeName.UpperCamel %>, value)
return false, nil
})
if err != nil {
panic(err)
}

for _, obj := range all<%= TypeName.UpperCamel %> {
simAccount, found = FindAccount(accs, obj.<%= MsgSigner.UpperCamel %>)
if found {
Expand Down Expand Up @@ -118,18 +109,9 @@ func SimulateMsgDelete<%= TypeName.UpperCamel %>(
simAccount = simtypes.Account{}
<%= TypeName.LowerCamel %> = types.<%= TypeName.UpperCamel %>{}
msg = &types.MsgUpdate<%= TypeName.UpperCamel %>{}
all<%= TypeName.UpperCamel %> = k.GetAll<%= TypeName.UpperCamel %>(ctx)
found = false
)

var all<%= TypeName.UpperCamel %> []types.<%= TypeName.UpperCamel %>
err := k.<%= TypeName.UpperCamel %>.Walk(ctx, nil, func(key string, value types.<%= TypeName.UpperCamel %>) (stop bool, err error) {
all<%= TypeName.UpperCamel %> = append(all<%= TypeName.UpperCamel %>, value)
return false, nil
})
if err != nil {
panic(err)
}

for _, obj := range all<%= TypeName.UpperCamel %> {
simAccount, found = FindAccount(accs, obj.<%= MsgSigner.UpperCamel %>)
if found {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package keeper_test

import (
"context"
"strconv"
"testing"

"<%= ModulePath %>/x/<%= ModuleName %>/keeper"
"<%= ModulePath %>/x/<%= ModuleName %>/types"
keepertest "<%= ModulePath %>/testutil/keeper"
"<%= ModulePath %>/testutil/nullify"
"github.com/stretchr/testify/require"
)

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

func createN<%= TypeName.UpperCamel %>(keeper keeper.Keeper, ctx context.Context, n int) []types.<%= TypeName.UpperCamel %> {
items := make([]types.<%= TypeName.UpperCamel %>, n)
for i := range items {
<%= for (i, index) in Indexes { %>items[i].<%= index.Name.UpperCamel %> = <%= index.ValueLoop() %>
<% } %>
keeper.Set<%= TypeName.UpperCamel %>(ctx, items[i])
}
return items
}

func Test<%= TypeName.UpperCamel %>Get(t *testing.T) {
keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t)
items := createN<%= TypeName.UpperCamel %>(keeper, ctx, 10)
for _, item := range items {
rst, found := keeper.Get<%= TypeName.UpperCamel %>(ctx,
<%= for (i, index) in Indexes { %>item.<%= index.Name.UpperCamel %>,
<% } %>
)
require.True(t, found)
require.Equal(t,
nullify.Fill(&item),
nullify.Fill(&rst),
)
}
}
func Test<%= TypeName.UpperCamel %>Remove(t *testing.T) {
keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t)
items := createN<%= TypeName.UpperCamel %>(keeper, ctx, 10)
for _, item := range items {
keeper.Remove<%= TypeName.UpperCamel %>(ctx,
<%= for (i, index) in Indexes { %>item.<%= index.Name.UpperCamel %>,
<% } %>
)
_, found := keeper.Get<%= TypeName.UpperCamel %>(ctx,
<%= for (i, index) in Indexes { %>item.<%= index.Name.UpperCamel %>,
<% } %>
)
require.False(t, found)
}
}

func Test<%= TypeName.UpperCamel %>GetAll(t *testing.T) {
keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t)
items := createN<%= TypeName.UpperCamel %>(keeper, ctx, 10)
require.ElementsMatch(t,
nullify.Fill(items),
nullify.Fill(keeper.GetAll<%= TypeName.UpperCamel %>(ctx)),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ func Test<%= TypeName.UpperCamel %>MsgServerCreate(t *testing.T) {
}
_, err := srv.Create<%= TypeName.UpperCamel %>(ctx, expected)
require.NoError(t, err)
rst, err := k.<%= TypeName.UpperCamel %>.Get(ctx,
rst, found := k.Get<%= TypeName.UpperCamel %>(ctx,
<%= for (i, index) in Indexes { %>expected.<%= index.Name.UpperCamel %>,
<% } %>
)
require.NoError(t, err)
require.True(t, found)
require.Equal(t, expected.<%= MsgSigner.UpperCamel %>, rst.<%= MsgSigner.UpperCamel %>)
}
}
Expand Down Expand Up @@ -83,11 +83,11 @@ func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) {
require.ErrorIs(t, err, tc.err)
} else {
require.NoError(t, err)
rst, err := k.<%= TypeName.UpperCamel %>.Get(ctx,
rst, found := k.Get<%= TypeName.UpperCamel %>(ctx,
<%= for (i, index) in Indexes { %>expected.<%= index.Name.UpperCamel %>,
<% } %>
)
require.NoError(t, err)
require.True(t, found)
require.Equal(t, expected.<%= MsgSigner.UpperCamel %>, rst.<%= MsgSigner.UpperCamel %>)
}
})
Expand Down Expand Up @@ -141,11 +141,10 @@ func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) {
require.ErrorIs(t, err, tc.err)
} else {
require.NoError(t, err)
found, err := k.<%= TypeName.UpperCamel %>.Has(ctx,
_, found := k.Get<%= TypeName.UpperCamel %>(ctx,
<%= for (i, index) in Indexes { %>tc.request.<%= index.Name.UpperCamel %>,
<% } %>
)
require.NoError(t, err)
require.False(t, found)
}
})
Expand Down
Loading

0 comments on commit 3f1c59d

Please sign in to comment.