2020-01-31 09:17:02 +03:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2020-09-24 06:32:38 +03:00
|
|
|
"strings"
|
2020-01-31 09:17:02 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestToDBName(t *testing.T) {
|
|
|
|
var maps = map[string]string{
|
|
|
|
"": "",
|
|
|
|
"x": "x",
|
|
|
|
"X": "x",
|
|
|
|
"userRestrictions": "user_restrictions",
|
|
|
|
"ThisIsATest": "this_is_a_test",
|
|
|
|
"PFAndESI": "pf_and_esi",
|
|
|
|
"AbcAndJkl": "abc_and_jkl",
|
|
|
|
"EmployeeID": "employee_id",
|
|
|
|
"SKU_ID": "sku_id",
|
|
|
|
"FieldX": "field_x",
|
|
|
|
"HTTPAndSMTP": "http_and_smtp",
|
|
|
|
"HTTPServerHandlerForURLID": "http_server_handler_for_url_id",
|
|
|
|
"UUID": "uuid",
|
|
|
|
"HTTPURL": "http_url",
|
|
|
|
"HTTP_URL": "http_url",
|
|
|
|
"SHA256Hash": "sha256_hash",
|
|
|
|
"SHA256HASH": "sha256_hash",
|
|
|
|
"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",
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range maps {
|
|
|
|
if toDBName(key) != value {
|
|
|
|
t.Errorf("%v toName should equal %v, but got %v", key, value, toDBName(key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 06:32:38 +03:00
|
|
|
|
|
|
|
type NewNamingStrategy struct {
|
|
|
|
NamingStrategy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns NewNamingStrategy) ColumnName(table, column string) string {
|
|
|
|
baseColumnName := ns.NamingStrategy.ColumnName(table, column)
|
|
|
|
|
|
|
|
if table == "" {
|
|
|
|
return baseColumnName
|
|
|
|
}
|
|
|
|
|
|
|
|
s := strings.Split(table, "_")
|
|
|
|
|
|
|
|
var prefix string
|
|
|
|
switch len(s) {
|
|
|
|
case 1:
|
|
|
|
prefix = s[0][:3]
|
|
|
|
case 2:
|
|
|
|
prefix = s[0][:1] + s[1][:2]
|
|
|
|
default:
|
|
|
|
prefix = s[0][:1] + s[1][:1] + s[2][:1]
|
|
|
|
}
|
|
|
|
return prefix + "_" + baseColumnName
|
|
|
|
}
|