Skip to content
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 go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ golang.org/x/tools v0.0.0-20191224055732-dd894d0a8a40/go.mod h1:TB2adYChydJhpapK
golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200110213125-a7a6caa82ab2/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200308013534-11ec41452d41 h1:9Di9iYgOt9ThCipBxChBVhgNipDoE5mxO84rQV7D0FE=
golang.org/x/tools v0.0.0-20200308013534-11ec41452d41/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
57 changes: 55 additions & 2 deletions integration/cmd_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/tendermint/starport/starport/pkg/cmdrunner/step"
)

func TestGenerateAnAppWithTypeAndVerify(t *testing.T) {
func TestGenerateAnAppWithLaunchpadWithTypeAndVerify(t *testing.T) {
var (
env = newEnv(t)
path = env.Scaffold("blog", Launchpad)
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestGenerateAnAppWithStargateWithTypeAndVerify(t *testing.T) {
env.EnsureAppIsSteady(path)
}

func TestCreateTypeInCustomModule(t *testing.T) {
func TestCreateTypeInCustomModuleWithLaunchpad(t *testing.T) {
var (
env = newEnv(t)
path = env.Scaffold("blog", Launchpad)
Expand Down Expand Up @@ -198,6 +198,14 @@ func TestCreateTypeInCustomModule(t *testing.T) {
ExecShouldError(),
))

env.Must(env.Exec("should prevent creating an indexed type",
step.NewSteps(step.New(
step.Exec("starport", "type", "indexeduser", "email", "--indexed"),
step.Workdir(path),
)),
ExecShouldError(),
))

env.EnsureAppIsSteady(path)
}

Expand Down Expand Up @@ -253,3 +261,48 @@ func TestCreateTypeInCustomModuleWithStargate(t *testing.T) {

env.EnsureAppIsSteady(path)
}

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

env.Must(env.Exec("create an indexed type",
step.NewSteps(step.New(
step.Exec("starport", "type", "user", "email", "--indexed"),
step.Workdir(path),
)),
))

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

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

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

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

env.EnsureAppIsSteady(path)
}
17 changes: 13 additions & 4 deletions starport/interface/cli/starport/cmd/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

const (
moduleFlag string = "module"
legacyFlag string = "legacy"
moduleFlag string = "module"
legacyFlag string = "legacy"
indexedFlag string = "indexed"
)

// NewType command creates a new type command to scaffold types.
Expand All @@ -26,6 +27,7 @@ func NewType() *cobra.Command {

c.Flags().String(moduleFlag, "", "Module to add the type into. Default: app's main module")
c.Flags().Bool(legacyFlag, false, "Scaffold the type without generating MsgServer service")
c.Flags().Bool(indexedFlag, false, "Scaffold an indexed type")

return c
}
Expand All @@ -39,13 +41,20 @@ func typeHandler(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
legacy, err := cmd.Flags().GetBool(legacyFlag)

// Add type options
var opts scaffolder.AddTypeOption
opts.Legacy, err = cmd.Flags().GetBool(legacyFlag)
if err != nil {
return err
}
opts.Indexed, err = cmd.Flags().GetBool(indexedFlag)
if err != nil {
return err
}

sc := scaffolder.New(appPath)
if err := sc.AddType(legacy, module, args[0], args[1:]...); err != nil {
if err := sc.AddType(opts, module, args[0], args[1:]...); err != nil {
return err
}

Expand Down
41 changes: 30 additions & 11 deletions starport/services/scaffolder/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scaffolder

import (
"context"
"errors"
"fmt"
"go/ast"
"go/parser"
Expand All @@ -10,6 +11,8 @@ import (
"path/filepath"
"strings"

"github.com/tendermint/starport/starport/templates/typed/indexed"

"github.com/gobuffalo/genny"
"github.com/tendermint/starport/starport/pkg/cosmosver"
"github.com/tendermint/starport/starport/pkg/gomodulepath"
Expand All @@ -22,8 +25,13 @@ const (
TypeInt32 = "int32"
)

type AddTypeOption struct {
Legacy bool
Indexed bool
}

// AddType adds a new type stype to scaffolded app by using optional type fields.
func (s *Scaffolder) AddType(legacy bool, moduleName string, stype string, fields ...string) error {
func (s *Scaffolder) AddType(addTypeOptions AddTypeOption, moduleName string, stype string, fields ...string) error {
version, err := s.version()
if err != nil {
return err
Expand Down Expand Up @@ -75,23 +83,34 @@ func (s *Scaffolder) AddType(legacy bool, moduleName string, stype string, field
OwnerName: owner(path.RawPath),
TypeName: stype,
Fields: tFields,
Legacy: legacy,
Legacy: addTypeOptions.Legacy,
}
)
// generate depending on the version
if majorVersion == cosmosver.Launchpad {
if addTypeOptions.Indexed {
return errors.New("indexed types not supported on Launchpad")
}

g, err = typed.NewLaunchpad(opts)
} else {
// check if the msgServer convention is used
var msgServerDefined bool
msgServerDefined, err = isMsgServerDefined(s.path, moduleName)
if err != nil {
return err
}
if !msgServerDefined {
opts.Legacy = true
// Check if indexed type
if addTypeOptions.Indexed {
g, err = indexed.NewStargate(opts)
} else {
// Scaffolding a type with ID

// check if the msgServer convention is used
var msgServerDefined bool
msgServerDefined, err = isMsgServerDefined(s.path, moduleName)
if err != nil {
return err
}
if !msgServerDefined {
opts.Legacy = true
}
g, err = typed.NewStargate(opts)
}
g, err = typed.NewStargate(opts)
}
if err != nil {
return err
Expand Down
30 changes: 15 additions & 15 deletions starport/templates/typed/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ func (t *typedStargate) genesisProtoModify(opts *Options) genny.RunFn {

templateProtoImport := `%[1]v
import "%[2]v/%[3]v.proto";`
replacementProtoImport := fmt.Sprintf(templateProtoImport, placeholderGenesisProtoImport, opts.ModuleName, opts.TypeName)
content := strings.Replace(f.String(), placeholderGenesisProtoImport, replacementProtoImport, 1)
replacementProtoImport := fmt.Sprintf(templateProtoImport, PlaceholderGenesisProtoImport, opts.ModuleName, opts.TypeName)
content := strings.Replace(f.String(), PlaceholderGenesisProtoImport, replacementProtoImport, 1)

// Determine the new field number
fieldNumber := strings.Count(content, placeholderGenesisProtoStateField) + 1
fieldNumber := strings.Count(content, PlaceholderGenesisProtoStateField) + 1

templateProtoState := `%[1]v
repeated %[2]v %[3]vList = %[4]v; %[5]v`
replacementProtoState := fmt.Sprintf(
templateProtoState,
placeholderGenesisProtoState,
PlaceholderGenesisProtoState,
strings.Title(opts.TypeName),
opts.TypeName,
fieldNumber,
placeholderGenesisProtoStateField,
PlaceholderGenesisProtoStateField,
)
content = strings.Replace(content, placeholderGenesisProtoState, replacementProtoState, 1)
content = strings.Replace(content, PlaceholderGenesisProtoState, replacementProtoState, 1)

newFile := genny.NewFileS(path, content)
return r.File(newFile)
Expand All @@ -55,12 +55,12 @@ func (t *typedStargate) genesisTypesModify(opts *Options) genny.RunFn {
}

templateTypesImport := `"fmt"`
content := strings.Replace(f.String(), placeholderGenesisTypesImport, templateTypesImport, 1)
content := strings.Replace(f.String(), PlaceholderGenesisTypesImport, templateTypesImport, 1)

templateTypesDefault := `%[1]v
%[2]vList: []*%[2]v{},`
replacementTypesDefault := fmt.Sprintf(templateTypesDefault, placeholderGenesisTypesDefault, strings.Title(opts.TypeName))
content = strings.Replace(content, placeholderGenesisTypesDefault, replacementTypesDefault, 1)
replacementTypesDefault := fmt.Sprintf(templateTypesDefault, PlaceholderGenesisTypesDefault, strings.Title(opts.TypeName))
content = strings.Replace(content, PlaceholderGenesisTypesDefault, replacementTypesDefault, 1)

templateTypesValidate := `%[1]v
// Check for duplicated ID in %[2]v
Expand All @@ -74,11 +74,11 @@ for _, elem := range gs.%[3]vList {
}`
replacementTypesValidate := fmt.Sprintf(
templateTypesValidate,
placeholderGenesisTypesValidate,
PlaceholderGenesisTypesValidate,
opts.TypeName,
strings.Title(opts.TypeName),
)
content = strings.Replace(content, placeholderGenesisTypesValidate, replacementTypesValidate, 1)
content = strings.Replace(content, PlaceholderGenesisTypesValidate, replacementTypesValidate, 1)

newFile := genny.NewFileS(path, content)
return r.File(newFile)
Expand All @@ -104,11 +104,11 @@ k.Set%[3]vCount(ctx, int64(len(genState.%[3]vList)))
`
replacementModuleInit := fmt.Sprintf(
templateModuleInit,
placeholderGenesisModuleInit,
PlaceholderGenesisModuleInit,
opts.TypeName,
strings.Title(opts.TypeName),
)
content := strings.Replace(f.String(), placeholderGenesisModuleInit, replacementModuleInit, 1)
content := strings.Replace(f.String(), PlaceholderGenesisModuleInit, replacementModuleInit, 1)

templateModuleExport := `%[1]v
// Get all %[2]v
Expand All @@ -120,11 +120,11 @@ for _, elem := range %[2]vList {
`
replacementModuleExport := fmt.Sprintf(
templateModuleExport,
placeholderGenesisModuleExport,
PlaceholderGenesisModuleExport,
opts.TypeName,
strings.Title(opts.TypeName),
)
content = strings.Replace(content, placeholderGenesisModuleExport, replacementModuleExport, 1)
content = strings.Replace(content, PlaceholderGenesisModuleExport, replacementModuleExport, 1)

newFile := genny.NewFileS(path, content)
return r.File(newFile)
Expand Down
Loading