Merge custom Funcmap with sprig

This commit is contained in:
Mathieu Acthernoene 2016-12-15 12:08:00 +01:00
parent f48fe29ebf
commit e48d798d89

View File

@ -14,6 +14,14 @@ import (
"github.com/golang/protobuf/protoc-gen-go/plugin" "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 { type GenericTemplateBasedEncoder struct {
templateDir string templateDir string
service *descriptor.ServiceDescriptorProto service *descriptor.ServiceDescriptorProto
@ -102,7 +110,10 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err
Service: e.service, Service: e.service,
} }
buffer := new(bytes.Buffer) 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 { if err != nil {
return nil, err return nil, err
} }
@ -117,7 +128,10 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str
// initialize template engine // initialize template engine
fullPath := filepath.Join(e.templateDir, templateFilename) fullPath := filepath.Join(e.templateDir, templateFilename)
templateName := filepath.Base(fullPath) 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 { if err != nil {
return "", "", err return "", "", err
} }