8000 feat: scaffold singleton type by lumtis · Pull Request #1269 · ignite/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: scaffold singleton type #1269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Added the flag `--no-default-module` to the command `starport app` to prevent scaffolding a default module when creating a new app
- Added support for multiple naming conventions for component names and field names
- Print created and modified files when scaffolding a new component
- Scaffold a type that contains a single instance in the store
- Introduce `starport tools` command for advanced users. Existing `starport relayer lowlevel *` commands are also moved under `tools`.
- Added `faucet.rate_limit_window` property to `config.yml`

Expand Down
52 changes: 52 additions & 0 deletions integration/cmd_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,55 @@ func TestCreateMapWithStargate(t *testing.T) {

env.EnsureAppIsSteady(path)
}

func TestCreateSingletonTypeWithStargate(t *testing.T) {
var (
env = newEnv(t)
path = env.Scaffold("blog")
)

env.Must(env.Exec("create an singleton type",
step.NewSteps(step.New(
step.Exec("starport", "s", "single", "user", "email"),
step.Workdir(path),
)),
))

env.Must(env.Exec("create an singleton type with no message",
step.NewSteps(step.New(
step.Exec("starport", "s", "single", "no-message", "email", "--no-message"),
step.Workdir(path),
)),
))

env.Must(env.Exec("create a module",
step.NewSteps(step.New(
step.Exec("starport", "s", "module", "example", "--require-registration"),
step.Workdir(path),
)),
))

env.Must(env.Exec("create another type",
step.NewSteps(step.New(
step.Exec("starport", "s", "list", "user", "email", "--module", "example"),
step.Workdir(path),
)),
))

env.Must(env.Exec("should prevent creating an singleton type with a typename that already exist",
step.NewSteps(step.New(
step.Exec("starport", "s", "single", "user", "email", "--module", "example"),
step.Workdir(path),
)),
ExecShouldError(),
))

env.Must(env.Exec("create an singleton type in a custom module",
step.NewSteps(step.New(
step.Exec("starport", "s", "single", "singleuser", "email", "--module", "example"),
step.Workdir(path),
)),
))

env.EnsureAppIsSteady(path)
}
13 changes: 11 additions & 2 deletions starport/cmd/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (
"github.com/tendermint/starport/starport/services/scaffolder"
)

// flags related to component scaffolding
const (
flagModule = "module"
flagNoMessage = "no-message"
flagResponse = "response"
flagDescription = "desc"
)

