Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Nov 6, 2023
1 parent 55fafe1 commit 742c1e2
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,83 @@ import (

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


func (k msgServer) Create<%= TypeName.UpperCamel %>(goCtx context.Context, msg *types.MsgCreate<%= TypeName.UpperCamel %>) (*types.MsgCreate<%= TypeName.UpperCamel %>Response, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

func (k msgServer) Create<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgCreate<%= TypeName.UpperCamel %>) (*types.MsgCreate<%= TypeName.UpperCamel %>Response, error) {
var <%= TypeName.LowerCamel %> = types.<%= TypeName.UpperCamel %>{
<%= MsgSigner.UpperCamel %>: msg.<%= MsgSigner.UpperCamel %>,<%= for (field) in Fields { %>
<%= field.Name.UpperCamel %>: msg.<%= field.Name.UpperCamel %>,<% } %>
}

id := k.Append<%= TypeName.UpperCamel %>(
nextId, err := k.<%= TypeName.UpperCamel %>Seq.Next(ctx)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "failed to get next id")
}

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

return &types.MsgCreate<%= TypeName.UpperCamel %>Response{
Id: id,
Id: nextId,
}, nil
}

func (k msgServer) Update<%= TypeName.UpperCamel %>(goCtx context.Context, msg *types.MsgUpdate<%= TypeName.UpperCamel %>) (*types.MsgUpdate<%= TypeName.UpperCamel %>Response, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

func (k msgServer) Update<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgUpdate<%= TypeName.UpperCamel %>) (*types.MsgUpdate<%= TypeName.UpperCamel %>Response, error) {
var <%= TypeName.LowerCamel %> = types.<%= TypeName.UpperCamel %>{
<%= MsgSigner.UpperCamel %>: msg.<%= MsgSigner.UpperCamel %>,
Id: msg.Id,<%= for (field) in Fields { %>
<%= field.Name.UpperCamel %>: msg.<%= field.Name.UpperCamel %>,<% } %>
}

// Checks that the element exists
val, found := k.Get<%= TypeName.UpperCamel %>(ctx, msg.Id)
if !found {
return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
val, err := k.<%= TypeName.UpperCamel %>.Get(ctx, msg.Id)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
}

return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "failed to get <%= TypeName.LowerCamel %>")
}

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

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

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

func (k msgServer) Delete<%= TypeName.UpperCamel %>(goCtx context.Context, msg *types.MsgDelete<%= TypeName.UpperCamel %>) (*types.MsgDelete<%= TypeName.UpperCamel %>Response, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

func (k msgServer) Delete<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgDelete<%= TypeName.UpperCamel %>) (*types.MsgDelete<%= TypeName.UpperCamel %>Response, error) {
// Checks that the element exists
val, found := k.Get<%= TypeName.UpperCamel %>(ctx, msg.Id)
if !found {
return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
val, err := k.<%= TypeName.UpperCamel %>.Get(ctx, msg.Id)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
}

return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "failed to get <%= TypeName.LowerCamel %>")
}

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

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

return &types.MsgDelete<%= TypeName.UpperCamel %>Response{}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,18 @@ 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 uint64, 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 @@ -97,9 +106,18 @@ 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 uint64, 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
24 changes: 20 additions & 4 deletions ignite/templates/typed/list/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,15 @@ func genesisModuleModify(replacer placeholder.Replacer, opts *typed.Options) gen

templateModuleInit := `// Set all the %[2]v
for _, elem := range genState.%[3]vList {
k.Set%[3]v(ctx, elem)
if err := k.%[3]v.Set(ctx, elem.Id, elem); err != nil {
panic(err)
}

Check warning on line 128 in ignite/templates/typed/list/genesis.go

View check run for this annotation

Codecov / codecov/patch

ignite/templates/typed/list/genesis.go#L126-L128

Added lines #L126 - L128 were not covered by tests
}
// Set %[2]v count
k.Set%[3]vCount(ctx, genState.%[3]vCount)
if err := k.%[3]vSeq.Set(ctx, genState.%[3]vCount); err != nil {
panic(err)
}

Check warning on line 134 in ignite/templates/typed/list/genesis.go

View check run for this annotation

Codecov / codecov/patch

ignite/templates/typed/list/genesis.go#L132-L134

Added lines #L132 - L134 were not covered by tests
%[1]v`
replacementModuleInit := fmt.Sprintf(
templateModuleInit,
Expand All @@ -137,8 +141,20 @@ k.Set%[3]vCount(ctx, genState.%[3]vCount)
)
content := replacer.Replace(f.String(), typed.PlaceholderGenesisModuleInit, replacementModuleInit)

