mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
cmd/internal/base: add name conflict check for init command (#1110)
* adds checks for name conflicts also tidies up error handling inside init * fix failing test, check for lower name for golang idents * update function name and prefer builtin function * pkg instead of lowerName
This commit is contained in:
committed by
GitHub
parent
56b3db8448
commit
cfefd26543
@@ -83,7 +83,7 @@ func InitCmd() *cobra.Command {
|
|||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, names []string) {
|
Run: func(cmd *cobra.Command, names []string) {
|
||||||
if err := initEnv(target, names); err != nil {
|
if err := initEnv(target, names); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(fmt.Errorf("ent/init: %w", err))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -179,16 +179,19 @@ func GenerateCmd(postRun ...func(*gen.Config)) *cobra.Command {
|
|||||||
// initEnv initialize an environment for ent codegen.
|
// initEnv initialize an environment for ent codegen.
|
||||||
func initEnv(target string, names []string) error {
|
func initEnv(target string, names []string) error {
|
||||||
if err := createDir(target); err != nil {
|
if err := createDir(target); err != nil {
|
||||||
return err
|
return fmt.Errorf("create dir %s: %w", target, err)
|
||||||
}
|
}
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
|
if err := gen.ValidSchemaName(name); err != nil {
|
||||||
|
return fmt.Errorf("init schema %s: %w", name, err)
|
||||||
|
}
|
||||||
b := bytes.NewBuffer(nil)
|
b := bytes.NewBuffer(nil)
|
||||||
if err := tmpl.Execute(b, name); err != nil {
|
if err := tmpl.Execute(b, name); err != nil {
|
||||||
log.Fatalln(err)
|
return fmt.Errorf("executing template %s: %w", name, err)
|
||||||
}
|
}
|
||||||
target := filepath.Join(target, strings.ToLower(name+".go"))
|
newFileTarget := filepath.Join(target, strings.ToLower(name+".go"))
|
||||||
if err := ioutil.WriteFile(target, b.Bytes(), 0644); err != nil {
|
if err := ioutil.WriteFile(newFileTarget, b.Bytes(), 0644); err != nil {
|
||||||
log.Fatalln(err)
|
return fmt.Errorf("writing file %s: %w", newFileTarget, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ func NewType(c *Config, schema *load.Schema) (*Type, error) {
|
|||||||
fields: make(map[string]*Field, len(schema.Fields)),
|
fields: make(map[string]*Field, len(schema.Fields)),
|
||||||
foreignKeys: make(map[string]struct{}),
|
foreignKeys: make(map[string]struct{}),
|
||||||
}
|
}
|
||||||
if err := typ.check(); err != nil {
|
if err := ValidSchemaName(typ.Name); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, f := range schema.Fields {
|
for _, f := range schema.Fields {
|
||||||
@@ -680,17 +680,20 @@ func (t Type) RelatedTypes() []*Type {
|
|||||||
return related
|
return related
|
||||||
}
|
}
|
||||||
|
|
||||||
// check checks the schema type.
|
// ValidSchemaName will determine if a name is going to conflict with any
|
||||||
func (t *Type) check() error {
|
// pre-defined names
|
||||||
pkg := t.Package()
|
func ValidSchemaName(name string) error {
|
||||||
|
// schema package is lower-cased (see Type.Package)
|
||||||
|
pkg := strings.ToLower(name)
|
||||||
|
|
||||||
if token.Lookup(pkg).IsKeyword() {
|
if token.Lookup(pkg).IsKeyword() {
|
||||||
return fmt.Errorf("schema lowercase name conflicts with Go keyword %q", pkg)
|
return fmt.Errorf("schema lowercase name conflicts with Go keyword %q", pkg)
|
||||||
}
|
}
|
||||||
if types.Universe.Lookup(pkg) != nil {
|
if types.Universe.Lookup(pkg) != nil {
|
||||||
return fmt.Errorf("schema lowercase name conflicts with Go predeclared identifier %q", pkg)
|
return fmt.Errorf("schema lowercase name conflicts with Go predeclared identifier %q", pkg)
|
||||||
}
|
}
|
||||||
if _, ok := globalIdent[t.Name]; ok {
|
if _, ok := globalIdent[name]; ok {
|
||||||
return fmt.Errorf("schema name conflicts with ent predeclared identifier %q", t.Name)
|
return fmt.Errorf("schema name conflicts with ent predeclared identifier %q", name)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user