Skip to content

Commit

Permalink
fixes package name override doesn't work (grpc-ecosystem#277)
Browse files Browse the repository at this point in the history
* adds a test case for package overriding

* fixes package name override doesn't work

* sanitizes package name
  • Loading branch information
favadi authored and tamalsaha committed May 31, 2017
1 parent ca4c8d6 commit 8d98ce2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
25 changes: 22 additions & 3 deletions protoc-gen-grpc-gateway/descriptor/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ func (r *Registry) goPackagePath(f *descriptor.FileDescriptorProto) string {
gopkg := f.Options.GetGoPackage()
idx := strings.LastIndex(gopkg, "/")
if idx >= 0 {
if sc := strings.LastIndex(gopkg, ";"); sc > 0 {
gopkg = gopkg[:sc+1-1]
}
return gopkg
}

Expand Down Expand Up @@ -269,11 +272,19 @@ func (r *Registry) SetAllowDeleteBody(allow bool) {
r.allowDeleteBody = allow
}

// sanitizePackageName replaces unallowed character in package name
// with allowed character.
func sanitizePackageName(pkgName string) string {
pkgName = strings.Replace(pkgName, ".", "_", -1)
pkgName = strings.Replace(pkgName, "-", "_", -1)
return pkgName
}

// 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)
return strings.Replace(name, ".", "_", -1)
return sanitizePackageName(name)
}

// packageIdentityName returns the identity of packages.
Expand All @@ -284,10 +295,18 @@ func packageIdentityName(f *descriptor.FileDescriptorProto) string {
gopkg := f.Options.GetGoPackage()
idx := strings.LastIndex(gopkg, "/")
if idx < 0 {
return gopkg
gopkg = gopkg[idx+1:]
}

return gopkg[idx+1:]
gopkg = gopkg[idx+1:]
// package name is overrided with the string after the
// ';' character
sc := strings.IndexByte(gopkg, ';')
if sc < 0 {
return sanitizePackageName(gopkg)

}
return sanitizePackageName(gopkg[sc+1:])
}

if f.Package == nil {
Expand Down
18 changes: 18 additions & 0 deletions protoc-gen-grpc-gateway/descriptor/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,21 @@ func TestLoadWithInconsistentTargetPackage(t *testing.T) {
}
}
}

func TestLoadOverridedPackageName(t *testing.T) {
reg := NewRegistry()
loadFile(t, reg, `
name: 'example.proto'
package: 'example'
options < go_package: 'example.com/xyz;pb' >
`)
file := reg.files["example.proto"]
if file == nil {
t.Errorf("reg.files[%q] = nil; want non-nil", "example.proto")
return
}
wantPkg := GoPackage{Path: "example.com/xyz", Name: "pb"}
if got, want := file.GoPkg, wantPkg; got != want {
t.Errorf("file.GoPkg = %#v; want %#v", got, want)
}
}

0 comments on commit 8d98ce2

Please sign in to comment.