templateModuleExport := `genesis.%[2]vList = k.GetAll%[2]v(ctx)
genesis.%[2]vCount = k.Get%[2]vCount(ctx)
templateModuleExport := `
err = k.%[2]v.Walk(ctx, nil, func(key uint64, elem types.%[2]v) (bool, error) {
genesis.%[2]vList = append(genesis.%[2]vList, elem)
return false, nil
})
if err != nil {
panic(err)
}
genesis.%[2]vCount, err = k.%[2]vSeq.Peek(ctx)
if err != nil {
panic(err)
}

Check warning on line 157 in ignite/templates/typed/list/genesis.go

View check run for this annotation

Codecov / codecov/patch

ignite/templates/typed/list/genesis.go#L144-L157

Added lines #L144 - L157 were not covered by tests
%[1]v`
replacementModuleExport := fmt.Sprintf(
templateModuleExport,
Expand Down
7 changes: 7 additions & 0 deletions ignite/templates/typed/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import (
"github.com/ignite/cli/ignite/templates/typed"
)

/*
TODO add the following in keeper.go
TypeNameSeq collections.Sequence
TypeName collections.Map[uint64, types.TypeName]
*/

var (
//go:embed files/component/* files/component/**/*
fsComponent embed.FS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import (

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


func (k msgServer) Create<%= TypeName.UpperCamel %>(goCtx context.Context, msg *types.MsgCreate<%= TypeName.UpperCamel %>) (*types.MsgCreate<%= TypeName.UpperCamel %>Response, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

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
_, isFound := k.Get<%= TypeName.UpperCamel %>(
ok, err := k.<%= TypeName.UpperCamel %>.Has(
ctx,
<%= for (i, index) in Indexes { %>msg.<%= index.Name.UpperCamel %>,
<% } %>)
if isFound {
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, err.Error())
} else if ok {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "index already set")
}

Expand All @@ -29,16 +28,19 @@ func (k msgServer) Create<%= TypeName.UpperCamel %>(goCtx context.Context, msg
<% } %>
}

k.Set<%= TypeName.UpperCamel %>(
if err := k.Set<%= TypeName.UpperCamel %>(
ctx,
<%= for (i, index) in Indexes { %>msg.<%= index.Name.UpperCamel %>,
<% } %>
<%= TypeName.LowerCamel %>,
)
return &types.MsgCreate<%= TypeName.UpperCamel %>Response{}, nil
); err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "failed to set <%= index.Name.UpperCamel %>")
}

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

func (k msgServer) Update<%= TypeName.UpperCamel %>(goCtx context.Context, msg *types.MsgUpdate<%= TypeName.UpperCamel %>) (*types.MsgUpdate<%= TypeName.UpperCamel %>Response, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

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
valFound, isFound := k.Get<%= TypeName.UpperCamel %>(
ctx,
Expand All @@ -65,9 +67,7 @@ func (k msgServer) Update<%= TypeName.UpperCamel %>(goCtx context.Context, msg
return &types.MsgUpdate<%= TypeName.UpperCamel %>Response{}, nil
}

func (k msgServer) Delete<%= TypeName.UpperCamel %>(goCtx context.Context, msg *types.MsgDelete<%= TypeName.UpperCamel %>) (*types.MsgDelete<%= TypeName.UpperCamel %>Response, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

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
valFound, isFound := k.Get<%= TypeName.UpperCamel %>(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ 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 uint64, 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 @@ -109,9 +118,18 @@ 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 uint64, 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
5 changes: 5 additions & 0 deletions ignite/templates/typed/map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
"github.com/ignite/cli/ignite/templates/typed"
)

/*
TODO add the following in keeper.go
TypeName collections.Map[string, types.TypeName]
*/

var (
//go:embed files/messages/* files/messages/**/*
fsMessages embed.FS
Expand Down

0 comments on commit 742c1e2

Please sign in to comment.