From 895857595895691222b9dda3f091fd92f7193320 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Wed, 9 Nov 2016 18:38:32 +0100 Subject: [PATCH] Add filename templating support (fix #5) --- encoder.go | 65 +++++++++++++++++++++++++------ examples/dummy/output/export.json | 3 +- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/encoder.go b/encoder.go index 2000293..39b107a 100644 --- a/encoder.go +++ b/encoder.go @@ -30,6 +30,7 @@ type Ast struct { PWD string `json:"pwd"` Debug bool `json:"debug"` File *descriptor.FileDescriptorProto `json:"file"` + RawFilename string `json:"raw-filename"` Filename string `json:"filename"` Service *descriptor.ServiceDescriptorProto `json:"service"` } @@ -80,14 +81,8 @@ func (e *GenericTemplateBasedEncoder) templates() ([]string, error) { return filenames, nil } -func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, error) { - fullPath := filepath.Join(e.templateDir, templateFilename) - - tmpl, err := template.New(templateFilename).Funcs(funcmap.FuncMap).ParseFiles(fullPath) - if err != nil { - return "", err - } - +func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) { + // prepare the ast passed to the template engine hostname, _ := os.Hostname() pwd, _ := os.Getwd() goPwd := "" @@ -104,10 +99,34 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str PWD: pwd, GoPWD: goPwd, File: e.file, - Filename: templateFilename, - Service: e.service, + RawFilename: templateFilename, + // Filename: "", + Service: e.service, + } + return &ast, nil +} + +func (e *GenericTemplateBasedEncoder) buildContent(templateFilename 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 } + 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 + } + + // generate the content buffer := new(bytes.Buffer) if err := tmpl.Execute(buffer, ast); err != nil { return "", err @@ -116,6 +135,24 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str 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 +} + func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File { files := []*plugin_go.CodeGeneratorResponse_File{} @@ -125,13 +162,17 @@ func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse } for _, templateFilename := range templates { - filename := templateFilename[0 : len(templateFilename)-len(".tmpl")] - content, err := e.buildContent(templateFilename) if err != nil { panic(err) } + 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, diff --git a/examples/dummy/output/export.json b/examples/dummy/output/export.json index bd2b309..2bd1687 100644 --- a/examples/dummy/output/export.json +++ b/examples/dummy/output/export.json @@ -1,5 +1,5 @@ { - "build-date": "2016-11-07T10:27:35.986181944+01:00", + "build-date": "2016-11-09T18:39:15.154031816+01:00", "build-hostname": "moul-rose.local", "build-user": "moul", "go-pwd": "github.com/moul/protoc-gen-gotemplate/examples/dummy", @@ -820,6 +820,7 @@ }, "syntax": "proto3" }, + "raw-filename": "export.json.tmpl", "filename": "export.json.tmpl", "service": { "name": "DummyService",