Isolate helpers in their own package (#63)
This commit is contained in:
parent
62b7b2227e
commit
31a84ee58f
@ -11,6 +11,8 @@ import (
|
|||||||
|
|
||||||
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
||||||
"github.com/golang/protobuf/protoc-gen-go/plugin"
|
"github.com/golang/protobuf/protoc-gen-go/plugin"
|
||||||
|
|
||||||
|
pgghelpers "github.com/moul/protoc-gen-gotemplate/helpers"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GenericTemplateBasedEncoder struct {
|
type GenericTemplateBasedEncoder struct {
|
||||||
@ -122,7 +124,7 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err
|
|||||||
Enum: e.enum,
|
Enum: e.enum,
|
||||||
}
|
}
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename)
|
tmpl, err := template.New("").Funcs(pgghelpers.ProtoHelpersFuncMap).Parse(templateFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -137,7 +139,7 @@ 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(ProtoHelpersFuncMap).ParseFiles(fullPath)
|
tmpl, err := template.New(templateName).Funcs(pgghelpers.ProtoHelpersFuncMap).ParseFiles(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package pgghelpers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"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 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{
|
var ProtoHelpersFuncMap = template.FuncMap{
|
||||||
"string": func(i interface {
|
"string": func(i interface {
|
||||||
String() string
|
String() string
|
3
main.go
3
main.go
@ -10,6 +10,8 @@ import (
|
|||||||
"github.com/golang/protobuf/protoc-gen-go/generator"
|
"github.com/golang/protobuf/protoc-gen-go/generator"
|
||||||
"github.com/golang/protobuf/protoc-gen-go/plugin"
|
"github.com/golang/protobuf/protoc-gen-go/plugin"
|
||||||
ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
|
ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
|
||||||
|
|
||||||
|
pgghelpers "github.com/moul/protoc-gen-gotemplate/helpers"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -101,6 +103,7 @@ func main() {
|
|||||||
|
|
||||||
if singlePackageMode {
|
if singlePackageMode {
|
||||||
registry = ggdescriptor.NewRegistry()
|
registry = ggdescriptor.NewRegistry()
|
||||||
|
pgghelpers.SetRegistry(registry)
|
||||||
if err := registry.Load(g.Request); err != nil {
|
if err := registry.Load(g.Request); err != nil {
|
||||||
g.Error(err, "registry: failed to load the request")
|
g.Error(err, "registry: failed to load the request")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user