gorm/utils.go

49 lines
1.2 KiB
Go
Raw Normal View History

2013-10-25 14:04:48 +04:00
package gorm
2013-10-26 05:49:40 +04:00
import (
"bytes"
2013-10-26 05:49:40 +04:00
"strings"
)
2015-02-18 05:19:34 +03:00
// 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...)
}
2015-02-18 04:26:35 +03:00
var smap = map[string]string{}
2013-12-15 06:09:19 +04:00
2015-02-18 05:19:34 +03:00
func ToDBName(name string) string {
if v, ok := smap[name]; ok {
2013-11-17 04:28:30 +04:00
return v
}
2015-02-18 05:19:34 +03:00
value := commonInitialismsReplacer.Replace(name)
buf := bytes.NewBufferString("")
2015-02-18 05:19:34 +03:00
for i, v := range value {
if i > 0 && v >= 'A' && v <= 'Z' {
buf.WriteRune('_')
}
buf.WriteRune(v)
}
2013-11-17 04:28:30 +04:00
s := strings.ToLower(buf.String())
2015-02-18 05:19:34 +03:00
smap[name] = s
2013-11-17 04:28:30 +04:00
return s
}
2015-02-24 17:06:35 +03:00
type expr struct {
expr string
args []interface{}
}
func Expr(expression string, args ...interface{}) *expr {
return &expr{expr: expression, args: args}
}