From 8b7fa40e8739675328d10afb8b920a6c51812517 Mon Sep 17 00:00:00 2001 From: Quentin Perez Date: Thu, 15 Dec 2016 12:34:56 +0100 Subject: [PATCH] :bug: fix race condition on ProtoHelpersFuncMap --- encoder.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/encoder.go b/encoder.go index 330370c..6cf6181 100644 --- a/encoder.go +++ b/encoder.go @@ -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