If prior hooks.go is missing a func, append default version of it.
This commit is contained in:
Родитель
9e86b749a5
Коммит
a2d63d46bd
|
@ -45,9 +45,34 @@ func (h *HookRender) Render(_ string, _ *gengokit.Data) (io.Reader, error) {
|
|||
return code, nil
|
||||
}
|
||||
|
||||
// Note which hooks do not need to be added:
|
||||
for _, d := range h.ast.Decls {
|
||||
switch v := d.(type) {
|
||||
case *ast.FuncDecl:
|
||||
for _, h := range templates.Hooks {
|
||||
if h.Name == v.Name.Name {
|
||||
h.Code = ""
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Place to collect code for any missing hooks:
|
||||
extra := new(bytes.Buffer)
|
||||
|
||||
for _, hd := range templates.Hooks {
|
||||
if "" == hd.Code {
|
||||
continue
|
||||
}
|
||||
// Add source code for this missing hook:
|
||||
extra.WriteString(hd.Code)
|
||||
}
|
||||
|
||||
if err := printer.Fprint(code, h.fset, h.ast); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
code.ReadFrom(extra)
|
||||
|
||||
return code, nil
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче