mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-06-27 14:47:50 -03:00
code-gen: refactoring, re-use function map in templates
This commit is contained in:
@ -40,26 +40,21 @@ func executeTemplate(w io.Writer, name, path, commit string, fmap template.FuncM
|
|||||||
val = strings.ReplaceAll(val, "`", "`+\"`\"+`")
|
val = strings.ReplaceAll(val, "`", "`+\"`\"+`")
|
||||||
return fmt.Sprintf("`%s`", 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 {
|
if fmap == nil {
|
||||||
fmap = make(template.FuncMap)
|
fmap = make(template.FuncMap)
|
||||||
}
|
}
|
||||||
fmap["getCommit"] = getCommit
|
fmap["getCommit"] = getCommit
|
||||||
fmap["stringVal"] = stringVal
|
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))
|
t := template.Must(template.New(name).Funcs(fmap).ParseFiles(path))
|
||||||
if err := t.Execute(buf, data); err != nil {
|
if err := t.Execute(buf, data); err != nil {
|
||||||
return err
|
return err
|
||||||
|
Reference in New Issue
Block a user