mirror of https://github.com/go-gorm/gorm.git
* fix race condition
This commit is contained in:
parent
1e6be1646e
commit
bd93629e32
17
utils.go
17
utils.go
|
@ -6,14 +6,23 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var toSnakeMap = map[string]string{}
|
type SnakeMap struct {
|
||||||
|
Map map[string]string
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
var snake = SnakeMap{Map:map[string]string{}}
|
||||||
var toUpperMap = map[string]string{}
|
var toUpperMap = map[string]string{}
|
||||||
|
|
||||||
func toSnake(u string) string {
|
func toSnake(u string) string {
|
||||||
if v := toSnakeMap[u]; v != "" {
|
snake.mu.RLock()
|
||||||
|
if v := snake.Map[u]; v != "" {
|
||||||
|
snake.mu.RUnlock()
|
||||||
return v
|
return v
|
||||||
|
} else {
|
||||||
|
snake.mu.RUnlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := bytes.NewBufferString("")
|
buf := bytes.NewBufferString("")
|
||||||
|
@ -25,7 +34,9 @@ func toSnake(u string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
s := strings.ToLower(buf.String())
|
s := strings.ToLower(buf.String())
|
||||||
toSnakeMap[u] = s
|
snake.mu.Lock()
|
||||||
|
defer snake.mu.Unlock()
|
||||||
|
snake.Map[u] = s
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue