From b3d0898bf3e36954714d8c9983ac661b5a307664 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 31 Aug 2015 15:07:06 +0800 Subject: [PATCH] Add safeMap back --- utils.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/utils.go b/utils.go index ca7e04e8..b457f321 100644 --- a/utils.go +++ b/utils.go @@ -3,6 +3,7 @@ package gorm import ( "bytes" "strings" + "sync" ) // Copied from golint @@ -17,10 +18,31 @@ func init() { 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 { - if v, ok := smap[name]; ok { + if v := smap.Get(name); v != "" { return v } @@ -34,7 +56,7 @@ func ToDBName(name string) string { } s := strings.ToLower(buf.String()) - smap[name] = s + smap.Set(name, s) return s }