-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revert map due to high complexity to scaffold (map and indexmap and v…
…alue codec)
- Loading branch information
1 parent
24d3b83
commit 3f1c59d
Showing
7 changed files
with
174 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
ignite/templates/typed/map/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...plates/typed/map/files/tests/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.