From 31a84ee58fc9355bed9efcfafea958b1958d44a3 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Fri, 19 May 2017 10:02:40 +0200 Subject: [PATCH] Isolate helpers in their own package (#63) --- encoder.go | 6 ++++-- helpers.go => helpers/helpers.go | 10 +++++++++- main.go | 3 +++ 3 files changed, 16 insertions(+), 3 deletions(-) rename helpers.go => helpers/helpers.go (98%) diff --git a/encoder.go b/encoder.go index 2444d34..2406137 100644 --- a/encoder.go +++ b/encoder.go @@ -11,6 +11,8 @@ import ( "github.com/golang/protobuf/protoc-gen-go/descriptor" "github.com/golang/protobuf/protoc-gen-go/plugin" + + pgghelpers "github.com/moul/protoc-gen-gotemplate/helpers" ) type GenericTemplateBasedEncoder struct { @@ -122,7 +124,7 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err Enum: e.enum, } buffer := new(bytes.Buffer) - tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename) + tmpl, err := template.New("").Funcs(pgghelpers.ProtoHelpersFuncMap).Parse(templateFilename) if err != nil { return nil, err } @@ -137,7 +139,7 @@ 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(ProtoHelpersFuncMap).ParseFiles(fullPath) + tmpl, err := template.New(templateName).Funcs(pgghelpers.ProtoHelpersFuncMap).ParseFiles(fullPath) if err != nil { return "", "", err } diff --git a/helpers.go b/helpers/helpers.go similarity index 98% rename from helpers.go rename to helpers/helpers.go index 9ba34c8..d25194f 100644 --- a/helpers.go +++ b/helpers/helpers.go @@ -1,4 +1,4 @@ -package main +package pgghelpers import ( "encoding/json" @@ -17,6 +17,14 @@ import ( var jsReservedRe *regexp.Regexp = regexp.MustCompile(`(^|[^A-Za-z])(do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)($|[^A-Za-z])`) +var ( + registry *ggdescriptor.Registry // some helpers need access to registry +) + +func SetRegistry(reg *ggdescriptor.Registry) { + registry = reg +} + var ProtoHelpersFuncMap = template.FuncMap{ "string": func(i interface { String() string diff --git a/main.go b/main.go index 8ea27d8..618ce84 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,8 @@ import ( "github.com/golang/protobuf/protoc-gen-go/generator" "github.com/golang/protobuf/protoc-gen-go/plugin" ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor" + + pgghelpers "github.com/moul/protoc-gen-gotemplate/helpers" ) var ( @@ -101,6 +103,7 @@ func main() { if singlePackageMode { registry = ggdescriptor.NewRegistry() + pgghelpers.SetRegistry(registry) if err := registry.Load(g.Request); err != nil { g.Error(err, "registry: failed to load the request") }