🐛 fix race condition on ProtoHelpersFuncMap

This commit is contained in:
Quentin Perez 2016-12-15 12:34:56 +01:00
parent 2ab581a1d6
commit 8b7fa40e87
No known key found for this signature in database
GPG Key ID: 7C6DCB859CF22206

View File

@ -37,6 +37,12 @@ var ProtoHelpersFuncMap = template.FuncMap{
},
}
func init() {
for k, v := range sprig.TxtFuncMap() {
ProtoHelpersFuncMap[k] = v
}
}
type GenericTemplateBasedEncoder struct {
templateDir string
service *descriptor.ServiceDescriptorProto
@ -125,9 +131,6 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err
Service: e.service,
}
buffer := new(bytes.Buffer)
for k, v := range sprig.TxtFuncMap() {
ProtoHelpersFuncMap[k] = v
}
tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename)
if err != nil {
return nil, err
@ -143,9 +146,7 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str
// initialize template engine
fullPath := filepath.Join(e.templateDir, templateFilename)
templateName := filepath.Base(fullPath)
for k, v := range sprig.TxtFuncMap() {
ProtoHelpersFuncMap[k] = v
}
tmpl, err := template.New(templateName).Funcs(ProtoHelpersFuncMap).ParseFiles(fullPath)
if err != nil {
return "", "", err