2019-06-23 09:22:43 -06:00
|
|
|
// Copyright 2019 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-03-22 09:12:55 -06:00
|
|
|
package names
|
2016-11-03 16:16:01 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-06-15 14:46:01 -06:00
|
|
|
"unsafe"
|
2016-11-03 16:16:01 -06:00
|
|
|
)
|
|
|
|
|
2020-03-22 09:12:55 -06:00
|
|
|
// Mapper represents a name convertation between struct's fields name and table's column name
|
|
|
|
type Mapper interface {
|
2016-11-03 16:16:01 -06:00
|
|
|
Obj2Table(string) string
|
|
|
|
Table2Obj(string) string
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// CacheMapper represents a cache mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
type CacheMapper struct {
|
2020-03-22 09:12:55 -06:00
|
|
|
oriMapper Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
obj2tableCache map[string]string
|
|
|
|
obj2tableMutex sync.RWMutex
|
|
|
|
table2objCache map[string]string
|
|
|
|
table2objMutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// NewCacheMapper creates a cache mapper
|
2020-03-22 09:12:55 -06:00
|
|
|
func NewCacheMapper(mapper Mapper) *CacheMapper {
|
2016-11-03 16:16:01 -06:00
|
|
|
return &CacheMapper{oriMapper: mapper, obj2tableCache: make(map[string]string),
|
|
|
|
table2objCache: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// Obj2Table implements Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
func (m *CacheMapper) Obj2Table(o string) string {
|
|
|
|
m.obj2tableMutex.RLock()
|
|
|
|
t, ok := m.obj2tableCache[o]
|
|
|
|
m.obj2tableMutex.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
t = m.oriMapper.Obj2Table(o)
|
|
|
|
m.obj2tableMutex.Lock()
|
|
|
|
m.obj2tableCache[o] = t
|
|
|
|
m.obj2tableMutex.Unlock()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// Table2Obj implements Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
func (m *CacheMapper) Table2Obj(t string) string {
|
|
|
|
m.table2objMutex.RLock()
|
|
|
|
o, ok := m.table2objCache[t]
|
|
|
|
m.table2objMutex.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
o = m.oriMapper.Table2Obj(t)
|
|
|
|
m.table2objMutex.Lock()
|
|
|
|
m.table2objCache[t] = o
|
|
|
|
m.table2objMutex.Unlock()
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// SameMapper implements Mapper and provides same name between struct and
|
2016-11-03 16:16:01 -06:00
|
|
|
// database table
|
|
|
|
type SameMapper struct {
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// Obj2Table implements Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
func (m SameMapper) Obj2Table(o string) string {
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// Table2Obj implements Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
func (m SameMapper) Table2Obj(t string) string {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:11:42 -06:00
|
|
|
// SnakeMapper implements IMapper and provides name translation between
|
2016-11-03 16:16:01 -06:00
|
|
|
// struct and database table
|
|
|
|
type SnakeMapper struct {
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:46:01 -06:00
|
|
|
func b2s(b []byte) string {
|
|
|
|
return *(*string)(unsafe.Pointer(&b))
|
|
|
|
}
|
|
|
|
|
2016-11-03 16:16:01 -06:00
|
|
|
func snakeCasedName(name string) string {
|
2020-06-15 14:46:01 -06:00
|
|
|
newstr := make([]byte, 0, len(name)+1)
|
|
|
|
for i := 0; i < len(name); i++ {
|
|
|
|
c := name[i]
|
|
|
|
if isUpper := 'A' <= c && c <= 'Z'; isUpper {
|
|
|
|
if i > 0 {
|
2016-11-03 16:16:01 -06:00
|
|
|
newstr = append(newstr, '_')
|
|
|
|
}
|
2020-06-15 14:46:01 -06:00
|
|
|
c += 'a' - 'A'
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
2020-06-15 14:46:01 -06:00
|
|
|
newstr = append(newstr, c)
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:46:01 -06:00
|
|
|
return b2s(newstr)
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// Obj2Table implements Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
func (mapper SnakeMapper) Obj2Table(name string) string {
|
|
|
|
return snakeCasedName(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func titleCasedName(name string) string {
|
2020-06-15 14:46:01 -06:00
|
|
|
newstr := make([]byte, 0, len(name))
|
2016-11-03 16:16:01 -06:00
|
|
|
upNextChar := true
|
|
|
|
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
|
2020-06-15 14:46:01 -06:00
|
|
|
for i := 0; i < len(name); i++ {
|
|
|
|
c := name[i]
|
2016-11-03 16:16:01 -06:00
|
|
|
switch {
|
|
|
|
case upNextChar:
|
|
|
|
upNextChar = false
|
2020-06-15 14:46:01 -06:00
|
|
|
if 'a' <= c && c <= 'z' {
|
|
|
|
c -= 'a' - 'A'
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
2020-06-15 14:46:01 -06:00
|
|
|
case c == '_':
|
2016-11-03 16:16:01 -06:00
|
|
|
upNextChar = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:46:01 -06:00
|
|
|
newstr = append(newstr, c)
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:46:01 -06:00
|
|
|
return b2s(newstr)
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// Table2Obj implements Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
func (mapper SnakeMapper) Table2Obj(name string) string {
|
|
|
|
return titleCasedName(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 map[string]bool
|
|
|
|
|
|
|
|
func isASCIIUpper(r rune) bool {
|
|
|
|
return 'A' <= r && r <= 'Z'
|
|
|
|
}
|
|
|
|
|
|
|
|
func toASCIIUpper(r rune) rune {
|
|
|
|
if 'a' <= r && r <= 'z' {
|
|
|
|
r -= ('a' - 'A')
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func gonicCasedName(name string) string {
|
|
|
|
newstr := make([]rune, 0, len(name)+3)
|
|
|
|
for idx, chr := range name {
|
|
|
|
if isASCIIUpper(chr) && idx > 0 {
|
|
|
|
if !isASCIIUpper(newstr[len(newstr)-1]) {
|
|
|
|
newstr = append(newstr, '_')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isASCIIUpper(chr) && idx > 1 {
|
|
|
|
l := len(newstr)
|
|
|
|
if isASCIIUpper(newstr[l-1]) && isASCIIUpper(newstr[l-2]) {
|
|
|
|
newstr = append(newstr, newstr[l-1])
|
|
|
|
newstr[l-1] = '_'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newstr = append(newstr, chr)
|
|
|
|
}
|
|
|
|
return strings.ToLower(string(newstr))
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// Obj2Table implements Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
func (mapper GonicMapper) Obj2Table(name string) string {
|
|
|
|
return gonicCasedName(name)
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// Table2Obj implements Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-08-26 20:17:23 -06:00
|
|
|
// LintGonicMapper is A GonicMapper that contains a list of common initialisms taken from golang/lint
|
2016-11-03 16:16:01 -06:00
|
|
|
var LintGonicMapper = GonicMapper{
|
|
|
|
"API": true,
|
|
|
|
"ASCII": true,
|
|
|
|
"CPU": true,
|
|
|
|
"CSS": true,
|
|
|
|
"DNS": true,
|
|
|
|
"EOF": true,
|
|
|
|
"GUID": true,
|
|
|
|
"HTML": true,
|
|
|
|
"HTTP": true,
|
|
|
|
"HTTPS": true,
|
|
|
|
"ID": true,
|
|
|
|
"IP": true,
|
|
|
|
"JSON": true,
|
|
|
|
"LHS": true,
|
|
|
|
"QPS": true,
|
|
|
|
"RAM": true,
|
|
|
|
"RHS": true,
|
|
|
|
"RPC": true,
|
|
|
|
"SLA": true,
|
|
|
|
"SMTP": true,
|
|
|
|
"SSH": true,
|
|
|
|
"TLS": true,
|
|
|
|
"TTL": true,
|
|
|
|
"UI": true,
|
|
|
|
"UID": true,
|
|
|
|
"UUID": true,
|
|
|
|
"URI": true,
|
|
|
|
"URL": true,
|
|
|
|
"UTF8": true,
|
|
|
|
"VM": true,
|
|
|
|
"XML": true,
|
|
|
|
"XSRF": true,
|
|
|
|
"XSS": true,
|
|
|
|
}
|
|
|
|
|
2019-08-26 20:17:23 -06:00
|
|
|
// PrefixMapper provides prefix table name support
|
2016-11-03 16:16:01 -06:00
|
|
|
type PrefixMapper struct {
|
2020-03-22 09:12:55 -06:00
|
|
|
Mapper Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
Prefix string
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// Obj2Table implements Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
func (mapper PrefixMapper) Obj2Table(name string) string {
|
|
|
|
return mapper.Prefix + mapper.Mapper.Obj2Table(name)
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// Table2Obj implements Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
func (mapper PrefixMapper) Table2Obj(name string) string {
|
|
|
|
return mapper.Mapper.Table2Obj(name[len(mapper.Prefix):])
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// NewPrefixMapper creates a prefix mapper
|
2020-03-22 09:12:55 -06:00
|
|
|
func NewPrefixMapper(mapper Mapper, prefix string) PrefixMapper {
|
2016-11-03 16:16:01 -06:00
|
|
|
return PrefixMapper{mapper, prefix}
|
|
|
|
}
|
|
|
|
|
2019-08-26 20:17:23 -06:00
|
|
|
// SuffixMapper provides suffix table name support
|
2016-11-03 16:16:01 -06:00
|
|
|
type SuffixMapper struct {
|
2020-03-22 09:12:55 -06:00
|
|
|
Mapper Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
Suffix string
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// Obj2Table implements Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
func (mapper SuffixMapper) Obj2Table(name string) string {
|
|
|
|
return mapper.Mapper.Obj2Table(name) + mapper.Suffix
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// Table2Obj implements Mapper
|
2016-11-03 16:16:01 -06:00
|
|
|
func (mapper SuffixMapper) Table2Obj(name string) string {
|
|
|
|
return mapper.Mapper.Table2Obj(name[:len(name)-len(mapper.Suffix)])
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:17:06 -06:00
|
|
|
// NewSuffixMapper creates a suffix mapper
|
2020-03-22 09:12:55 -06:00
|
|
|
func NewSuffixMapper(mapper Mapper, suffix string) SuffixMapper {
|
2016-11-03 16:16:01 -06:00
|
|
|
return SuffixMapper{mapper, suffix}
|
|
|
|
}
|