Add NoLowerCase option to skip the snake_casing of names.
This commit is contained in:
parent
2abab3cf60
commit
96fe56218b
@ -30,6 +30,7 @@ type NamingStrategy struct {
|
||||
TablePrefix string
|
||||
SingularTable bool
|
||||
NameReplacer Replacer
|
||||
NoLowerCase bool
|
||||
}
|
||||
|
||||
// TableName convert string to table name
|
||||
@ -47,7 +48,7 @@ func (ns NamingStrategy) ColumnName(table, column string) string {
|
||||
|
||||
// JoinTableName convert string to join table name
|
||||
func (ns NamingStrategy) JoinTableName(str string) string {
|
||||
if strings.ToLower(str) == str {
|
||||
if !ns.NoLowerCase && strings.ToLower(str) == str {
|
||||
return ns.TablePrefix + str
|
||||
}
|
||||
|
||||
@ -116,6 +117,11 @@ func (ns NamingStrategy) toDBName(name string) string {
|
||||
name = ns.NameReplacer.Replace(name)
|
||||
}
|
||||
|
||||
if ns.NoLowerCase {
|
||||
smap.Store(name, name) // TODO: should store with original name, not replaced name
|
||||
return name
|
||||
}
|
||||
|
||||
var (
|
||||
value = commonInitialismsReplacer.Replace(name)
|
||||
buf strings.Builder
|
||||
@ -153,6 +159,6 @@ func (ns NamingStrategy) toDBName(name string) string {
|
||||
buf.WriteByte(value[len(value)-1])
|
||||
}
|
||||
ret := buf.String()
|
||||
smap.Store(name, ret)
|
||||
smap.Store(name, ret) // TODO: should store with original name, not replaced name
|
||||
return ret
|
||||
}
|
||||
|
@ -85,18 +85,21 @@ func (r CustomReplacer) Replace(name string) string {
|
||||
return r.f(name)
|
||||
}
|
||||
|
||||
var testReplacer = CustomReplacer{
|
||||
func(name string) string {
|
||||
replaced := "REPLACED_" + strings.ToUpper(name)
|
||||
return strings.NewReplacer("CID", "_Cid").Replace(replaced)
|
||||
},
|
||||
}
|
||||
|
||||
func TestCustomReplacer(t *testing.T) {
|
||||
reset()
|
||||
|
||||
var ns = NamingStrategy{
|
||||
TablePrefix: "public.",
|
||||
SingularTable: true,
|
||||
NameReplacer: CustomReplacer{
|
||||
func(name string) string {
|
||||
replaced := "REPLACED_" + strings.ToUpper(name)
|
||||
return strings.NewReplacer("CID", "_Cid").Replace(replaced)
|
||||
},
|
||||
},
|
||||
NameReplacer: testReplacer,
|
||||
NoLowerCase: false,
|
||||
}
|
||||
|
||||
idxName := ns.IndexName("public.table", "name")
|
||||
@ -129,3 +132,44 @@ func TestCustomReplacer(t *testing.T) {
|
||||
t.Errorf("invalid column name generated, got %v", columdName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomReplacerWithNoLowerCase(t *testing.T) {
|
||||
reset()
|
||||
|
||||
var ns = NamingStrategy{
|
||||
TablePrefix: "public.",
|
||||
SingularTable: true,
|
||||
NameReplacer: testReplacer,
|
||||
NoLowerCase: true,
|
||||
}
|
||||
|
||||
idxName := ns.IndexName("public.table", "name")
|
||||
if idxName != "idx_public_table_REPLACED_NAME" {
|
||||
t.Errorf("invalid index name generated, got %v", idxName)
|
||||
}
|
||||
|
||||
chkName := ns.CheckerName("public.table", "name")
|
||||
if chkName != "chk_public_table_name" {
|
||||
t.Errorf("invalid checker name generated, got %v", chkName)
|
||||
}
|
||||
|
||||
joinTable := ns.JoinTableName("user_languages")
|
||||
if joinTable != "public.REPLACED_USER_LANGUAGES" {
|
||||
t.Errorf("invalid join table generated, got %v", joinTable)
|
||||
}
|
||||
|
||||
joinTable2 := ns.JoinTableName("UserLanguage")
|
||||
if joinTable2 != "public.REPLACED_USERLANGUAGE" {
|
||||
t.Errorf("invalid join table generated, got %v", joinTable2)
|
||||
}
|
||||
|
||||
tableName := ns.TableName("Company")
|
||||
if tableName != "public.REPLACED_COMPANY" {
|
||||
t.Errorf("invalid table name generated, got %v", tableName)
|
||||
}
|
||||
|
||||
columdName := ns.ColumnName("", "NameCID")
|
||||
if columdName != "REPLACED_NAME_Cid" {
|
||||
t.Errorf("invalid column name generated, got %v", columdName)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user