Skip to content

Commit

Permalink
grpc-gateway/generator: respect full package (#462)
Browse files Browse the repository at this point in the history
If you provide a go_package, your wishes will be reflected in the grpc-gateway output.
  • Loading branch information
glerchundi authored and achew22 committed Nov 8, 2017
1 parent 566deb4 commit 93cf3eb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
3 changes: 3 additions & 0 deletions protoc-gen-grpc-gateway/gengateway/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func (g *generator) Generate(targets []*descriptor.File) ([]*plugin.CodeGenerato
return nil, err
}
name := file.GetName()
if file.GoPkg.Path != "" {
name = fmt.Sprintf("%s/%s", file.GoPkg.Path, filepath.Base(name))
}
ext := filepath.Ext(name)
base := strings.TrimSuffix(name, ext)
output := fmt.Sprintf("%s.pb.gw.go", base)
Expand Down
79 changes: 72 additions & 7 deletions protoc-gen-grpc-gateway/gengateway/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gengateway

import (
"path/filepath"
"strings"
"testing"

Expand All @@ -9,7 +10,16 @@ import (
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
)

func TestGenerateServiceWithoutBindings(t *testing.T) {
func newExampleFileDescriptor() *descriptor.File {
return newExampleFileDescriptorWithGoPkg(
&descriptor.GoPackage{
Path: "example.com/path/to/example/example.pb",
Name: "example_pb",
},
)
}

func newExampleFileDescriptorWithGoPkg(gp *descriptor.GoPackage) *descriptor.File {
msgdesc := &protodescriptor.DescriptorProto{
Name: proto.String("ExampleMessage"),
}
Expand Down Expand Up @@ -39,18 +49,15 @@ func TestGenerateServiceWithoutBindings(t *testing.T) {
Name: proto.String("ExampleService"),
Method: []*protodescriptor.MethodDescriptorProto{meth, meth1},
}
file := descriptor.File{
return &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",
},
GoPkg: *gp,
Messages: []*descriptor.Message{msg},
Services: []*descriptor.Service{
{
Expand All @@ -76,8 +83,12 @@ func TestGenerateServiceWithoutBindings(t *testing.T) {
},
},
}
}

func TestGenerateServiceWithoutBindings(t *testing.T) {
file := newExampleFileDescriptor()
g := &generator{}
got, err := g.generate(crossLinkFixture(&file))
got, err := g.generate(crossLinkFixture(file))
if err != nil {
t.Errorf("generate(%#v) failed with %v; want success", file, err)
return
Expand All @@ -86,3 +97,57 @@ func TestGenerateServiceWithoutBindings(t *testing.T) {
t.Errorf("generate(%#v) = %s; does not want to contain %s", file, got, notwanted)
}
}

func TestGenerateOutputPath(t *testing.T) {
cases := []struct {
file *descriptor.File
expected string
}{
{
file: newExampleFileDescriptorWithGoPkg(
&descriptor.GoPackage{
Path: "example.com/path/to/example",
Name: "example_pb",
},
),
expected: "example.com/path/to/example",
},
{
file: newExampleFileDescriptorWithGoPkg(
&descriptor.GoPackage{
Path: "example",
Name: "example_pb",
},
),
expected: "example",
},
}

g := &generator{}
for _, c := range cases {
file := c.file
gots, err := g.Generate([]*descriptor.File{crossLinkFixture(file)})
if err != nil {
t.Errorf("Generate(%#v) failed with %v; wants success", file, err)
return
}

if len(gots) != 1 {
t.Errorf("Generate(%#v) failed; expects on result got %d", file, len(gots))
return
}

got := gots[0]
if got.Name == nil {
t.Errorf("Generate(%#v) failed; expects non-nil Name(%v)", file, got.Name)
return
}

gotPath := filepath.Dir(*got.Name)
expectedPath := c.expected
if gotPath != expectedPath {
t.Errorf("Generate(%#v) failed; got path: %s expected path: %s", file, gotPath, expectedPath)
return
}
}
}

2 comments on commit 93cf3eb

@c4milo
Copy link

@c4milo c4milo commented on 93cf3eb Nov 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glerchundi @achew22 can we make this optional so it is backwards compatible?

@achew22
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you would like to add a flag that defaults to the new behavior and allows you to revert to the old then I would welcome that. Could you please send in a PR?

Please sign in to comment.