From 077670451a0e892ebe9d4ec14f072da54deecabf Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Sun, 17 Jul 2022 17:14:43 +0300 Subject: [PATCH] entc/gen: skip enum identifier checks in case it has custom Go type (#2766) Fixes https://github.com/ent/ent/issues/2756 --- entc/gen/type.go | 2 +- entc/integration/ent/fieldtype/fieldtype.go | 2 +- entc/integration/ent/migrate/schema.go | 2 +- entc/integration/ent/role/role.go | 13 +++++++------ entc/integration/gremlin/ent/fieldtype/fieldtype.go | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/entc/gen/type.go b/entc/gen/type.go index fc1d2832d..12f6206c9 100644 --- a/entc/gen/type.go +++ b/entc/gen/type.go @@ -1503,7 +1503,7 @@ func (f Field) enums(lf *load.Field) ([]Enum, error) { 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 !token.IsIdentifier(name): + case !token.IsIdentifier(name) && !f.HasGoType(): return nil, fmt.Errorf("enum %q does not have a valid Go identifier (%q)", value, name) default: values[value] = true diff --git a/entc/integration/ent/fieldtype/fieldtype.go b/entc/integration/ent/fieldtype/fieldtype.go index b7a4d5e38..1a0a2e73a 100644 --- a/entc/integration/ent/fieldtype/fieldtype.go +++ b/entc/integration/ent/fieldtype/fieldtype.go @@ -316,7 +316,7 @@ const DefaultRole role.Role = "READ" // RoleValidator is a validator for the "role" field enum values. It is called by the builders before save. func RoleValidator(r role.Role) error { switch r { - case "ADMIN", "OWNER", "USER", "READ", "WRITE": + case "ADMIN", "OWNER", "USER", "READ", "WRITE", "READ+WRITE": return nil default: return fmt.Errorf("fieldtype: invalid enum value for role field: %q", r) diff --git a/entc/integration/ent/migrate/schema.go b/entc/integration/ent/migrate/schema.go index be81a68d0..f7982499b 100644 --- a/entc/integration/ent/migrate/schema.go +++ b/entc/integration/ent/migrate/schema.go @@ -126,7 +126,7 @@ var ( {Name: "schema_float", Type: field.TypeFloat64, Nullable: true}, {Name: "schema_float32", Type: field.TypeFloat32, Nullable: true}, {Name: "null_float", Type: field.TypeFloat64, Nullable: true}, - {Name: "role", Type: field.TypeEnum, Enums: []string{"ADMIN", "OWNER", "USER", "READ", "WRITE"}, Default: "READ"}, + {Name: "role", Type: field.TypeEnum, Enums: []string{"ADMIN", "OWNER", "USER", "READ", "WRITE", "READ+WRITE"}, Default: "READ"}, {Name: "priority", Type: field.TypeEnum, Nullable: true, Enums: []string{"UNKNOWN", "LOW", "HIGH"}}, {Name: "optional_uuid", Type: field.TypeUUID, Nullable: true}, {Name: "nillable_uuid", Type: field.TypeUUID, Nullable: true}, diff --git a/entc/integration/ent/role/role.go b/entc/integration/ent/role/role.go index 0da3254c5..ae3228244 100644 --- a/entc/integration/ent/role/role.go +++ b/entc/integration/ent/role/role.go @@ -11,15 +11,16 @@ import ( type Role string const ( - Admin Role = "ADMIN" - Owner Role = "OWNER" - User Role = "USER" - Read Role = "READ" - Write Role = "WRITE" + Admin Role = "ADMIN" + Owner Role = "OWNER" + User Role = "USER" + Read Role = "READ" + Write Role = "WRITE" + ReadWrite Role = "READ+WRITE" ) func (Role) Values() (roles []string) { - for _, r := range []Role{Admin, Owner, User, Read, Write} { + for _, r := range []Role{Admin, Owner, User, Read, Write, ReadWrite} { roles = append(roles, string(r)) } return diff --git a/entc/integration/gremlin/ent/fieldtype/fieldtype.go b/entc/integration/gremlin/ent/fieldtype/fieldtype.go index 7fc239a56..dc5afbee9 100644 --- a/entc/integration/gremlin/ent/fieldtype/fieldtype.go +++ b/entc/integration/gremlin/ent/fieldtype/fieldtype.go @@ -223,7 +223,7 @@ const DefaultRole role.Role = "READ" // RoleValidator is a validator for the "role" field enum values. It is called by the builders before save. func RoleValidator(r role.Role) error { switch r { - case "ADMIN", "OWNER", "USER", "READ", "WRITE": + case "ADMIN", "OWNER", "USER", "READ", "WRITE", "READ+WRITE": return nil default: return fmt.Errorf("fieldtype: invalid enum value for role field: %q", r)