Skip to content

Commit

Permalink
Refactor: Extract method and move it up the caller stack
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 bd75ccf commit 84413f6
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions metaschema/templates/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ import (
)

func GenerateAll(metaschema *parser.Metaschema, baseDir string) error {
return GenerateModels(metaschema, baseDir)
}

func GenerateModels(metaschema *parser.Metaschema, baseDir string) error {
t, err := newTemplate(baseDir)
pkgDir, err := ensurePkgDir(metaschema, baseDir)
if err != nil {
return err
}
return GenerateModels(metaschema, baseDir, pkgDir)
}

packageName := metaschema.GoPackageName()
dir := filepath.Join(baseDir, packageName)
err = os.MkdirAll(dir, os.FileMode(0722))
func GenerateModels(metaschema *parser.Metaschema, baseDir, pkgDir string) error {
t, err := newTemplate(baseDir)
if err != nil {
return err
}
f, err := os.Create(fmt.Sprintf("%s/generated_models.go", dir))

f, err := os.Create(fmt.Sprintf("%s/generated_models.go", pkgDir))
if err != nil {
return err
}
Expand Down Expand Up @@ -86,3 +84,9 @@ func newTemplate(baseDir string) (*template.Template, error) {
"getImports": getImports,
}).Parse(string(tempText))
}

func ensurePkgDir(metaschema *parser.Metaschema, baseDir string) (string, error) {
dir := filepath.Join(baseDir, metaschema.GoPackageName())
err := os.MkdirAll(dir, os.FileMode(0722))
return dir, err
}

0 comments on commit 84413f6

Please sign in to comment.