Merge branch 'master' into master

This commit is contained in:
Jinzhu 2018-07-27 07:21:04 +08:00 committed by GitHub
commit 53d5c9df19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 7 deletions

View File

@ -4,11 +4,11 @@ import (
"database/sql"
"database/sql/driver"
_ "github.com/lib/pq"
"github.com/lib/pq/hstore"
"encoding/json"
"errors"
"fmt"
_ "github.com/lib/pq"
"github.com/lib/pq/hstore"
)
type Hstore map[string]*string

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",