Skip to content

Commit

Permalink
grpc-gateway/generator: respect full package (grpc-ecosystem#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 f59b980 commit e32ac81
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
}
}
}

0 comments on commit e32ac81

Please sign in to comment.