fix(store): Fix store concurrency

This commit is contained in:
Guilhem Fanton 2018-08-23 18:48:43 +02:00
parent 90a63053d4
commit 5b8733f8ec

View File

@ -6,6 +6,7 @@ import (
"reflect" "reflect"
"regexp" "regexp"
"strings" "strings"
"sync"
"text/template" "text/template"
"github.com/Masterminds/sprig" "github.com/Masterminds/sprig"
@ -22,13 +23,6 @@ var (
registry *ggdescriptor.Registry // some helpers need access to registry registry *ggdescriptor.Registry // some helpers need access to registry
) )
// Utility to store some vars across multiple scope
var store = make(map[string]interface{})
func SetRegistry(reg *ggdescriptor.Registry) {
registry = reg
}
var ProtoHelpersFuncMap = template.FuncMap{ var ProtoHelpersFuncMap = template.FuncMap{
"string": func(i interface { "string": func(i interface {
String() string String() string
@ -166,19 +160,50 @@ var ProtoHelpersFuncMap = template.FuncMap{
var pathMap map[interface{}]*descriptor.SourceCodeInfo_Location var pathMap map[interface{}]*descriptor.SourceCodeInfo_Location
func setStore(key string, i interface{}) string { var store = newStore()
store[key] = i
return "" // Utility to store some vars across multiple scope
type globalStore struct {
store map[string]interface{}
mu sync.Mutex
} }
func getStore(s string) interface{} { func newStore() *globalStore {
if v, ok := store[s]; ok { return &globalStore{
store: make(map[string]interface{}),
}
}
func (s *globalStore) getData(key string) interface{} {
s.mu.Lock()
defer s.mu.Unlock()
if v, ok := s.store[key]; ok {
return v return v
} }
return false return false
} }
func (s *globalStore) setData(key string, o interface{}) {
s.mu.Lock()
s.store[key] = o
s.mu.Unlock()
}
func setStore(key string, o interface{}) string {
store.setData(key, o)
return ""
}
func getStore(key string) interface{} {
return store.getData(key)
}
func SetRegistry(reg *ggdescriptor.Registry) {
registry = reg
}
func InitPathMap(file *descriptor.FileDescriptorProto) { func InitPathMap(file *descriptor.FileDescriptorProto) {
pathMap = make(map[interface{}]*descriptor.SourceCodeInfo_Location) pathMap = make(map[interface{}]*descriptor.SourceCodeInfo_Location)
addToPathMap(file.GetSourceCodeInfo(), file, []int32{}) addToPathMap(file.GetSourceCodeInfo(), file, []int32{})