Skip to content

Commit

Permalink
Isolate helpers in their own package (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed May 19, 2017
1 parent 62b7b22 commit 31a84ee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 4 additions & 2 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
10 changes: 9 additions & 1 deletion helpers.go → helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pgghelpers

import (
"encoding/json"
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 31a84ee

Please sign in to comment.