Skip to content

Commit

Permalink
sanitizes package name
Browse files Browse the repository at this point in the history
  • Loading branch information
favadi committed Dec 8, 2016
1 parent dc8da2f commit cadaaec
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions protoc-gen-grpc-gateway/descriptor/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,19 @@ func (r *Registry) GetAllFQENs() []string {
return keys
}

// 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 @@ -275,17 +283,18 @@ func packageIdentityName(f *descriptor.FileDescriptorProto) string {
gopkg := f.Options.GetGoPackage()
idx := strings.LastIndex(gopkg, "/")
if idx < 0 {
return gopkg
return sanitizePackageName(gopkg)
}

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

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

if f.Package == nil {
Expand Down

0 comments on commit cadaaec

Please sign in to comment.