Skip to content

Commit

Permalink
Refactor: rewrite main loop to allow for more templates being added
Browse files Browse the repository at this point in the history
Clean-ups before increasing complexity.
  • Loading branch information
isimluk committed Sep 5, 2020
1 parent 20bf109 commit 1fcbb79
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions metaschema/templates/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ func GenerateAll(metaschema *parser.Metaschema, baseDir string) error {
if err != nil {
return err
}
return GenerateModels(metaschema, baseDir, pkgDir)
}

func GenerateModels(metaschema *parser.Metaschema, baseDir, pkgDir string) error {
t, err := newTemplate(baseDir)
if err != nil {
return err
for _, templateName := range []string{"generated_models"} {
t, err := newTemplate(baseDir, templateName)
if err != nil {
return err
}
err = executeTemplate(t, metaschema, fmt.Sprintf("%s/%s.go", pkgDir, templateName))
if err != nil {
return err
}
}
return nil
}

f, err := os.Create(fmt.Sprintf("%s/generated_models.go", pkgDir))
func executeTemplate(t *template.Template, metaschema *parser.Metaschema, filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
Expand All @@ -48,7 +53,7 @@ func GenerateModels(metaschema *parser.Metaschema, baseDir, pkgDir string) error
return err
}

func newTemplate(baseDir string) (*template.Template, error) {
func newTemplate(baseDir, templateName string) (*template.Template, error) {
getImports := func(metaschema parser.Metaschema) string {
var imports strings.Builder
imports.WriteString("import (\n")
Expand Down Expand Up @@ -76,7 +81,7 @@ func newTemplate(baseDir string) (*template.Template, error) {
return nil, err
}

return template.New("generated_models.tmpl").Funcs(template.FuncMap{
return template.New(templateName + ".tmpl").Funcs(template.FuncMap{
"getImports": getImports,
}).Parse(string(tempText))
}
Expand Down

0 comments on commit 1fcbb79

Please sign in to comment.