Skip to content

Commit

Permalink
Add support for --Import_path (grpc-ecosystem#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
achew22 authored Dec 20, 2017
1 parent cd6ac50 commit 6a715f6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions protoc-gen-grpc-gateway/descriptor/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions protoc-gen-grpc-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6a715f6

Please sign in to comment.