From e48d798d8963796ddd3fa16ff7bb1701a80caab4 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Thu, 15 Dec 2016 12:08:00 +0100 Subject: [PATCH] Merge custom Funcmap with sprig --- encoder.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/encoder.go b/encoder.go index 5633c59..a45466d 100644 --- a/encoder.go +++ b/encoder.go @@ -14,6 +14,14 @@ import ( "github.com/golang/protobuf/protoc-gen-go/plugin" ) +var ProtoHelpersFuncMap = template.FuncMap{ + "string": func(i interface { + String() string + }) string { + return i.String() + }, +} + type GenericTemplateBasedEncoder struct { templateDir string service *descriptor.ServiceDescriptorProto @@ -102,7 +110,10 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err Service: e.service, } buffer := new(bytes.Buffer) - tmpl, err := template.New("").Funcs(sprig.TxtFuncMap()).Parse(templateFilename) + for k, v := range sprig.TxtFuncMap() { + ProtoHelpersFuncMap[k] = v + } + tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename) if err != nil { return nil, err } @@ -117,7 +128,10 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str // initialize template engine fullPath := filepath.Join(e.templateDir, templateFilename) templateName := filepath.Base(fullPath) - tmpl, err := template.New(templateName).Funcs(sprig.TxtFuncMap()).ParseFiles(fullPath) + for k, v := range sprig.TxtFuncMap() { + ProtoHelpersFuncMap[k] = v + } + tmpl, err := template.New(templateName).Funcs(ProtoHelpersFuncMap).ParseFiles(fullPath) if err != nil { return "", "", err }