Make initialisms configurable

This commit is contained in:
Peter Smit 2015-01-20 17:11:10 +02:00
parent c6b0cd81d3
commit 8ee59e351b
2 changed files with 27 additions and 31 deletions

View File

@ -154,7 +154,7 @@ func (mapper SnakeMapper) TableName(t string) string {
// GonicMapper implements IMapper. It will consider initialisms when mapping names.
// E.g. id -> ID, user -> User and to table names: UserID -> user_id, MyUID -> my_uid
type GonicMapper struct{}
type GonicMapper map[string]bool
func isASCIIUpper(r rune) bool {
return 'A' <= r && r <= 'Z'
@ -193,8 +193,31 @@ func (mapper GonicMapper) Obj2Table(name string) string {
return gonicCasedName(name)
}
// A list of common initialisms taken from golang/lint
var commonInitialisms = map[string]bool{
func (mapper GonicMapper) Table2Obj(name string) string {
newstr := make([]rune, 0)
name = strings.ToLower(name)
parts := strings.Split(name, "_")
for _, p := range parts {
_, isInitialism := mapper[strings.ToUpper(p)]
for i, r := range p {
if i == 0 || isInitialism {
r = toASCIIUpper(r)
}
newstr = append(newstr, r)
}
}
return string(newstr)
}
func (mapper GonicMapper) TableName(t string) string {
return t
}
// A GonicMapper that contains a list of common initialisms taken from golang/lint
var LintGonicMapper = GonicMapper{
"API": true,
"ASCII": true,
"CPU": true,
@ -230,33 +253,6 @@ var commonInitialisms = map[string]bool{
"XSS": true,
}
func gonicTitleCasedName(name string) string {
newstr := make([]rune, 0)
name = strings.ToLower(name)
parts := strings.Split(name, "_")
for _, p := range parts {
_, isInitialism := commonInitialisms[strings.ToUpper(p)]
for i, r := range p {
if i == 0 || isInitialism {
r = toASCIIUpper(r)
}
newstr = append(newstr, r)
}
}
return string(newstr)
}
func (mapper GonicMapper) Table2Obj(name string) string {
return gonicTitleCasedName(name)
}
func (mapper GonicMapper) TableName(t string) string {
return t
}
// provide prefix table name support
type PrefixMapper struct {
Mapper IMapper

View File

@ -37,7 +37,7 @@ func TestGonicMapperToObj(t *testing.T) {
}
for in, expected := range testCases {
out := gonicTitleCasedName(in)
out := LintGonicMapper.Table2Obj(in)
if out != expected {
t.Errorf("Given %s, expected %s but got %s", in, expected, out)
}