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

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

View File

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