Isolate helpers in their own package (#63)

This commit is contained in:
Manfred Touron 2017-05-19 10:02:40 +02:00
parent 62b7b2227e
commit 31a84ee58f
No known key found for this signature in database
GPG Key ID: 9CCF47DF1FD978A1
3 changed files with 16 additions and 3 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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")
}