Add filename templating support (fix #5)

This commit is contained in:
Manfred Touron 2016-11-09 18:38:32 +01:00
parent 9222cc15b9
commit 8958575958
No known key found for this signature in database
GPG Key ID: 9CCF47DF1FD978A1
2 changed files with 55 additions and 13 deletions

View File

@ -30,6 +30,7 @@ type Ast struct {
PWD string `json:"pwd"` PWD string `json:"pwd"`
Debug bool `json:"debug"` Debug bool `json:"debug"`
File *descriptor.FileDescriptorProto `json:"file"` File *descriptor.FileDescriptorProto `json:"file"`
RawFilename string `json:"raw-filename"`
Filename string `json:"filename"` Filename string `json:"filename"`
Service *descriptor.ServiceDescriptorProto `json:"service"` Service *descriptor.ServiceDescriptorProto `json:"service"`
} }
@ -80,14 +81,8 @@ func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {
return filenames, nil return filenames, nil
} }
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, error) { func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) {
fullPath := filepath.Join(e.templateDir, templateFilename) // prepare the ast passed to the template engine
tmpl, err := template.New(templateFilename).Funcs(funcmap.FuncMap).ParseFiles(fullPath)
if err != nil {
return "", err
}
hostname, _ := os.Hostname() hostname, _ := os.Hostname()
pwd, _ := os.Getwd() pwd, _ := os.Getwd()
goPwd := "" goPwd := ""
@ -104,10 +99,34 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str
PWD: pwd, PWD: pwd,
GoPWD: goPwd, GoPWD: goPwd,
File: e.file, File: e.file,
Filename: templateFilename, RawFilename: templateFilename,
// Filename: "",
Service: e.service, 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) buffer := new(bytes.Buffer)
if err := tmpl.Execute(buffer, ast); err != nil { if err := tmpl.Execute(buffer, ast); err != nil {
return "", err return "", err
@ -116,6 +135,24 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str
return buffer.String(), nil 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 { func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
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 { for _, templateFilename := range templates {
filename := templateFilename[0 : len(templateFilename)-len(".tmpl")]
content, err := e.buildContent(templateFilename) content, err := e.buildContent(templateFilename)
if err != nil { if err != nil {
panic(err) 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{ files = append(files, &plugin_go.CodeGeneratorResponse_File{
Content: &content, Content: &content,
Name: &filename, Name: &filename,

View File

@ -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-hostname": "moul-rose.local",
"build-user": "moul", "build-user": "moul",
"go-pwd": "github.com/moul/protoc-gen-gotemplate/examples/dummy", "go-pwd": "github.com/moul/protoc-gen-gotemplate/examples/dummy",
@ -820,6 +820,7 @@
}, },
"syntax": "proto3" "syntax": "proto3"
}, },
"raw-filename": "export.json.tmpl",
"filename": "export.json.tmpl", "filename": "export.json.tmpl",
"service": { "service": {
"name": "DummyService", "name": "DummyService",