Skip to content

Commit

Permalink
grpc-ecosystem#536: Using importPath to set package name rather than …
Browse files Browse the repository at this point in the history
…package path.
  • Loading branch information
rwl committed Feb 4, 2018
1 parent 6658b3a commit a5f2f2e
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions protoc-gen-grpc-gateway/descriptor/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *Registry) Load(req *plugin.CodeGeneratorRequest) error {
if target == nil {
return fmt.Errorf("no such file: %s", name)
}
name := packageIdentityName(target.FileDescriptorProto)
name := r.packageIdentityName(target.FileDescriptorProto)
if targetPkg == "" {
targetPkg = name
} else {
Expand All @@ -83,7 +83,7 @@ func (r *Registry) Load(req *plugin.CodeGeneratorRequest) error {
func (r *Registry) loadFile(file *descriptor.FileDescriptorProto) {
pkg := GoPackage{
Path: r.goPackagePath(file),
Name: defaultGoPackageName(file),
Name: r.defaultGoPackageName(file),
}
if err := r.ReserveGoPackageAlias(pkg.Name, pkg.Path); err != nil {
for i := 0; ; i++ {
Expand Down Expand Up @@ -247,9 +247,6 @@ 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 Expand Up @@ -295,15 +292,21 @@ func sanitizePackageName(pkgName string) string {

// defaultGoPackageName returns the default go package name to be used for go files generated from "f".
// You might need to use an unique alias for the package when you import it. Use ReserveGoPackageAlias to get a unique alias.
func defaultGoPackageName(f *descriptor.FileDescriptorProto) string {
name := packageIdentityName(f)
func (r *Registry) defaultGoPackageName(f *descriptor.FileDescriptorProto) string {
name := r.packageIdentityName(f)
return sanitizePackageName(name)
}

// packageIdentityName returns the identity of packages.
// protoc-gen-grpc-gateway rejects CodeGenerationRequests which contains more than one packages
// as protoc-gen-go does.
func packageIdentityName(f *descriptor.FileDescriptorProto) string {
func (r *Registry) packageIdentityName(f *descriptor.FileDescriptorProto) string {
if p := r.importPath; len(p) != 0 {
if i := strings.LastIndex(p, "/"); i >= 0 {
p = p[i+1:]
}
return p
}
if f.Options != nil && f.Options.GoPackage != nil {
gopkg := f.Options.GetGoPackage()
idx := strings.LastIndex(gopkg, "/")
Expand Down

0 comments on commit a5f2f2e

Please sign in to comment.