Created
November 25, 2013 07:29
-
-
Save wuman/7637605 to your computer and use it in GitHub Desktop.
Patch to goprotobuf for issue #32 (https://code.google.com/p/goprotobuf/issues/detail?id=32).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -r 61664b8425f3 protoc-gen-go/descriptor/descriptor.pb.go | |
--- a/protoc-gen-go/descriptor/descriptor.pb.go Fri Oct 11 10:08:35 2013 +1100 | |
+++ b/protoc-gen-go/descriptor/descriptor.pb.go Sat Nov 23 23:39:44 2013 +0800 | |
@@ -682,7 +682,8 @@ | |
OptimizeFor *FileOptions_OptimizeMode `protobuf:"varint,9,opt,name=optimize_for,enum=google.protobuf.FileOptions_OptimizeMode,def=1" json:"optimize_for,omitempty"` | |
// Sets the Go package where structs generated from this .proto will be | |
// placed. There is no default. | |
- GoPackage *string `protobuf:"bytes,11,opt,name=go_package" json:"go_package,omitempty"` | |
+ GoPackage *string `protobuf:"bytes,11,opt,name=go_package" json:"go_package,omitempty"` | |
+ GoImportPath *string `protobuf:"bytes,12,opt,name=go_import_path" json:"go_import_path,omitempty"` | |
// Should generic services be generated in each language? "Generic" services | |
// are not specific to any particular RPC system. They are generated by the | |
// main code generators in each language (without additional plugins). | |
@@ -769,6 +770,13 @@ | |
return "" | |
} | |
+func (m *FileOptions) GetGoImportPath() string { | |
+ if m != nil && m.GoImportPath != nil { | |
+ return *m.GoImportPath | |
+ } | |
+ return "" | |
+} | |
+ | |
func (m *FileOptions) GetCcGenericServices() bool { | |
if m != nil && m.CcGenericServices != nil { | |
return *m.CcGenericServices | |
diff -r 61664b8425f3 protoc-gen-go/generator/generator.go | |
--- a/protoc-gen-go/generator/generator.go Fri Oct 11 10:08:35 2013 +1100 | |
+++ b/protoc-gen-go/generator/generator.go Sat Nov 23 23:39:44 2013 +0800 | |
@@ -45,6 +45,7 @@ | |
"log" | |
"os" | |
"path" | |
+ "reflect" | |
"strconv" | |
"strings" | |
"unicode" | |
@@ -251,10 +252,16 @@ | |
// or the input file name. | |
func (d *FileDescriptor) goPackageName() (name string, explicit bool) { | |
// Does the file have a "go_package" option? | |
- if opts := d.Options; opts != nil { | |
+ opts := d.Options | |
+ if opts != nil { | |
if pkg := opts.GetGoPackage(); pkg != "" { | |
return pkg, true | |
} | |
+ if p := opts.GetGoImportPath(); p != "" { | |
+ if i := strings.LastIndex(p, "/"); i >= 0 { | |
+ return p[i+1:], true | |
+ } | |
+ } | |
} | |
// Does the file have a package clause? | |
@@ -491,9 +498,24 @@ | |
if pkg == g.packageName { | |
return "" | |
} | |
+ opts := obj.File().Options | |
+ | |
+ if !isNilInterface(opts) && !isNilInterface(opts.GoImportPath) && | |
+ !isNilInterface(g.file) && !isNilInterface(g.file.Options) && | |
+ !isNilInterface(g.file.Options.GoImportPath) && | |
+ *opts.GoImportPath == *g.file.Options.GoImportPath { | |
+ return "" | |
+ } | |
return pkg + "." | |
} | |
+// isNilInterface is a helper function that checks for the nullity of an interface. | |
+// An interface with type non-nil and value nil is still considered nil. | |
+// Refer to http://golang.org/doc/faq#nil_error for more explanation. | |
+func isNilInterface(v interface{}) bool { | |
+ return v == nil || !reflect.ValueOf(v).Elem().IsValid() | |
+} | |
+ | |
// For each input file, the unique package name to use, underscored. | |
var uniquePackageName = make(map[*descriptor.FileDescriptorProto]string) | |
@@ -1096,7 +1118,14 @@ | |
continue | |
} | |
if _, ok := g.usedPackages[fd.PackageName()]; ok { | |
- g.P("import ", fd.PackageName(), " ", strconv.Quote(filename)) | |
+ if opts := fd.Options; !isNilInterface(opts) && !isNilInterface(opts.GoImportPath) { | |
+ if !isNilInterface(g.file.Options) && | |
+ (isNilInterface(g.file.Options.GoImportPath) || *g.file.Options.GoImportPath != *opts.GoImportPath) { | |
+ g.P("import ", fd.PackageName(), " ", strconv.Quote(*opts.GoImportPath)) | |
+ } | |
+ } else { | |
+ g.P("import ", fd.PackageName(), " ", strconv.Quote(filename)) | |
+ } | |
} else { | |
// TODO: Re-enable this when we are more feature-complete. | |
// For instance, some protos use foreign field extensions, which we don't support. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment