Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc-gateway/generator: respect full package #462

Merged
merged 1 commit into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't the file name end in .go?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so.

I migrated an already existing example into a customisable function without doing any change to it but as you can observe in the two cases that I've implemented i used this descriptor as a 1-1 representation of the go_package proto option.

I tried to touch as less as possible :)

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
}
}
}