Skip to content

Commit

Permalink
Merge pull request grpc-ecosystem#129 from kazegusuri/respect-go-pack…
Browse files Browse the repository at this point in the history
…age-option

go_package option as go import path
  • Loading branch information
yugui committed Apr 18, 2016
2 parents d5f85e7 + 3b44866 commit c080463
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_output/
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ GATEWAY_PLUGIN_SRC= utilities/doc.go \
GOOGLEAPIS_DIR=third_party/googleapis
OPTIONS_PROTO=$(GOOGLEAPIS_DIR)/google/api/annotations.proto $(GOOGLEAPIS_DIR)/google/api/http.proto
OPTIONS_GO=$(OPTIONS_PROTO:.proto=.pb.go)
OUTPUT_DIR=_output

PKGMAP=Mgoogle/protobuf/descriptor.proto=$(GO_PLUGIN_PKG)/descriptor,Mgoogle/api/annotations.proto=$(PKG)/$(GOOGLEAPIS_DIR)/google/api,Mexamples/sub/message.proto=$(PKG)/examples/sub
SWAGGER_EXAMPLES=examples/examplepb/echo_service.proto \
Expand All @@ -49,7 +50,7 @@ EXAMPLES=examples/examplepb/echo_service.proto \
EXAMPLE_SVCSRCS=$(EXAMPLES:.proto=.pb.go)
EXAMPLE_GWSRCS=$(EXAMPLES:.proto=.pb.gw.go)
EXAMPLE_SWAGGERSRCS=$(EXAMPLES:.proto=.swagger.json)
EXAMPLE_DEPS=examples/sub/message.proto
EXAMPLE_DEPS=examples/sub/message.proto examples/sub2/message.proto
EXAMPLE_DEPSRCS=$(EXAMPLE_DEPS:.proto=.pb.go)
PROTOC_INC_PATH=$(dir $(shell which protoc))/../include

Expand All @@ -73,7 +74,9 @@ $(SWAGGER_PLUGIN): $(OPTIONS_GO) $(SWAGGER_PLUGIN_SRC)
$(EXAMPLE_SVCSRCS): $(GO_PLUGIN) $(EXAMPLES)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --go_out=$(PKGMAP),plugins=grpc:. $(EXAMPLES)
$(EXAMPLE_DEPSRCS): $(GO_PLUGIN) $(EXAMPLE_DEPS)
protoc -I $(PROTOC_INC_PATH) -I. --plugin=$(GO_PLUGIN) --go_out=$(PKGMAP),plugins=grpc:. $(EXAMPLE_DEPS)
mkdir -p $(OUTPUT_DIR)
protoc -I $(PROTOC_INC_PATH) -I. --plugin=$(GO_PLUGIN) --go_out=$(PKGMAP),plugins=grpc:$(OUTPUT_DIR) $(@:.pb.go=.proto)
cp $(OUTPUT_DIR)/$(PKG)/$@ $@ || cp $(OUTPUT_DIR)/$@ $@
$(EXAMPLE_GWSRCS): $(GATEWAY_PLUGIN) $(EXAMPLES)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GATEWAY_PLUGIN) --grpc-gateway_out=logtostderr=true,$(PKGMAP):. $(EXAMPLES)
$(EXAMPLE_SWAGGERSRCS): $(SWAGGER_PLUGIN) $(SWAGGER_EXAMPLES)
Expand Down
145 changes: 68 additions & 77 deletions examples/examplepb/a_bit_of_everything.pb.go

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions examples/examplepb/a_bit_of_everything.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions examples/examplepb/a_bit_of_everything.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package gengo.grpc.gateway.examples.examplepb;

import "google/api/annotations.proto";
import "examples/sub/message.proto";
import "examples/sub2/message.proto";

