Skip to content

Commit ef62b64

Browse files
authored
Add ability split path struct by file and module (#707)
1 parent 0793c34 commit ef62b64

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

generator/generator.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,17 @@ func main() {
469469
}
470470
path = filepath.Join(*outputDir, packageName, fmt.Sprintf("%s.go", packageName))
471471
}
472-
outfh := genutil.OpenFile(path)
473-
defer genutil.SyncFile(outfh)
474-
err := writeGoPathCodeSingleFile(outfh, code)
475-
if err != nil {
476-
log.Exitf("Error while writing path struct file: %v", err)
472+
if *pathStructsFileN <= 1 || packageName == pcg.PackageName {
473+
outfh := genutil.OpenFile(path)
474+
defer genutil.SyncFile(outfh)
475+
err := writeGoPathCodeSingleFile(outfh, code)
476+
if err != nil {
477+
log.Exitf("Error while writing path struct file: %v", err)
478+
}
479+
} else {
480+
if err := writePathPackage(pathCode, packageName, filepath.Join(*outputDir, packageName)); err != nil {
481+
log.Errorln(err)
482+
}
477483
}
478484
}
479485
case generatePathStructsSingleFile:
@@ -491,17 +497,24 @@ func main() {
491497
}
492498
writeGoPathCodeSingleFile(outfh, pathCode[pcg.PackageName])
493499
case generatePathStructsMultipleFiles:
494-
out := map[string]string{}
495-
// Split the path struct code into files.
496-
files, err := pathCode[pcg.PackageName].SplitFiles(*pathStructsFileN)
497-
if err != nil {
498-
log.Exitf("Error while splitting path structs code into %d files: %v\n", pathStructsFileN, err)
499-
}
500-
for i, file := range files {
501-
out[fmt.Sprintf(pathStructsFileFmt, i)] = file
502-
}
503-
if err := writeFiles(*outputDir, out); err != nil {
504-
log.Exitf("Error while writing path struct files: %v", err)
500+
if err := writePathPackage(pathCode, pcg.PackageName, *outputDir); err != nil {
501+
log.Exit(err)
505502
}
506503
}
507504
}
505+
506+
func writePathPackage(pathCode map[string]*ypathgen.GeneratedPathCode, pkgName, dir string) error {
507+
out := map[string]string{}
508+
// Split the path struct code into files.
509+
files, err := pathCode[pkgName].SplitFiles(*pathStructsFileN)
510+
if err != nil {
511+
return fmt.Errorf("error while splitting path structs code into %d files: %w", pathStructsFileN, err)
512+
}
513+
for i, file := range files {
514+
out[fmt.Sprintf(pathStructsFileFmt, i)] = file
515+
}
516+
if err := writeFiles(dir, out); err != nil {
517+
return fmt.Errorf("Error while writing path struct files: %w", err)
518+
}
519+
return nil
520+
}

0 commit comments

Comments
 (0)