Skip to content

Commit 8879672

Browse files
authored
Add support for --Import_path (grpc-ecosystem#507)
Fixes: grpc-ecosystem#443
1 parent 8db8c1a commit 8879672

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

protoc-gen-grpc-gateway/descriptor/registry.go

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ type Registry struct {
2525
// prefix is a prefix to be inserted to golang package paths generated from proto package names.
2626
prefix string
2727

28+
// importPath is used as the package if no input files declare go_package. If it contains slashes, everything up to the rightmost slash is ignored.
29+
importPath string
30+
2831
// pkgMap is a user-specified mapping from file path to proto package.
2932
pkgMap map[string]string
3033

@@ -212,6 +215,13 @@ func (r *Registry) SetPrefix(prefix string) {
212215
r.prefix = prefix
213216
}
214217

218+
// SetImportPath registers the importPath which is used as the package if no
219+
// input files declare go_package. If it contains slashes, everything up to the
220+
// rightmost slash is ignored.
221+
func (r *Registry) SetImportPath(importPath string) {
222+
r.importPath = importPath
223+
}
224+
215225
// ReserveGoPackageAlias reserves the unique alias of go package.
216226
// If succeeded, the alias will be never used for other packages in generated go files.
217227
// If failed, the alias is already taken by another package, so you need to use another
@@ -237,6 +247,9 @@ func (r *Registry) goPackagePath(f *descriptor.FileDescriptorProto) string {
237247
}
238248

239249
gopkg := f.Options.GetGoPackage()
250+
if len(gopkg) == 0 {
251+
gopkg = r.importPath
252+
}
240253
idx := strings.LastIndex(gopkg, "/")
241254
if idx >= 0 {
242255
if sc := strings.LastIndex(gopkg, ";"); sc > 0 {

protoc-gen-grpc-gateway/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
var (
2626
importPrefix = flag.String("import_prefix", "", "prefix to be added to go package paths for imported proto files")
27+
importPath = flag.String("import_path", "", "used as the package if no input files declare go_package. If it contains slashes, everything up to the rightmost slash is ignored.")
2728
useRequestContext = flag.Bool("request_context", true, "determine whether to use http.Request's context or not")
2829
allowDeleteBody = flag.Bool("allow_delete_body", false, "unless set, HTTP DELETE methods may not have a body")
2930
)
@@ -78,6 +79,7 @@ func main() {
7879
g := gengateway.New(reg, *useRequestContext)
7980

8081
reg.SetPrefix(*importPrefix)
82+
reg.SetImportPath(*importPath)
8183
reg.SetAllowDeleteBody(*allowDeleteBody)
8284
if err := reg.Load(req); err != nil {
8385
emitError(err)

0 commit comments

Comments
 (0)