Skip to content

Commit

Permalink
Merge pull request #312 from fische/master
Browse files Browse the repository at this point in the history
Do not add imports from methods with no bindings.
  • Loading branch information
tmc committed Feb 19, 2017
2 parents fdf0869 + bb3b7c7 commit b855144
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 96 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Here's the recommended process of contribution.
4. Make sure that your change follows best practices in Go
* [Effective Go](https://golang.org/doc/effective_go.html)
* [Go Code Review Comments](https://golang.org/wiki/CodeReviewComments)
5. Make sure that `make test` passes.
5. Make sure that `make test` passes. (use swagger-codegen 2.1.6, not newer versions)
6. Sign [a Contributor License Agreement](https://cla.developers.google.com/clas)
7. Open a pull request in Github

Expand Down
10 changes: 10 additions & 0 deletions examples/clients/abe/ProtobufDuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package abe

import (
)

type ProtobufDuration struct {
Seconds string `json:"seconds,omitempty"`
Nanos int32 `json:"nanos,omitempty"`

}
181 changes: 92 additions & 89 deletions examples/examplepb/a_bit_of_everything.pb.go

Large diffs are not rendered by default.

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

import "google/api/annotations.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/duration.proto";
import "examples/sub/message.proto";
import "examples/sub2/message.proto";
import "google/protobuf/timestamp.proto";
Expand Down Expand Up @@ -123,7 +124,7 @@ service ABitOfEverythingService {
body: "*"
};
}
rpc NoBindings(google.protobuf.Empty) returns (google.protobuf.Empty) {}
rpc NoBindings(google.protobuf.Duration) returns (google.protobuf.Empty) {}
rpc Timeout(google.protobuf.Empty) returns (google.protobuf.Empty) {
option (google.api.http) = {
get: "/v2/example/timeout",
Expand Down
16 changes: 16 additions & 0 deletions examples/examplepb/a_bit_of_everything.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,22 @@
"default": "ZERO",
"description": "NumericEnum is one or zero.\n\n - ZERO: ZERO means 0\n - ONE: ONE means 1"
},
"protobufDuration": {
"type": "object",
"properties": {
"seconds": {
"type": "string",
"format": "int64",
"description": "Signed seconds of the span of time. Must be from -315,576,000,000\nto +315,576,000,000 inclusive."
},
"nanos": {
"type": "integer",
"format": "int32",
"description": "Signed fractions of a second at nanosecond resolution of the span\nof time. Durations less than one second are represented with a 0\n`seconds` field and a positive or negative `nanos` field. For durations\nof one second or more, a non-zero value for the `nanos` field must be\nof the same sign as the `seconds` field. Must be from -999,999,999\nto +999,999,999 inclusive."
}
},
"description": "A Duration represents a signed, fixed-length span of time represented\nas a count of seconds and fractions of seconds at nanosecond\nresolution. It is independent of any calendar and concepts like \"day\"\nor \"month\". It is related to Timestamp in that the difference between\ntwo Timestamp values is a Duration and it can be added or subtracted\nfrom a Timestamp. Range is approximately +-10,000 years.\n\nExample 1: Compute Duration from two Timestamps in pseudo code.\n\n Timestamp start = ...;\n Timestamp end = ...;\n Duration duration = ...;\n\n duration.seconds = end.seconds - start.seconds;\n duration.nanos = end.nanos - start.nanos;\n\n if (duration.seconds \u003c 0 \u0026\u0026 duration.nanos \u003e 0) {\n duration.seconds += 1;\n duration.nanos -= 1000000000;\n } else if (durations.seconds \u003e 0 \u0026\u0026 duration.nanos \u003c 0) {\n duration.seconds -= 1;\n duration.nanos += 1000000000;\n }\n\nExample 2: Compute Timestamp from Timestamp + Duration in pseudo code.\n\n Timestamp start = ...;\n Duration duration = ...;\n Timestamp end = ...;\n\n end.seconds = start.seconds + duration.seconds;\n end.nanos = start.nanos + duration.nanos;\n\n if (end.nanos \u003c 0) {\n end.seconds -= 1;\n end.nanos += 1000000000;\n } else if (end.nanos \u003e= 1000000000) {\n end.seconds += 1;\n end.nanos -= 1000000000;\n }\n\nExample 3: Compute Duration from datetime.timedelta in Python.\n\n td = datetime.timedelta(days=3, minutes=10)\n duration = Duration()\n duration.FromTimedelta(td)"
},
"protobufEmpty": {
"type": "object",
"description": "service Foo {\n rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty);\n }\n\nThe JSON representation for `Empty` is empty JSON object `{}`.",
Expand Down
3 changes: 2 additions & 1 deletion examples/server/a_bit_of_everything.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"

"github.com/golang/glog"
"github.com/golang/protobuf/ptypes/duration"
"github.com/golang/protobuf/ptypes/empty"
examples "github.com/grpc-ecosystem/grpc-gateway/examples/examplepb"
sub "github.com/grpc-ecosystem/grpc-gateway/examples/sub"
Expand Down Expand Up @@ -234,7 +235,7 @@ func (s *_ABitOfEverythingServer) DeepPathEcho(ctx context.Context, msg *example
return msg, nil
}

func (s *_ABitOfEverythingServer) NoBindings(ctx context.Context, msg *empty.Empty) (*empty.Empty, error) {
func (s *_ABitOfEverythingServer) NoBindings(ctx context.Context, msg *duration.Duration) (*empty.Empty, error) {
return nil, nil
}

Expand Down
7 changes: 3 additions & 4 deletions protoc-gen-grpc-gateway/gengateway/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
gen "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/generator"
options "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"
)

var (
Expand Down Expand Up @@ -98,10 +99,8 @@ func (g *generator) generate(file *descriptor.File) (string, error) {
for _, svc := range file.Services {
for _, m := range svc.Methods {
pkg := m.RequestType.File.GoPkg
if pkg == file.GoPkg {
continue
}
if pkgSeen[pkg.Path] {
if m.Options == nil || !proto.HasExtension(m.Options, options.E_Http) ||
pkg == file.GoPkg || pkgSeen[pkg.Path] {
continue
}
pkgSeen[pkg.Path] = true
Expand Down
88 changes: 88 additions & 0 deletions protoc-gen-grpc-gateway/gengateway/generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package gengateway

import (
"strings"
"testing"

"github.com/golang/protobuf/proto"
protodescriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
)

func TestGenerateServiceWithoutBindings(t *testing.T) {
msgdesc := &protodescriptor.DescriptorProto{
Name: proto.String("ExampleMessage"),
}
msg := &descriptor.Message{
DescriptorProto: msgdesc,
}
msg1 := &descriptor.Message{
DescriptorProto: msgdesc,
File: &descriptor.File{
GoPkg: descriptor.GoPackage{
Path: "github.com/golang/protobuf/ptypes/empty",
Name: "empty",
},
},
}
meth := &protodescriptor.MethodDescriptorProto{
Name: proto.String("Example"),
InputType: proto.String("ExampleMessage"),
OutputType: proto.String("ExampleMessage"),
}
meth1 := &protodescriptor.MethodDescriptorProto{
Name: proto.String("ExampleWithoutBindings"),
InputType: proto.String("empty.Empty"),
OutputType: proto.String("empty.Empty"),
}
svc := &protodescriptor.ServiceDescriptorProto{
Name: proto.String("ExampleService"),
Method: []*protodescriptor.MethodDescriptorProto{meth, meth1},
}
file := descriptor.File{
FileDescriptorProto: &protodescriptor.FileDescriptorProto{
Name: proto.String("example.proto"),
Package: proto.String("example"),
Dependency: []string{"a.example/b/c.proto", "a.example/d/e.proto"},
MessageType: []*protodescriptor.DescriptorProto{msgdesc},
Service: []*protodescriptor.ServiceDescriptorProto{svc},
},
GoPkg: descriptor.GoPackage{
Path: "example.com/path/to/example/example.pb",
Name: "example_pb",
},
Messages: []*descriptor.Message{msg},
Services: []*descriptor.Service{
{
ServiceDescriptorProto: svc,
Methods: []*descriptor.Method{
{
MethodDescriptorProto: meth,
RequestType: msg,
ResponseType: msg,
Bindings: []*descriptor.Binding{
{
HTTPMethod: "GET",
Body: &descriptor.Body{FieldPath: nil},
},
},
},
{
MethodDescriptorProto: meth1,
RequestType: msg1,
ResponseType: msg1,
},
},
},
},
}
g := &generator{}
got, err := g.generate(crossLinkFixture(&file))
if err != nil {
t.Errorf("generate(%#v) failed with %v; want success", file, err)
return
}
if notwanted := `"github.com/golang/protobuf/ptypes/empty"`; strings.Contains(got, notwanted) {
t.Errorf("generate(%#v) = %s; does not want to contain %s", file, got, notwanted)
}
}

0 comments on commit b855144

Please sign in to comment.