-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate import paths in accordance with the standard Go package layout.
If a .proto imports "a/b/c/foo.proto", we assume that will generate "a/b/c/foo.pb.go". Previously we would try to import that as "a/b/c/foo.pb", which makes no sense if this is a standard Go source layout, so we switch to trying to import that as "a/b/c". The `M` parameter permits overridding this anyway. Fixes #8.
- Loading branch information
Showing
4 changed files
with
12 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1161,26 +1161,25 @@ func (g *Generator) generateImports() { | |
continue | ||
} | ||
filename := goFileName(s) | ||
// By default, import path is the dirname of the Go filename. | ||
importPath := path.Dir(filename) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dsymonds
Author
Contributor
|
||
if substitution, ok := g.ImportMap[s]; ok { | ||
filename = substitution | ||
} | ||
filename = g.ImportPrefix + filename | ||
if strings.HasSuffix(filename, ".go") { | ||
filename = filename[0 : len(filename)-3] | ||
importPath = substitution | ||
} | ||
importPath = g.ImportPrefix + importPath | ||
// Skip weak imports. | ||
if g.weak(int32(i)) { | ||
g.P("// skipping weak import ", fd.PackageName(), " ", strconv.Quote(filename)) | ||
g.P("// skipping weak import ", fd.PackageName(), " ", strconv.Quote(importPath)) | ||
continue | ||
} | ||
if _, ok := g.usedPackages[fd.PackageName()]; ok { | ||
g.P("import ", fd.PackageName(), " ", strconv.Quote(filename)) | ||
g.P("import ", fd.PackageName(), " ", strconv.Quote(importPath)) | ||
} else { | ||
// TODO: Re-enable this when we are more feature-complete. | ||
// For instance, some protos use foreign field extensions, which we don't support. | ||
// Until then, this is just annoying spam. | ||
//log.Printf("protoc-gen-go: discarding unused import from %v: %v", *g.file.Name, s) | ||
g.P("// discarding unused import ", fd.PackageName(), " ", strconv.Quote(filename)) | ||
g.P("// discarding unused import ", fd.PackageName(), " ", strconv.Quote(importPath)) | ||
} | ||
} | ||
g.P() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
What if filename is like "test.proto" (w/o a dir name)?
Try to run this to see whether it works:
// dep.proto
package proto;
message Dep {
required int i = 1;
};
// test.proto
import "dep.proto"
package proto;
message Test {
optional Dep dep = 1;
}
protoc --go_out=. dep.proto
protoc --go_out=. test.proto