🔨 rework genAst
This commit is contained in:
parent
a4749d98ae
commit
1afdd936da
45
encoder.go
45
encoder.go
@ -94,57 +94,42 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err
|
|||||||
GoPWD: goPwd,
|
GoPWD: goPwd,
|
||||||
File: e.file,
|
File: e.file,
|
||||||
RawFilename: templateFilename,
|
RawFilename: templateFilename,
|
||||||
// Filename: "",
|
Filename: "",
|
||||||
Service: e.service,
|
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
|
return &ast, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, error) {
|
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, string, error) {
|
||||||
// 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(funcmap.FuncMap).ParseFiles(fullPath)
|
tmpl, err := template.New(templateName).Funcs(funcmap.FuncMap).ParseFiles(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
ast, err := e.genAst(templateFilename)
|
ast, err := e.genAst(templateFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
|
||||||
|
|
||||||
// translate the filename
|
|
||||||
ast.Filename, err = e.translateString(templateFilename, templateFilename)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate the content
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
return buffer.String(), nil
|
return buffer.String(), ast.Filename, 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 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user