message ABitOfEverything {
message Nested {
Expand Down Expand Up @@ -40,10 +41,6 @@ message ABitOfEverything {
message EmptyMessage {
}

message IdMessage {
string uuid = 1;
}

enum NumericEnum {
ZERO = 0;
ONE = 1;
Expand All @@ -68,7 +65,7 @@ service ABitOfEverythingService {
body: "*"
};
}
rpc Lookup(IdMessage) returns (ABitOfEverything) {
rpc Lookup(sub2.IdMessage) returns (ABitOfEverything) {
option (google.api.http) = {
get: "/v1/example/a_bit_of_everything/{uuid}"
};
Expand All @@ -84,7 +81,7 @@ service ABitOfEverythingService {
body: "*"
};
}
rpc Delete(IdMessage) returns (EmptyMessage) {
rpc Delete(sub2.IdMessage) returns (EmptyMessage) {
option (google.api.http) = {
delete: "/v1/example/a_bit_of_everything/{uuid}"
};
Expand Down
1 change: 0 additions & 1 deletion examples/examplepb/echo_service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions examples/server/a_bit_of_everything.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

examples "github.com/gengo/grpc-gateway/examples/examplepb"
sub "github.com/gengo/grpc-gateway/examples/sub"
sub2 "github.com/gengo/grpc-gateway/examples/sub2"
"github.com/golang/glog"
"github.com/rogpeppe/fastuuid"
"golang.org/x/net/context"
Expand Down Expand Up @@ -84,7 +85,7 @@ func (s *_ABitOfEverythingServer) BulkCreate(stream examples.ABitOfEverythingSer
return stream.SendAndClose(new(examples.EmptyMessage))
}

func (s *_ABitOfEverythingServer) Lookup(ctx context.Context, msg *examples.IdMessage) (*examples.ABitOfEverything, error) {
func (s *_ABitOfEverythingServer) Lookup(ctx context.Context, msg *sub2.IdMessage) (*examples.ABitOfEverything, error) {
s.m.Lock()
defer s.m.Unlock()
glog.Info(msg)
Expand Down Expand Up @@ -150,7 +151,7 @@ func (s *_ABitOfEverythingServer) Update(ctx context.Context, msg *examples.ABit
return new(examples.EmptyMessage), nil
}

func (s *_ABitOfEverythingServer) Delete(ctx context.Context, msg *examples.IdMessage) (*examples.EmptyMessage, error) {
func (s *_ABitOfEverythingServer) Delete(ctx context.Context, msg *sub2.IdMessage) (*examples.EmptyMessage, error) {
s.m.Lock()
defer s.m.Unlock()

Expand Down
52 changes: 52 additions & 0 deletions examples/sub2/message.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/sub2/message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";
option go_package = "github.com/gengo/grpc-gateway/examples/sub2";
package sub2;

message IdMessage {
string uuid = 1;
}
19 changes: 16 additions & 3 deletions protoc-gen-grpc-gateway/descriptor/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,20 @@ func (r *Registry) ReserveGoPackageAlias(alias, pkgpath string) error {
}

// goPackagePath returns the go package path which go files generated from "f" should have.
// It respects the mapping registered by AddPkgMap if exists. Or it generates a path from
// the file name of "f" if otherwise.
// It respects the mapping registered by AddPkgMap if exists. Or use go_package as import path
// if it includes a slash, Otherwide, it generates a path from the file name of "f".
func (r *Registry) goPackagePath(f *descriptor.FileDescriptorProto) string {
name := f.GetName()
if pkg, ok := r.pkgMap[name]; ok {
return path.Join(r.prefix, pkg)
}

gopkg := f.Options.GetGoPackage()
idx := strings.LastIndex(gopkg, "/")
if idx >= 0 {
return gopkg
}

return path.Join(r.prefix, path.Dir(name))
}

Expand Down Expand Up @@ -263,7 +270,13 @@ func defaultGoPackageName(f *descriptor.FileDescriptorProto) string {
// as protoc-gen-go does.
func packageIdentityName(f *descriptor.FileDescriptorProto) string {
if f.Options != nil && f.Options.GoPackage != nil {
return f.Options.GetGoPackage()
gopkg := f.Options.GetGoPackage()
idx := strings.LastIndex(gopkg, "/")
if idx < 0 {
return gopkg
}

return gopkg[idx+1:]
}

if f.Package == nil {
Expand Down

0 comments on commit c080463

Please sign in to comment.