From b4d1f1f9bb55f8c975a6c7d10e9c22f934dedf1c Mon Sep 17 00:00:00 2001 From: Quentin Perez Date: Wed, 14 Dec 2016 10:33:08 +0100 Subject: [PATCH 1/5] :heavy_minus_sign: remove kr/fs dependency --- encoder.go | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/encoder.go b/encoder.go index 39b107a..1009120 100644 --- a/encoder.go +++ b/encoder.go @@ -11,7 +11,6 @@ import ( "github.com/golang/protobuf/protoc-gen-go/descriptor" "github.com/golang/protobuf/protoc-gen-go/plugin" - "github.com/kr/fs" "github.com/moul/funcmap" ) @@ -53,32 +52,27 @@ func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.Serv func (e *GenericTemplateBasedEncoder) templates() ([]string, error) { filenames := []string{} - walker := fs.Walk(e.templateDir) - for walker.Step() { - if err := walker.Err(); err != nil { - return nil, err - } - - if walker.Stat().IsDir() { - continue - } - - if filepath.Ext(walker.Path()) != ".tmpl" { - continue - } - - rel, err := filepath.Rel(e.templateDir, walker.Path()) + err := filepath.Walk(e.templateDir, func(path string, info os.FileInfo, err error) error { if err != nil { - return nil, err + return err + } + if info.IsDir() { + return nil + } + if filepath.Ext(path) != ".tmpl" { + return nil + } + rel, err := filepath.Rel(e.templateDir, path) + if err != nil { + return err } if e.debug { log.Printf("new template: %q", rel) } - filenames = append(filenames, rel) - } - - return filenames, nil + return nil + }) + return filenames, err } func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) { From a4749d98ae150b96bba5611a0936896165ea8ab6 Mon Sep 17 00:00:00 2001 From: Quentin Perez Date: Wed, 14 Dec 2016 11:04:34 +0100 Subject: [PATCH 2/5] :pencil2: json tag --- encoder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/encoder.go b/encoder.go index 1009120..cf66ffa 100644 --- a/encoder.go +++ b/encoder.go @@ -25,7 +25,7 @@ type Ast struct { BuildDate time.Time `json:"build-date"` BuildHostname string `json:"build-hostname"` BuildUser string `json:"build-user"` - GoPWD string `json:"go-pwd",omitempty` + GoPWD string `json:"go-pwd,omitempty"` PWD string `json:"pwd"` Debug bool `json:"debug"` File *descriptor.FileDescriptorProto `json:"file"` From 1afdd936dae083360b950e10f6edb5e548c35cd9 Mon Sep 17 00:00:00 2001 From: Quentin Perez Date: Wed, 14 Dec 2016 11:06:24 +0100 Subject: [PATCH 3/5] :hammer: rework genAst --- encoder.go | 47 ++++++++++++++++------------------------------- 1 file changed, 16 insertions(+), 31 deletions(-) diff --git a/encoder.go b/encoder.go index cf66ffa..bbcd476 100644 --- a/encoder.go +++ b/encoder.go @@ -94,57 +94,42 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err GoPWD: goPwd, File: e.file, RawFilename: templateFilename, - // Filename: "", - Service: e.service, + Filename: "", + Service: e.service, } + buffer := new(bytes.Buffer) + tmpl, err := template.New("").Funcs(funcmap.FuncMap).Parse(templateFilename) + if err != nil { + return nil, err + } + if err := tmpl.Execute(buffer, ast); err != nil { + return nil, err + } + ast.Filename = buffer.String() return &ast, nil } -func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, error) { +func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, string, error) { // initialize template engine fullPath := filepath.Join(e.templateDir, templateFilename) templateName := filepath.Base(fullPath) tmpl, err := template.New(templateName).Funcs(funcmap.FuncMap).ParseFiles(fullPath) if err != nil { - return "", err + return "", "", err } ast, err := e.genAst(templateFilename) if err != nil { - return "", err - } - - // translate the filename - ast.Filename, err = e.translateString(templateFilename, templateFilename) - if err != nil { - return "", err + return "", "", err } // generate the content buffer := new(bytes.Buffer) if err := tmpl.Execute(buffer, ast); err != nil { - return "", err + return "", "", err } - return buffer.String(), nil -} - -func (e *GenericTemplateBasedEncoder) translateString(input string, templateFilename string) (string, error) { - buffer := new(bytes.Buffer) - tmpl, err := template.New("").Funcs(funcmap.FuncMap).Parse(input) - if err != nil { - return "", err - } - - ast, err := e.genAst(templateFilename) - if err != nil { - return "", err - } - - if err := tmpl.Execute(buffer, ast); err != nil { - return "", err - } - return buffer.String(), nil + return buffer.String(), ast.Filename, nil } func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File { From aa9a47426231fdd9c9e50a9aec669bdaf911eb5a Mon Sep 17 00:00:00 2001 From: Quentin Perez Date: Wed, 14 Dec 2016 11:07:05 +0100 Subject: [PATCH 4/5] :zap: generate tmpl in parallel --- encoder.go | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/encoder.go b/encoder.go index bbcd476..893bce7 100644 --- a/encoder.go +++ b/encoder.go @@ -133,30 +133,39 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str } func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File { - files := []*plugin_go.CodeGeneratorResponse_File{} - templates, err := e.templates() if err != nil { log.Fatalf("cannot get templates from %q: %v", e.templateDir, err) } + length := len(templates) + files := make([]*plugin_go.CodeGeneratorResponse_File, 0, length) + errChan := make(chan error, length) + resultChan := make(chan *plugin_go.CodeGeneratorResponse_File, length) for _, templateFilename := range templates { - content, err := e.buildContent(templateFilename) - if err != nil { - panic(err) - } + go func(tmpl string) { + content, translatedFilename, err := e.buildContent(tmpl) + if err != nil { + errChan <- err + return + } + filename := translatedFilename[:len(translatedFilename)-len(".tmpl")] - translatedFilename, err := e.translateString(templateFilename, templateFilename) - if err != nil { - panic(err) - } - filename := translatedFilename[0 : len(translatedFilename)-len(".tmpl")] - - files = append(files, &plugin_go.CodeGeneratorResponse_File{ - Content: &content, - Name: &filename, - }) + resultChan <- &plugin_go.CodeGeneratorResponse_File{ + Content: &content, + Name: &filename, + } + }(templateFilename) + } + for i := 0; i < length; i++ { + select { + case f := <-resultChan: + files = append(files, f) + case err = <-errChan: + } + } + if err != nil { + panic(err) } - return files } From ba1f64a73141ed0c29447960ccca64fac77ae1f5 Mon Sep 17 00:00:00 2001 From: Quentin Perez Date: Wed, 14 Dec 2016 11:08:51 +0100 Subject: [PATCH 5/5] don't print error when debug=false --- main.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 89f157e..3907e1b 100644 --- a/main.go +++ b/main.go @@ -43,9 +43,11 @@ func main() { templateDir = parts[1] break case "debug": - if parts[1] == "true" { + switch strings.ToLower(parts[1]) { + case "true", "t": debug = true - } else { + case "false", "f": + default: log.Printf("Err: invalid value for debug: %q", parts[1]) } break