8000 entc/gen: allow spaces in enum fields by a8m · Pull Request #1977 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

entc/gen: allow spaces in enum fields #1977

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 1 commit into from
Sep 23, 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
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ linters-settings:
dupl:
threshold: 100
funlen:
lines: 110
statements: 100
lines: 115
statements: 115
goheader:
template: |-
Copyright 2019-present Facebook Inc. All rights reserved.
Expand Down
12 changes: 9 additions & 3 deletions dialect/sql/schema/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,15 @@ func (d *MySQL) scanColumn(c *Column, rows *sql.Rows) error {
c.Type = field.TypeJSON
case "enum":
c.Type = field.TypeEnum
c.Enums = make([]string, len(parts)-1)
for i, e := range parts[1:] {
c.Enums[i] = strings.Trim(e, "'")
// Parse the enum values according to the MySQL format.
// github.com/mysql/mysql-server/blob/8.0/sql/field.cc#Field_enum::sql_type
values := strings.TrimSuffix(strings.TrimPrefix(c.typ, "enum("), ")")
if values == "" {
return fmt.Errorf("mysql: unexpected enum type: %q", c.typ)
}
parts := strings.Split(values, "','")
for i := range parts {
c.Enums = append(c.Enums, strings.Trim(parts[i], "'"))
}
case "char":
c.Type = field.TypeOther
Expand Down
8 changes: 5 additions & 3 deletions dialect/sql/schema/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ func TestMySQL_Create(t *testing.T) {
Columns: []*Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "name", Type: field.TypeString, Nullable: true},
{Name: "enums1", Type: field.TypeEnum, Enums: []string{"a", "b"}}, // add enum.
{Name: "enums2", Type: field.TypeEnum, Enums: []string{"a"}}, // remove enum.
{Name: "enums1", Type: field.TypeEnum, Enums: []string{"a", "b"}}, // add enum.
{Name: "enums2", Type: field.TypeEnum, Enums: []string{"a"}}, // remove enum.
{Name: "enums3", Type: field.TypeEnum, Enums: []string{"a", "b c"}}, // no changes.
},
PrimaryKey: []*Column{
{Name: "id", Type: field.TypeInt, Increment: true},
Expand All @@ -324,7 +325,8 @@ func TestMySQL_Create(t *testing.T) {
AddRow("id", "bigint(20)", "NO", "PRI", "NULL", "auto_increment", "", "", nil, nil).
AddRow("name", "varchar(255)", "YES", "YES", "NULL", "", "", "", nil, nil).
AddRow("enums1", "enum('a')", "YES", "NO", "NULL", "", "", "", nil, nil).
AddRow("enums2", "enum('b', 'a')", "NO", "YES", "NULL", "", "", "", nil, nil))
AddRow("enums2", "enum('b', 'a')", "NO", "YES", "NULL", "", "", "", nil, nil).
AddRow("enums3", "enum('a', 'b c')", "NO", "YES", "NULL", "", "", "", nil, nil))
mock.ExpectQuery(escape("SELECT `index_name`, `column_name`, `sub_part`, `non_unique`, `seq_in_index` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = (SELECT DATABASE()) AND `TABLE_NAME` = ? ORDER BY `index_name`, `seq_in_index`")).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "sub_part", "non_unique", "seq_in_index"}).
Expand Down
2 changes: 1 addition & 1 deletion entc/gen/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func plural(name string) string {
}

func isSeparator(r rune) bool {
return r == '_' || r == '-'
return r == '_' || r == '-' || unicode.IsSpace(r)
}

func pascalWords(words []string) string {
Expand Down
8 changes: 4 additions & 4 deletions entc/gen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -1371,16 +1371,16 @@ func (f Field) enums(lf *load.Field) ([]Enum, error) {
enums := make([]Enum, 0, len(lf.Enums))
values := make(map[string]bool, len(lf.Enums))
for i := range lf.Enums {
switch name, value := lf.Enums[i].N, lf.Enums[i].V; {
switch name, value := f.EnumName(lf.Enums[i].N), lf.Enums[i].V; {
case value == "":
return nil, fmt.Errorf("%q field value cannot be empty", f.Name)
case values[value]:
return nil, fmt.Errorf("duplicate values %q for enum field %q", value, f.Name)
case strings.IndexFunc(value, unicode.IsSpace) != -1:
return nil, fmt.Errorf("enum value %q cannot contain spaces", value)
case !token.IsIdentifier(name):
return nil, fmt.Errorf("enum %q does not have a valid Go indetifier (%q)", value, name)
default:
values[value] = true
enums = append(enums, Enum{Name: f.EnumName(name), Value: value})
enums = append(enums, Enum{Name: name, Value: value})
}
}
if value := lf.DefaultValue; value != nil {
Expand Down
1 change: 1 addition & 0 deletions entc/gen/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func TestField_EnumName(t *testing.T) {
{"MP4", "TypeMP4"},
{"unknown", "TypeUnknown"},
{"user_data", "TypeUserData"},
{"test user", "TypeTestUser"},
}
for _, tt := range tests {
require.Equal(t, tt.enum, Field{Name: "Type"}.EnumName(tt.name))
Expand Down
2 changes: 1 addition & 1 deletion entc/integration/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ services:
gremlin:
platform: linux/amd64
image: entgo/gremlin-server
build: gremlin-server
build: compose/gremlin-server
restart: on-failure
ports:
- 8182:8182
2 changes: 1 addition & 1 deletion entc/integration/ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion entc/integration/ent/schema/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (User) Fields() []ent.Field {
Optional().
Sensitive(),
field.Enum("role").
Values("user", "admin", "free-user").
Values("user", "admin", "free-user", "test user").
Default("user"),
field.String("SSOCert").
Optional(),
Expand Down
3 changes: 2 additions & 1 deletion entc/integration/ent/user/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion entc/integration/gremlin/ent/user/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0