forked from go-gorm/gorm
/
utils.go
55 lines (46 loc) · 1.31 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package gorm
import (
"bytes"
"strings"
"sync"
)
// Copied from golint
var commonInitialisms = []string{"API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "LHS", "QPS", "RAM", "RHS", "RPC", "SLA", "SMTP", "SSH", "TLS", "TTL", "UI", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XSRF", "XSS"}
var commonInitialismsReplacer *strings.Replacer
func init() {
var commonInitialismsForReplacer []string
for _, initialism := range commonInitialisms {
commonInitialismsForReplacer = append(commonInitialismsForReplacer, initialism, strings.Title(strings.ToLower(initialism)))
}
commonInitialismsReplacer = strings.NewReplacer(commonInitialismsForReplacer...)
}
var smap = map[string]string{}
var mutex = &sync.RWMutex{}
func ToDBName(name string) string {
mutex.RLock()
v, ok := smap[name]
mutex.RUnlock()
if ok {
return v
}
value := commonInitialismsReplacer.Replace(name)
buf := bytes.NewBufferString("")
for i, v := range value {
if i > 0 && v >= 'A' && v <= 'Z' {
buf.WriteRune('_')
}
buf.WriteRune(v)
}
s := strings.ToLower(buf.String())
mutex.Lock()
smap[name] = s
mutex.Unlock()
return s
}
type expr struct {
expr string
args []interface{}
}
func Expr(expression string, args ...interface{}) *expr {
return &expr{expr: expression, args: args}
}