Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gogenproto] Skip generating mappings for proto files that already use go_package #37

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions gogenproto/gen/generate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package gen

import (
"bufio"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
Expand Down Expand Up @@ -64,6 +66,15 @@ func (g Generate) Run() error {
return err
}
for _, path := range protoImportPaths {
hasGoPackage, err := protoFileHasGoPackage(path)
if err != nil {
return err
}
if hasGoPackage {
// Don't make a mapping for this, it already specifies its output package name
continue
}

relPath, err := filepath.Rel(includePath, path)
if err != nil {
return err
Expand Down Expand Up @@ -125,6 +136,27 @@ func (g Generate) findProtos(dir string, recurse bool) ([]string, error) {
return protoList, err
}

// A basic check for whether a proto file has a go_package option declared.
func protoFileHasGoPackage(path string) (bool, error) {
f, err := os.Open(path)
if err != nil {
return false, err
}
defer f.Close()

// Read line-by-line instead of loading the whole file into memory
scanner := bufio.NewScanner(f)
if err != nil {
return false, err
}
for scanner.Scan() {
if strings.Contains(scanner.Text(), "option go_package =") {
return true, nil
}
}
return false, nil
}

type logPipe struct{}

func (lw logPipe) Write(p []byte) (n int, err error) {
Expand Down
Loading