Refactor proto file writing into separate function
The file writing process for protobuf files has been extracted into a new function called writeProtoFile(). This refactoring simplifies the main function. The code is cleaner and more manageable this way, improving maintainability and readability.
This commit is contained in:
parent
21c947e107
commit
0bb9027957
|
|
@ -305,35 +305,41 @@ func WriteProtoFiles(outProtoDirPath string, descSource DescriptorSource, symbol
|
||||||
// now expand that to include transitive dependencies in topologically sorted
|
// now expand that to include transitive dependencies in topologically sorted
|
||||||
// order (such that file always appears after its dependencies)
|
// order (such that file always appears after its dependencies)
|
||||||
expandedFiles := make(map[string]struct{}, len(fds))
|
expandedFiles := make(map[string]struct{}, len(fds))
|
||||||
allFilesSlice := make([]*desc.FileDescriptor, 0, len(fds))
|
allFileDescriptors := make([]*desc.FileDescriptor, 0, len(fds))
|
||||||
for _, filename := range filenames {
|
for _, filename := range filenames {
|
||||||
allFilesSlice = addFilesToFileDescriptorList(allFilesSlice, expandedFiles, fds[filename])
|
allFileDescriptors = addFilesToFileDescriptorList(allFileDescriptors, expandedFiles, fds[filename])
|
||||||
}
|
}
|
||||||
pr := protoprint.Printer{}
|
pr := protoprint.Printer{}
|
||||||
// now we can serialize to files
|
// now we can serialize to files
|
||||||
for _, fd := range allFilesSlice {
|
for i := range allFileDescriptors {
|
||||||
fdFQName := fd.GetFullyQualifiedName()
|
if err := writeProtoFile(outProtoDirPath, allFileDescriptors[i], &pr); err != nil {
|
||||||
dirPath := filepath.Dir(fdFQName)
|
return err
|
||||||
outFilepath := filepath.Join(outProtoDirPath, dirPath)
|
|
||||||
if err := os.MkdirAll(outFilepath, 0755); err != nil {
|
|
||||||
return fmt.Errorf("failed to create directory %q: %v", outFilepath, err)
|
|
||||||
}
|
|
||||||
fileName := filepath.Base(fdFQName)
|
|
||||||
filePath := filepath.Join(outFilepath, fileName)
|
|
||||||
f, err := os.Create(filePath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create file %q: %v", filePath, err)
|
|
||||||
}
|
|
||||||
err = pr.PrintProtoFile(fd, f)
|
|
||||||
if err == nil {
|
|
||||||
_ = f.Close()
|
|
||||||
} else {
|
|
||||||
return fmt.Errorf("failed to write file %q: %v", filePath, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeProtoFile(outProtoDirPath string, fd *desc.FileDescriptor, pr *protoprint.Printer) error {
|
||||||
|
fdFQName := fd.GetFullyQualifiedName()
|
||||||
|
dirPath := filepath.Dir(fdFQName)
|
||||||
|
outFilepath := filepath.Join(outProtoDirPath, dirPath)
|
||||||
|
if err := os.MkdirAll(outFilepath, 0755); err != nil {
|
||||||
|
return fmt.Errorf("failed to create directory %q: %v", outFilepath, err)
|
||||||
|
}
|
||||||
|
fileName := filepath.Base(fdFQName)
|
||||||
|
filePath := filepath.Join(outFilepath, fileName)
|
||||||
|
|
||||||
|
f, err := os.Create(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create proto file: %v", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
if err := pr.PrintProtoFile(fd, f); err != nil {
|
||||||
|
return fmt.Errorf("failed to write proto file: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func getFileDescriptors(symbols []string, descSource DescriptorSource) ([]string, map[string]*desc.FileDescriptor, error) {
|
func getFileDescriptors(symbols []string, descSource DescriptorSource) ([]string, map[string]*desc.FileDescriptor, error) {
|
||||||
// compute set of file descriptors
|
// compute set of file descriptors
|
||||||
filenames := make([]string, 0, len(symbols))
|
filenames := make([]string, 0, len(symbols))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue