protogen: add an option to rewrite import paths
This allows us to implement the import_prefix parameter in the v1
protoc-gen-go.
Drop support for import_prefix in protogen, and explicitly produce an
error if it is used in the v2 protoc-gen-go.
Change-Id: I66136b6b3affa3c0e9a93dc565619c90c42c0ecc
Reviewed-on: https://go-review.googlesource.com/138257
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/protogen/protogen_test.go b/protogen/protogen_test.go
index 237226c..6d8e5af 100644
--- a/protogen/protogen_test.go
+++ b/protogen/protogen_test.go
@@ -307,6 +307,42 @@
}
}
+func TestImportRewrites(t *testing.T) {
+ gen, err := New(&pluginpb.CodeGeneratorRequest{}, &Options{
+ ImportRewriteFunc: func(i GoImportPath) GoImportPath {
+ return "prefix/" + i
+ },
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ g := gen.NewGeneratedFile("foo.go", "golang.org/x/foo")
+ g.P("package foo")
+ g.P("var _ = ", GoIdent{GoName: "X", GoImportPath: "golang.org/x/bar"})
+ want := `package foo
+
+import bar "prefix/golang.org/x/bar"
+
+var _ = bar.X
+`
+ got, err := g.Content()
+ if err != nil {
+ t.Fatalf("g.Content() = %v", err)
+ }
+ if want != string(got) {
+ t.Fatalf(`want:
+==========
+%v
+==========
+
+got:
+==========
+%v
+==========`,
+ want, string(got))
+ }
+}
+
// makeRequest returns a CodeGeneratorRequest for the given protoc inputs.
//
// It does this by running protoc with the current binary as the protoc-gen-go