Skip to content

Commit

Permalink
Refactoring out the deduction of package name and generation of the c…
Browse files Browse the repository at this point in the history
…ontainer from ParseYAMLFile(). (#16)
  • Loading branch information
vgasparyan1995 authored and elliotchance committed Oct 9, 2019
1 parent c4be90e commit b162270
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 7 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type File struct {
file *ast.File
}

func ParseYAMLFile(filepath, outputFile string) (*File, error) {
func ParseYAMLFile(filepath string) (*File, error) {
f, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
Expand All @@ -29,9 +29,13 @@ func ParseYAMLFile(filepath, outputFile string) (*File, error) {
if err != nil {
return nil, err
}

all.fset = token.NewFileSet()
packageLine := fmt.Sprintf("// Code generated by dingo; DO NOT EDIT\npackage %s", all.getPackageName(filepath))
return all, nil
}

func GenerateContainer(all *File, packageName string, outputFile string) (*File, error) {
var err error
packageLine := fmt.Sprintf("// Code generated by dingo; DO NOT EDIT\npackage %s", packageName)
all.file, err = parser.ParseFile(all.fset, outputFile, packageLine, parser.ParseComments)
if err != nil {
return nil, err
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ func main() {
dingoYMLPath := "dingo.yml"
outputFile := "dingo.go"

file, err := ParseYAMLFile(dingoYMLPath, outputFile)
file, err := ParseYAMLFile(dingoYMLPath)
if err != nil {
log.Fatalln(err)
}
packageName := file.getPackageName(dingoYMLPath)
file, err = GenerateContainer(file, packageName, outputFile)
if err != nil {
log.Fatalln(err)
}
Expand Down

0 comments on commit b162270

Please sign in to comment.