entc/gen: reject schemas whose package conflicts with ent predeclared identifiers (#1244)

This commit is contained in:
Ariel Mashraki
2021-02-06 16:17:52 +02:00
committed by GitHub
parent 528db137de
commit 15af025dd0
2 changed files with 15 additions and 3 deletions

View File

@@ -691,12 +691,12 @@ func ValidSchemaName(name string) error {
if types.Universe.Lookup(pkg) != nil {
return fmt.Errorf("schema lowercase name conflicts with Go predeclared identifier %q", pkg)
}
if _, ok := globalIdent[pkg]; ok {
return fmt.Errorf("schema lowercase name conflicts ent predeclared identifier %q", pkg)
}
if _, ok := globalIdent[name]; ok {
return fmt.Errorf("schema name conflicts with ent predeclared identifier %q", name)
}
if _, ok := privateField[pkg]; ok {
return fmt.Errorf("schema name conflicts with ent builder fields %q", pkg)
}
return nil
}
@@ -1348,6 +1348,7 @@ var (
"As",
"Asc",
"Client",
"config",
"Count",
"Debug",
"Desc",

View File

@@ -321,3 +321,14 @@ func TestEdge(t *testing.T) {
require.Equal(t, "user_groups", users.Label())
require.Equal(t, "user_groups", groups.Label())
}
func TestValidSchemaName(t *testing.T) {
err := ValidSchemaName("Config")
require.Error(t, err)
err = ValidSchemaName("Mutation")
require.Error(t, err)
err = ValidSchemaName("Boring")
require.NoError(t, err)
err = ValidSchemaName("Order")
require.NoError(t, err)
}