From 0bb9027957a40785d7ba8bf0eb3f74b828d7f653 Mon Sep 17 00:00:00 2001 From: Eitol Date: Wed, 10 Jul 2024 23:30:04 -0400 Subject: [PATCH] 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. --- desc_source.go | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/desc_source.go b/desc_source.go index b9c6a62..9c55fd9 100644 --- a/desc_source.go +++ b/desc_source.go @@ -305,35 +305,41 @@ func WriteProtoFiles(outProtoDirPath string, descSource DescriptorSource, symbol // now expand that to include transitive dependencies in topologically sorted // order (such that file always appears after its dependencies) 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 { - allFilesSlice = addFilesToFileDescriptorList(allFilesSlice, expandedFiles, fds[filename]) + allFileDescriptors = addFilesToFileDescriptorList(allFileDescriptors, expandedFiles, fds[filename]) } pr := protoprint.Printer{} // now we can serialize to files - for _, fd := range allFilesSlice { - 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 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) + for i := range allFileDescriptors { + if err := writeProtoFile(outProtoDirPath, allFileDescriptors[i], &pr); err != nil { + return err } } 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) { // compute set of file descriptors filenames := make([]string, 0, len(symbols))