mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-09-20 07:21:22 +00:00
code-gen: refactoring, re-use function map in templates
This commit is contained in:
parent
454c78a59f
commit
0b92f97b9c
@ -40,26 +40,21 @@ func executeTemplate(w io.Writer, name, path, commit string, fmap template.FuncM
|
||||
val = strings.ReplaceAll(val, "`", "`+\"`\"+`")
|
||||
return fmt.Sprintf("`%s`", val)
|
||||
}
|
||||
|
||||
const headerTmpl = "header.go.tmpl"
|
||||
headerPath := filepath.Join(filepath.Dir(path), headerTmpl)
|
||||
|
||||
h := template.Must(template.New(headerTmpl).Funcs(template.FuncMap{
|
||||
"getCommit": getCommit,
|
||||
"stringVal": stringVal,
|
||||
}).ParseFiles(headerPath))
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
if err := h.Execute(buf, data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if fmap == nil {
|
||||
fmap = make(template.FuncMap)
|
||||
}
|
||||
fmap["getCommit"] = getCommit
|
||||
fmap["stringVal"] = stringVal
|
||||
|
||||
const headerTmpl = "header.go.tmpl"
|
||||
headerPath := filepath.Join(filepath.Dir(path), headerTmpl)
|
||||
|
||||
h := template.Must(template.New(headerTmpl).Funcs(fmap).ParseFiles(headerPath))
|
||||
buf := bytes.NewBuffer(nil)
|
||||
if err := h.Execute(buf, data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t := template.Must(template.New(name).Funcs(fmap).ParseFiles(path))
|
||||
if err := t.Execute(buf, data); err != nil {
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user