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
87 changes: 87 additions & 0 deletions starport/services/scaffolder/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package scaffolder

import (
"go/token"
"os"
"path/filepath"
)

// isMsgServerDefined checks if the module uses the MsgServer convention for transactions
// this is checked by verifying the existence of the tx.proto file
func isMsgServerDefined(appPath, moduleName string) (bool, error) {
txProto, err := filepath.Abs(filepath.Join(appPath, "proto", moduleName, "tx.proto"))
if err != nil {
return false, err
}

if _, err := os.Stat(txProto); os.IsNotExist(err) {
return false, nil
}
return true, err
}

// isForbiddenComponentName returns true if the name is forbidden as a component name
func isForbiddenComponentName(name string) bool {
switch name {
case
"logger",
"keeper",
"query",
"genesis",
"types",
"tx":
return true
}

return isGoReservedWord(name)
}

func isGoReservedWord(name string) bool {
// Check keyword or literal
if token.Lookup(name).IsKeyword() {
return true
}

// Check with builtin identifier
switch name {
case
"panic",
"recover",
"append",
"bool",
"byte",
"cap",
"close",
"complex",
"complex64",
"complex128",
"uint16",
"copy",
"false",
"float32",
"float64",
"imag",
"int",
"int8",
"int16",
"uint32",
"int32",
"int64",
"iota",
"len",
"make",
"new",
"nil",
"uint64",
"print",
"println",
"real",
"string",
"true",
"uint",
"uint8",
"uintptr":
return true
}
return false
}
6 changes: 3 additions & 3 deletions starport/services/scaffolder/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func (s *Scaffolder) AddMessage(moduleName string, msgName string, msgDesc strin
return fmt.Errorf("the module %s doesn't exist", moduleName)
}

// Ensure the msg name is not a Go reserved name, it would generate an incorrect code
if isGoReservedWord(msgName) {
return fmt.Errorf("%s can't be used as a type name", msgName)
// Ensure the name is valid, otherwise it would generate an incorrect code
if isForbiddenComponentName(msgName) {
return fmt.Errorf("%s can't be used as a packet name", msgName)
}

// Check msg is not already created
Expand Down
5 changes: 5 additions & 0 deletions starport/services/scaffolder/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func (s *Scaffolder) AddPacket(moduleName string, packetName string, packetField
return fmt.Errorf("the module %s doesn't implement IBC module interface", moduleName)
}

// Ensure the name is valid, otherwise it would generate an incorrect code
if isForbiddenComponentName(packetName) {
return fmt.Errorf("%s can't be used as a packet name", packetName)
}

// Check packet doesn't exist
ok, err = isPacketCreated(s.path, moduleName, packetName)
if err != nil {
Expand Down
78 changes: 7 additions & 71 deletions starport/services/scaffolder/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type AddTypeOption struct {
}

// AddType adds a new type stype to scaffolded app by using optional type fields.
func (s *Scaffolder) AddType(addTypeOptions AddTypeOption, moduleName string, stype string, fields ...string) error {
func (s *Scaffolder) AddType(addTypeOptions AddTypeOption, moduleName string, typeName string, fields ...string) error {
version, err := s.version()
if err != nil {
return err
Expand All @@ -54,18 +54,18 @@ func (s *Scaffolder) AddType(addTypeOptions AddTypeOption, moduleName string, st
return fmt.Errorf("the module %s doesn't exist", moduleName)
}

// Ensure the type name is not a Go reserved name, it would generate an incorrect code
if isGoReservedWord(stype) {
return fmt.Errorf("%s can't be used as a type name", stype)
// Ensure the type name is valid, otherwise it would generate an incorrect code
if isForbiddenComponentName(typeName) {
return fmt.Errorf("%s can't be used as a type name", typeName)
}

// Check type is not already created
ok, err = isTypeCreated(s.path, moduleName, stype)
ok, err = isTypeCreated(s.path, moduleName, typeName)
if err != nil {
return err
}
if ok {
return fmt.Errorf("%s type is already added", stype)
return fmt.Errorf("%s type is already added", typeName)
}

// Parse provided field
Expand All @@ -81,7 +81,7 @@ func (s *Scaffolder) AddType(addTypeOptions AddTypeOption, moduleName string, st
ModulePath: path.RawPath,
ModuleName: moduleName,
OwnerName: owner(path.RawPath),
TypeName: stype,
TypeName: typeName,
Fields: tFields,
Legacy: addTypeOptions.Legacy,
}
Expand Down Expand Up @@ -208,20 +208,6 @@ func isTypeCreated(appPath, moduleName, typeName string) (isCreated bool, err er
return
}

// isMsgServerDefined checks if the module uses the MsgServer convention for transactions
// this is checked by verifying the existence of the tx.proto file
func isMsgServerDefined(appPath, moduleName string) (bool, error) {
txProto, err := filepath.Abs(filepath.Join(appPath, "proto", moduleName, "tx.proto"))
if err != nil {
return false, err
}

if _, err := os.Stat(txProto); os.IsNotExist(err) {
return false, nil
}
return true, err
}

// isForbiddenTypeField returns true if the name is forbidden as a field name
func isForbiddenTypeField(name string) bool {
switch name {
Expand All @@ -234,53 +220,3 @@ func isForbiddenTypeField(name string) bool {

return isGoReservedWord(name)
}

func isGoReservedWord(name string) bool {
// Check keyword or literal
if token.Lookup(name).IsKeyword() {
return true
}

// Check with builtin identifier
switch name {
case
"panic",
"recover",
"append",
"bool",
"byte",
"cap",
"close",
"complex",
"complex64",
"complex128",
"uint16",
"copy",
"false",
"float32",
"float64",
"imag",
"int",
"int8",
"int16",
"uint32",
"int32",
"int64",
"iota",
"len",
"make",
"new",
"nil",
"uint64",
"print",
"println",
"real",
"string",
"true",
"uint",
"uint8",
"uintptr":
return true
}
return false
}