Support NameReplace for NamingStrategy, close #3779

This commit is contained in:
Jinzhu 2020-11-23 11:24:07 +08:00
parent 6186a4daa7
commit 66e8a72bf1
2 changed files with 23 additions and 10 deletions

View File

@ -24,19 +24,20 @@ type Namer interface {
type NamingStrategy struct { type NamingStrategy struct {
TablePrefix string TablePrefix string
SingularTable bool SingularTable bool
NameReplacer *strings.Replacer
} }
// TableName convert string to table name // TableName convert string to table name
func (ns NamingStrategy) TableName(str string) string { func (ns NamingStrategy) TableName(str string) string {
if ns.SingularTable { if ns.SingularTable {
return ns.TablePrefix + toDBName(str) return ns.TablePrefix + ns.toDBName(str)
} }
return ns.TablePrefix + inflection.Plural(toDBName(str)) return ns.TablePrefix + inflection.Plural(ns.toDBName(str))
} }
// ColumnName convert string to column name // ColumnName convert string to column name
func (ns NamingStrategy) ColumnName(table, column string) string { func (ns NamingStrategy) ColumnName(table, column string) string {
return toDBName(column) return ns.toDBName(column)
} }
// JoinTableName convert string to join table name // JoinTableName convert string to join table name
@ -46,14 +47,14 @@ func (ns NamingStrategy) JoinTableName(str string) string {
} }
if ns.SingularTable { if ns.SingularTable {
return ns.TablePrefix + toDBName(str) return ns.TablePrefix + ns.toDBName(str)
} }
return ns.TablePrefix + inflection.Plural(toDBName(str)) return ns.TablePrefix + inflection.Plural(ns.toDBName(str))
} }
// RelationshipFKName generate fk name for relation // RelationshipFKName generate fk name for relation
func (ns NamingStrategy) RelationshipFKName(rel Relationship) string { func (ns NamingStrategy) RelationshipFKName(rel Relationship) string {
return strings.Replace(fmt.Sprintf("fk_%s_%s", rel.Schema.Table, toDBName(rel.Name)), ".", "_", -1) return strings.Replace(fmt.Sprintf("fk_%s_%s", rel.Schema.Table, ns.toDBName(rel.Name)), ".", "_", -1)
} }
// CheckerName generate checker name // CheckerName generate checker name
@ -63,7 +64,7 @@ func (ns NamingStrategy) CheckerName(table, column string) string {
// IndexName generate index name // IndexName generate index name
func (ns NamingStrategy) IndexName(table, column string) string { func (ns NamingStrategy) IndexName(table, column string) string {
idxName := fmt.Sprintf("idx_%v_%v", table, toDBName(column)) idxName := fmt.Sprintf("idx_%v_%v", table, ns.toDBName(column))
idxName = strings.Replace(idxName, ".", "_", -1) idxName = strings.Replace(idxName, ".", "_", -1)
if utf8.RuneCountInString(idxName) > 64 { if utf8.RuneCountInString(idxName) > 64 {
@ -91,13 +92,17 @@ func init() {
commonInitialismsReplacer = strings.NewReplacer(commonInitialismsForReplacer...) commonInitialismsReplacer = strings.NewReplacer(commonInitialismsForReplacer...)
} }
func toDBName(name string) string { func (ns NamingStrategy) toDBName(name string) string {
if name == "" { if name == "" {
return "" return ""
} else if v, ok := smap.Load(name); ok { } else if v, ok := smap.Load(name); ok {
return v.(string) return v.(string)
} }
if ns.NameReplacer != nil {
name = ns.NameReplacer.Replace(name)
}
var ( var (
value = commonInitialismsReplacer.Replace(name) value = commonInitialismsReplacer.Replace(name)
buf strings.Builder buf strings.Builder

View File

@ -1,6 +1,7 @@
package schema package schema
import ( import (
"strings"
"testing" "testing"
) )
@ -26,9 +27,10 @@ func TestToDBName(t *testing.T) {
"ThisIsActuallyATestSoWeMayBeAbleToUseThisCodeInGormPackageAlsoIdCanBeUsedAtTheEndAsID": "this_is_actually_a_test_so_we_may_be_able_to_use_this_code_in_gorm_package_also_id_can_be_used_at_the_end_as_id", "ThisIsActuallyATestSoWeMayBeAbleToUseThisCodeInGormPackageAlsoIdCanBeUsedAtTheEndAsID": "this_is_actually_a_test_so_we_may_be_able_to_use_this_code_in_gorm_package_also_id_can_be_used_at_the_end_as_id",
} }
ns := NamingStrategy{}
for key, value := range maps { for key, value := range maps {
if toDBName(key) != value { if ns.toDBName(key) != value {
t.Errorf("%v toName should equal %v, but got %v", key, value, toDBName(key)) t.Errorf("%v toName should equal %v, but got %v", key, value, ns.toDBName(key))
} }
} }
} }
@ -37,6 +39,7 @@ func TestNamingStrategy(t *testing.T) {
var ns = NamingStrategy{ var ns = NamingStrategy{
TablePrefix: "public.", TablePrefix: "public.",
SingularTable: true, SingularTable: true,
NameReplacer: strings.NewReplacer("CID", "Cid"),
} }
idxName := ns.IndexName("public.table", "name") idxName := ns.IndexName("public.table", "name")
@ -63,4 +66,9 @@ func TestNamingStrategy(t *testing.T) {
if tableName != "public.company" { if tableName != "public.company" {
t.Errorf("invalid table name generated, got %v", tableName) t.Errorf("invalid table name generated, got %v", tableName)
} }
columdName := ns.ColumnName("", "NameCID")
if columdName != "name_cid" {
t.Errorf("invalid column name generated, got %v", columdName)
}
} }