mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: Split strings in pascal/camcelCase on - in addition to _ (#631)
This commit is contained in:
@@ -108,14 +108,11 @@ func plural(name string) string {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
// pascal converts the given column name into a PascalCase.
|
func isSeparator(r rune) bool {
|
||||||
//
|
return r == '_' || r == '-'
|
||||||
// user_info => UserInfo
|
}
|
||||||
// full_name => FullName
|
|
||||||
// user_id => UserID
|
func pascalWords(words []string) string {
|
||||||
//
|
|
||||||
func pascal(s string) string {
|
|
||||||
words := strings.Split(s, "_")
|
|
||||||
for i, w := range words {
|
for i, w := range words {
|
||||||
upper := strings.ToUpper(w)
|
upper := strings.ToUpper(w)
|
||||||
if _, ok := acronyms[upper]; ok {
|
if _, ok := acronyms[upper]; ok {
|
||||||
@@ -127,18 +124,31 @@ func pascal(s string) string {
|
|||||||
return strings.Join(words, "")
|
return strings.Join(words, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// camel converts the given column name into a camelCase.
|
// pascal converts the given name into a PascalCase.
|
||||||
//
|
//
|
||||||
// user_info => userInfo
|
// user_info => UserInfo
|
||||||
// full_name => fullName
|
// full_name => FullName
|
||||||
// user_id => userID
|
// user_id => UserID
|
||||||
|
// full-admin => FullAdmin
|
||||||
|
//
|
||||||
|
func pascal(s string) string {
|
||||||
|
words := strings.FieldsFunc(s, isSeparator)
|
||||||
|
return pascalWords(words)
|
||||||
|
}
|
||||||
|
|
||||||
|
// camel converts the given name into a camelCase.
|
||||||
|
//
|
||||||
|
// user_info => userInfo
|
||||||
|
// full_name => fullName
|
||||||
|
// user_id => userID
|
||||||
|
// full-admin => fullAdmin
|
||||||
//
|
//
|
||||||
func camel(s string) string {
|
func camel(s string) string {
|
||||||
words := strings.SplitN(s, "_", 2)
|
words := strings.FieldsFunc(s, isSeparator)
|
||||||
if len(words) == 1 {
|
if len(words) == 1 {
|
||||||
return strings.ToLower(words[0])
|
return strings.ToLower(words[0])
|
||||||
}
|
}
|
||||||
return strings.ToLower(words[0]) + pascal(words[1])
|
return strings.ToLower(words[0]) + pascalWords(words[1:])
|
||||||
}
|
}
|
||||||
|
|
||||||
// snake converts the given struct or field name into a snake_case.
|
// snake converts the given struct or field name into a snake_case.
|
||||||
|
|||||||
@@ -323,7 +323,7 @@ var (
|
|||||||
{Name: "nickname", Type: field.TypeString, Unique: true, Nullable: true},
|
{Name: "nickname", Type: field.TypeString, Unique: true, Nullable: true},
|
||||||
{Name: "phone", Type: field.TypeString, Unique: true, Nullable: true},
|
{Name: "phone", Type: field.TypeString, Unique: true, Nullable: true},
|
||||||
{Name: "password", Type: field.TypeString, Nullable: true},
|
{Name: "password", Type: field.TypeString, Nullable: true},
|
||||||
{Name: "role", Type: field.TypeEnum, Enums: []string{"admin", "user"}, Default: "user"},
|
{Name: "role", Type: field.TypeEnum, Enums: []string{"admin", "free-user", "user"}, Default: "user"},
|
||||||
{Name: "sso_cert", Type: field.TypeString, Nullable: true},
|
{Name: "sso_cert", Type: field.TypeString, Nullable: true},
|
||||||
{Name: "group_blocked", Type: field.TypeInt, Nullable: true},
|
{Name: "group_blocked", Type: field.TypeInt, Nullable: true},
|
||||||
{Name: "user_spouse", Type: field.TypeInt, Unique: true, Nullable: true},
|
{Name: "user_spouse", Type: field.TypeInt, Unique: true, Nullable: true},
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ func (User) Fields() []ent.Field {
|
|||||||
Optional().
|
Optional().
|
||||||
Sensitive(),
|
Sensitive(),
|
||||||
field.Enum("role").
|
field.Enum("role").
|
||||||
Values("user", "admin").
|
Values("user", "admin", "free-user").
|
||||||
Default("user"),
|
Default("user"),
|
||||||
field.String("SSOCert").
|
field.String("SSOCert").
|
||||||
Optional(),
|
Optional(),
|
||||||
|
|||||||
@@ -163,8 +163,9 @@ const DefaultRole = RoleUser
|
|||||||
|
|
||||||
// Role values.
|
// Role values.
|
||||||
const (
|
const (
|
||||||
RoleAdmin Role = "admin"
|
RoleAdmin Role = "admin"
|
||||||
RoleUser Role = "user"
|
RoleFreeUser Role = "free-user"
|
||||||
|
RoleUser Role = "user"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r Role) String() string {
|
func (r Role) String() string {
|
||||||
@@ -174,7 +175,7 @@ func (r Role) String() string {
|
|||||||
// RoleValidator is a validator for the "role" field enum values. It is called by the builders before save.
|
// RoleValidator is a validator for the "role" field enum values. It is called by the builders before save.
|
||||||
func RoleValidator(r Role) error {
|
func RoleValidator(r Role) error {
|
||||||
switch r {
|
switch r {
|
||||||
case RoleAdmin, RoleUser:
|
case RoleAdmin, RoleFreeUser, RoleUser:
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("user: invalid enum value for role field: %q", r)
|
return fmt.Errorf("user: invalid enum value for role field: %q", r)
|
||||||
|
|||||||
@@ -96,8 +96,9 @@ const DefaultRole = RoleUser
|
|||||||
|
|
||||||
// Role values.
|
// Role values.
|
||||||
const (
|
const (
|
||||||
RoleAdmin Role = "admin"
|
RoleAdmin Role = "admin"
|
||||||
RoleUser Role = "user"
|
RoleFreeUser Role = "free-user"
|
||||||
|
RoleUser Role = "user"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r Role) String() string {
|
func (r Role) String() string {
|
||||||
@@ -107,7 +108,7 @@ func (r Role) String() string {
|
|||||||
// RoleValidator is a validator for the "role" field enum values. It is called by the builders before save.
|
// RoleValidator is a validator for the "role" field enum values. It is called by the builders before save.
|
||||||
func RoleValidator(r Role) error {
|
func RoleValidator(r Role) error {
|
||||||
switch r {
|
switch r {
|
||||||
case RoleAdmin, RoleUser:
|
case RoleAdmin, RoleFreeUser, RoleUser:
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("user: invalid enum value for role field: %q", r)
|
return fmt.Errorf("user: invalid enum value for role field: %q", r)
|
||||||
|
|||||||
Reference in New Issue
Block a user