// NewScaffold returns a command that groups scaffolding related sub commands.
func NewScaffold() *cobra.Command {
c := &cobra.Command{
Expand All @@ -23,6 +31,7 @@ func NewScaffold() *cobra.Command {
c.AddCommand(NewScaffoldModule())
c.AddCommand(NewScaffoldList())
c.AddCommand(NewScaffoldMap())
c.AddCommand(NewScaffoldSingle())
c.AddCommand(NewScaffoldMessage())
c.AddCommand(NewScaffoldQuery())
c.AddCommand(NewScaffoldPacket())
Expand All @@ -32,7 +41,7 @@ func NewScaffold() *cobra.Command {
return c
}

func scaffoldType(kind, module, typeName string, typeFields []string, opts scaffolder.AddTypeOption) error {
func scaffoldType(module, typeName string, typeFields []string, opts scaffolder.AddTypeOption) error {
s := clispinner.New().SetText("Scaffolding...")
defer s.Stop()

Expand All @@ -48,7 +57,7 @@ func scaffoldType(kind, module, typeName string, typeFields []string, opts scaff
s.Stop()

fmt.Println(sourceModificationToString(sm))
fmt.Printf("\n🎉 Created a %s `%s`.\n\n", kind, typeName)
fmt.Printf("\n🎉 Created a %s `%s`.\n\n", opts.Model, typeName)

return nil
}
Expand Down
3 changes: 2 additions & 1 deletion starport/cmd/scaffold_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func NewScaffoldList() *cobra.Command {
func scaffoldListHandler(cmd *cobra.Command, args []string) error {
opts := scaffolder.AddTypeOption{
NoMessage: flagGetNoMessage(cmd),
Model: scaffolder.List,
}

return scaffoldType("list", flagGetModule(cmd), args[0], args[1:], opts)
return scaffoldType(flagGetModule(cmd), args[0], args[1:], opts)
}
9 changes: 2 additions & 7 deletions starport/cmd/scaffold_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import (
"github.com/tendermint/starport/starport/services/scaffolder"
)

const (
flagModule = "module"
flagNoMessage = "no-message"
)

// NewScaffoldMap returns a new command to scaffold a map.
func NewScaffoldMap() *cobra.Command {
c := &cobra.Command{
Expand All @@ -27,9 +22,9 @@ func NewScaffoldMap() *cobra.Command {

func scaffoldMapHandler(cmd *cobra.Command, args []string) error {
opts := scaffolder.AddTypeOption{
Indexed: true,
NoMessage: flagGetNoMessage(cmd),
Model: scaffolder.Map,
}

return scaffoldType("map", flagGetModule(cmd), args[0], args[1:], opts)
return scaffoldType(flagGetModule(cmd), args[0], args[1:], opts)
}
5 changes: 0 additions & 5 deletions starport/cmd/scaffold_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import (
"github.com/tendermint/starport/starport/services/scaffolder"
)

const (
flagResponse string = "response"
flagDescription string = "desc"
)

// NewScaffoldMessage returns the command to scaffold messages
func NewScaffoldMessage() *cobra.Command {
c := &cobra.Command{
Expand Down
30 changes: 30 additions & 0 deletions starport/cmd/scaffold_single.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package starportcmd

import (
"github.com/spf13/cobra"
"github.com/tendermint/starport/starport/services/scaffolder"
)

// NewScaffoldSingle returns a new command to scaffold a singleton.
func NewScaffoldSingle() *cobra.Command {
c := &cobra.Command{
Use: "single NAME [field]...",
Short: "Scaffold a singleton",
Args: cobra.MinimumNArgs(1),
RunE: scaffoldSingleHandler,
}

c.Flags().StringVarP(&appPath, "path", "p", "", "path of the app")
c.Flags().AddFlagSet(flagSetScaffoldType())

return c
}

func scaffoldSingleHandler(cmd *cobra.Command, args []string) error {
opts := scaffolder.AddTypeOption{
NoMessage: flagGetNoMessage(cmd),
Model: scaffolder.Singleton,
}

return scaffoldType(flagGetModule(cmd), args[0], args[1:], opts)
}
29 changes: 23 additions & 6 deletions starport/services/scaffolder/type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scaffolder

import (
"errors"
"fmt"
"os"

Expand All @@ -13,10 +14,19 @@ import (
modulecreate "github.com/tendermint/starport/starport/templates/module/create"
"github.com/tendermint/starport/starport/templates/typed"
"github.com/tendermint/starport/starport/templates/typed/indexed"
"github.com/tendermint/starport/starport/templates/typed/singleton"
)

type TypeModel string

const (
List TypeModel = "list"
Map TypeModel = "map"
Singleton TypeModel = "singleton"
)

type AddTypeOption struct {
Indexed bool
Model TypeModel
NoMessage bool
}

Expand Down Expand Up @@ -89,16 +99,23 @@ func (s *Scaffolder) AddType(
gens = append(gens, g)
}

// Check if indexed type
if addTypeOptions.Indexed {
g, err = indexed.NewStargate(tracer, opts)
} else {
// Scaffolding a type with ID
// create the type generator depending on the model
// TODO: rename the template packages to make it consistent with the type new naming
switch addTypeOptions.Model {
case List:
g, err = typed.NewStargate(tracer, opts)
case Map:
g, err = indexed.NewStargate(tracer, opts)
case Singleton:
g, err = singleton.NewStargate(tracer, opts)
default:
return sm, errors.New("unrecognized type model")
}
if err != nil {
return sm, err
}

// run the generation
gens = append(gens, g)
sm, err = xgenny.RunWithValidation(tracer, gens...)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (k Keeper) Get<%= TypeName.UpperCamel %>(ctx sdk.Context, index string) (va
return val, true
}

// Delete<%= TypeName.UpperCamel %> removes a <%= TypeName.LowerCamel %> from the store
// Remove<%= TypeName.UpperCamel %> removes a <%= TypeName.LowerCamel %> from the store
func (k Keeper) Remove<%= TypeName.UpperCamel %>(ctx sdk.Context, index string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.<%= TypeName.UpperCamel %>Key))
store.Delete(types.KeyPrefix(index))
Expand Down
Loading
0