Skip to content

Commit e32ac81

Browse files
glerchundiachew22
authored andcommitted
grpc-gateway/generator: respect full package (grpc-ecosystem#462)
If you provide a go_package, your wishes will be reflected in the grpc-gateway output.
1 parent f59b980 commit e32ac81

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

protoc-gen-grpc-gateway/gengateway/generator.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func (g *generator) Generate(targets []*descriptor.File) ([]*plugin.CodeGenerato
7878
return nil, err
7979
}
8080
name := file.GetName()
81+
if file.GoPkg.Path != "" {
82+
name = fmt.Sprintf("%s/%s", file.GoPkg.Path, filepath.Base(name))
83+
}
8184
ext := filepath.Ext(name)
8285
base := strings.TrimSuffix(name, ext)
8386
output := fmt.Sprintf("%s.pb.gw.go", base)

protoc-gen-grpc-gateway/gengateway/generator_test.go

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gengateway
22

33
import (
4+
"path/filepath"
45
"strings"
56
"testing"
67

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

12-
func TestGenerateServiceWithoutBindings(t *testing.T) {
13+
func newExampleFileDescriptor() *descriptor.File {
14+
return newExampleFileDescriptorWithGoPkg(
15+
&descriptor.GoPackage{
16+
Path: "example.com/path/to/example/example.pb",
17+
Name: "example_pb",
18+
},
19+
)
20+
}
21+
22+
func newExampleFileDescriptorWithGoPkg(gp *descriptor.GoPackage) *descriptor.File {
1323
msgdesc := &protodescriptor.DescriptorProto{
1424
Name: proto.String("ExampleMessage"),
1525
}
@@ -39,18 +49,15 @@ func TestGenerateServiceWithoutBindings(t *testing.T) {
3949
Name: proto.String("ExampleService"),
4050
Method: []*protodescriptor.MethodDescriptorProto{meth, meth1},
4151
}
42-
file := descriptor.File{
52+
return &descriptor.File{
4353
FileDescriptorProto: &protodescriptor.FileDescriptorProto{
4454
Name: proto.String("example.proto"),
4555
Package: proto.String("example"),
4656
Dependency: []string{"a.example/b/c.proto", "a.example/d/e.proto"},
4757
MessageType: []*protodescriptor.DescriptorProto{msgdesc},
4858
Service: []*protodescriptor.ServiceDescriptorProto{svc},
4959
},
50-
GoPkg: descriptor.GoPackage{
51-
Path: "example.com/path/to/example/example.pb",
52-
Name: "example_pb",
53-
},
60+
GoPkg: *gp,
5461
Messages: []*descriptor.Message{msg},
5562
Services: []*descriptor.Service{
5663
{
@@ -76,8 +83,12 @@ func TestGenerateServiceWithoutBindings(t *testing.T) {
7683
},
7784
},
7885
}
86+
}
87+
88+
func TestGenerateServiceWithoutBindings(t *testing.T) {
89+
file := newExampleFileDescriptor()
7990
g := &generator{}
80-
got, err := g.generate(crossLinkFixture(&file))
91+
got, err := g.generate(crossLinkFixture(file))
8192
if err != nil {
8293
t.Errorf("generate(%#v) failed with %v; want success", file, err)
8394
return
@@ -86,3 +97,57 @@ func TestGenerateServiceWithoutBindings(t *testing.T) {
8697
t.Errorf("generate(%#v) = %s; does not want to contain %s", file, got, notwanted)
8798
}
8899
}
100+
101+
func TestGenerateOutputPath(t *testing.T) {
102+
cases := []struct {
103+
file *descriptor.File
104+
expected string
105+
}{
106+
{
107+
file: newExampleFileDescriptorWithGoPkg(
108+
&descriptor.GoPackage{
109+
Path: "example.com/path/to/example",
110+
Name: "example_pb",
111+
},
112+
),
113+
expected: "example.com/path/to/example",
114+
},
115+
{
116+
file: newExampleFileDescriptorWithGoPkg(
117+
&descriptor.GoPackage{
118+
Path: "example",
119+
Name: "example_pb",
120+
},
121+
),
122+
expected: "example",
123+
},
124+
}
125+
126+
g := &generator{}
127+
for _, c := range cases {
128+
file := c.file
129+
gots, err := g.Generate([]*descriptor.File{crossLinkFixture(file)})
130+
if err != nil {
131+
t.Errorf("Generate(%#v) failed with %v; wants success", file, err)
132+
return
133+
}
134+
135+
if len(gots) != 1 {
136+
t.Errorf("Generate(%#v) failed; expects on result got %d", file, len(gots))
137+
return
138+
}
139+
140+
got := gots[0]
141+
if got.Name == nil {
142+
t.Errorf("Generate(%#v) failed; expects non-nil Name(%v)", file, got.Name)
143+
return
144+
}
145+
146+
gotPath := filepath.Dir(*got.Name)
147+
expectedPath := c.expected
148+
if gotPath != expectedPath {
149+
t.Errorf("Generate(%#v) failed; got path: %s expected path: %s", file, gotPath, expectedPath)
150+
return
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)