Add safeMap back

This commit is contained in:
Jinzhu 2015-08-31 15:07:06 +08:00
parent 8e415788d5
commit b3d0898bf3
1 changed files with 25 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package gorm
import ( import (
"bytes" "bytes"
"strings" "strings"
"sync"
) )
// Copied from golint // Copied from golint
@ -17,10 +18,31 @@ func init() {
commonInitialismsReplacer = strings.NewReplacer(commonInitialismsForReplacer...) commonInitialismsReplacer = strings.NewReplacer(commonInitialismsForReplacer...)
} }
var smap = map[string]string{} type safeMap struct {
m map[string]string
l *sync.RWMutex
}
func (s *safeMap) Set(key string, value string) {
s.l.Lock()
defer s.l.Unlock()
s.m[key] = value
}
func (s *safeMap) Get(key string) string {
s.l.RLock()
defer s.l.RUnlock()
return s.m[key]
}
func newSafeMap() *safeMap {
return &safeMap{l: new(sync.RWMutex), m: make(map[string]string)}
}
var smap = newSafeMap()
func ToDBName(name string) string { func ToDBName(name string) string {
if v, ok := smap[name]; ok { if v := smap.Get(name); v != "" {
return v return v
} }
@ -34,7 +56,7 @@ func ToDBName(name string) string {
} }
s := strings.ToLower(buf.String()) s := strings.ToLower(buf.String())
smap[name] = s smap.Set(name, s)
return s return s
} }