Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes package name override doesn't work #277

Merged
merged 3 commits into from
May 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}