diff --git a/protoc-gen-grpc-gateway/descriptor/registry.go b/protoc-gen-grpc-gateway/descriptor/registry.go index 9e4f3428dd3..61d6eb9bc74 100644 --- a/protoc-gen-grpc-gateway/descriptor/registry.go +++ b/protoc-gen-grpc-gateway/descriptor/registry.go @@ -25,6 +25,9 @@ type Registry struct { // prefix is a prefix to be inserted to golang package paths generated from proto package names. prefix string + // 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. + importPath string + // pkgMap is a user-specified mapping from file path to proto package. pkgMap map[string]string @@ -212,6 +215,13 @@ func (r *Registry) SetPrefix(prefix string) { r.prefix = prefix } +// SetImportPath registers the importPath which is used as the package if no +// input files declare go_package. If it contains slashes, everything up to the +// rightmost slash is ignored. +func (r *Registry) SetImportPath(importPath string) { + r.importPath = importPath +} + // ReserveGoPackageAlias reserves the unique alias of go package. // If succeeded, the alias will be never used for other packages in generated go files. // 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 { } gopkg := f.Options.GetGoPackage() + if len(gopkg) == 0 { + gopkg = r.importPath + } idx := strings.LastIndex(gopkg, "/") if idx >= 0 { if sc := strings.LastIndex(gopkg, ";"); sc > 0 { diff --git a/protoc-gen-grpc-gateway/main.go b/protoc-gen-grpc-gateway/main.go index 917f5cf6d9b..4b875d51b36 100644 --- a/protoc-gen-grpc-gateway/main.go +++ b/protoc-gen-grpc-gateway/main.go @@ -24,6 +24,7 @@ import ( var ( importPrefix = flag.String("import_prefix", "", "prefix to be added to go package paths for imported proto files") + 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.") useRequestContext = flag.Bool("request_context", true, "determine whether to use http.Request's context or not") allowDeleteBody = flag.Bool("allow_delete_body", false, "unless set, HTTP DELETE methods may not have a body") ) @@ -78,6 +79,7 @@ func main() { g := gengateway.New(reg, *useRequestContext) reg.SetPrefix(*importPrefix) + reg.SetImportPath(*importPath) reg.SetAllowDeleteBody(*allowDeleteBody) if err := reg.Load(req); err != nil { emitError(err)