Sfoglia il codice sorgente

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.
David Symonds 10 anni fa
parent
commit
dded9133a9

+ 7 - 8
protoc-gen-go/generator/generator.go

@@ -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)
 		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()

+ 3 - 3
protoc-gen-go/plugin/Makefile

@@ -35,9 +35,9 @@
 regenerate:
 	echo WARNING! THIS RULE IS PROBABLY NOT RIGHT FOR YOUR INSTALLATION
 	cd $(HOME)/src/protobuf/src && \
-	protoc --go_out=. ./google/protobuf/compiler/plugin.proto && \
-	cat ./google/protobuf/compiler/plugin.pb.go | \
-		sed '/^import/s;google/protobuf/descriptor.pb;github.com/golang/protobuf/protoc-gen-go/descriptor;' > $(GOPATH)/src/github.com/golang/protobuf/protoc-gen-go/plugin/plugin.pb.go
+	protoc --go_out=Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor:. \
+	  ./google/protobuf/compiler/plugin.proto && \
+	cat ./google/protobuf/compiler/plugin.pb.go > $(GOPATH)/src/github.com/golang/protobuf/protoc-gen-go/plugin/plugin.pb.go
 
 restore:
 	cp plugin.pb.golden plugin.pb.go

+ 1 - 1
protoc-gen-go/testdata/my_test/test.pb.go

@@ -23,7 +23,7 @@ package my_test
 import proto "github.com/golang/protobuf/proto"
 import math "math"
 
-// discarding unused import multitest2 "multi/multi1.pb"
+// discarding unused import multitest2 "multi"
 
 // Reference imports to suppress errors if they are not otherwise used.
 var _ = proto.Marshal

+ 1 - 1
protoc-gen-go/testdata/my_test/test.pb.go.golden

@@ -23,7 +23,7 @@ package my_test
 import proto "github.com/golang/protobuf/proto"
 import math "math"
 
-// discarding unused import multitest2 "multi/multi1.pb"
+// discarding unused import multitest2 "multi"
 
 // Reference imports to suppress errors if they are not otherwise used.
 var _ = proto.Marshal