Fix ToDBName (#1941)

Don't place '_' before number.

- NG: SHA256Hash -> sha_256_hash
- OK: SHA256Hash -> sha256_hash
This commit is contained in:
Masaki Yoshida 2018-06-25 14:07:53 +09:00 committed by Jinzhu
parent 1907bff373
commit 0fd395ab37
2 changed files with 10 additions and 5 deletions

View File

@ -78,16 +78,18 @@ func ToDBName(name string) string {
}
var (
value = commonInitialismsReplacer.Replace(name)
buf = bytes.NewBufferString("")
lastCase, currCase, nextCase strCase
value = commonInitialismsReplacer.Replace(name)
buf = bytes.NewBufferString("")
lastCase, currCase, nextCase, nextNumber strCase
)
for i, v := range value[:len(value)-1] {
nextCase = strCase(value[i+1] >= 'A' && value[i+1] <= 'Z')
nextNumber = strCase(value[i+1] >= '0' && value[i+1] <= '9')
if i > 0 {
if currCase == upper {
if lastCase == upper && nextCase == upper {
if lastCase == upper && (nextCase == upper || nextNumber == upper) {
buf.WriteRune(v)
} else {
if value[i-1] != '_' && value[i+1] != '_' {
@ -97,7 +99,7 @@ func ToDBName(name string) string {
}
} else {
buf.WriteRune(v)
if i == len(value)-2 && nextCase == upper {
if i == len(value)-2 && (nextCase == upper && nextNumber == lower) {
buf.WriteRune('_')
}
}

View File

@ -15,6 +15,9 @@ func TestToDBNameGenerateFriendlyName(t *testing.T) {
"AbcAndJkl": "abc_and_jkl",
"EmployeeID": "employee_id",
"SKU_ID": "sku_id",
"UTF8": "utf8",
"Level1": "level1",
"SHA256Hash": "sha256_hash",
"FieldX": "field_x",
"HTTPAndSMTP": "http_and_smtp",
"HTTPServerHandlerForURLID": "http_server_handler_for_url_id",