Skip to content

Commit

Permalink
Merge pull request #48 from gengo/feature/package-inconsistency
Browse files Browse the repository at this point in the history
Follow a change of go package name convention in protoc-gen-go
  • Loading branch information
yugui committed Sep 2, 2015
2 parents 54c3357 + f4d3eed commit 9078b50
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 40 deletions.
34 changes: 26 additions & 8 deletions protoc-gen-grpc-gateway/descriptor/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,22 @@ func (r *Registry) Load(req *plugin.CodeGeneratorRequest) error {
for _, file := range req.GetProtoFile() {
r.loadFile(file)
}
for _, target := range req.FileToGenerate {

var targetPkg string
for _, name := range req.FileToGenerate {
target := r.files[name]
if target == nil {
return fmt.Errorf("no such file: %s", name)
}
name := packageIdentityName(target.FileDescriptorProto)
if targetPkg == "" {
targetPkg = name
} else {
if targetPkg != name {
return fmt.Errorf("inconsistent package names: %s %s", targetPkg, name)
}
}

if err := r.loadServices(target); err != nil {
return err
}
Expand Down Expand Up @@ -170,17 +185,20 @@ func (r *Registry) goPackagePath(f *descriptor.FileDescriptorProto) string {
if pkg, ok := r.pkgMap[name]; ok {
return path.Join(r.prefix, pkg)
}

ext := filepath.Ext(name)
if ext == ".protodevel" || ext == ".proto" {
name = strings.TrimSuffix(name, ext)
}
return path.Join(r.prefix, fmt.Sprintf("%s.pb", name))
return path.Join(r.prefix, path.Dir(name))
}

// 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)
}

// 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 {
if f.Options != nil && f.Options.GoPackage != nil {
return f.Options.GetGoPackage()
}
Expand All @@ -190,5 +208,5 @@ func defaultGoPackageName(f *descriptor.FileDescriptorProto) string {
ext := filepath.Ext(base)
return strings.TrimSuffix(base, ext)
}
return strings.Replace(f.GetPackage(), ".", "_", -1)
return f.GetPackage()
}
Loading

0 comments on commit 9078b50

Please sign in to comment.