Skip to content

Commit

Permalink
fix(gengapic): only gen REST client for RESTable services (#1199)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdietz authored Dec 7, 2022
1 parent f606efd commit 0f063f1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/gengapic/gengapic.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func Gen(genReq *plugin.CodeGeneratorRequest) (*plugin.CodeGeneratorResponse, er
outFile = filepath.Join(g.opts.outDir, outFile)

g.reset()
// If the service has no REST-able RPCs, then a REGAPIC should not be
// generated for it, even if REST is an enabled transport.
transports := g.opts.transports
hasREST := hasRESTMethod(s)
if !hasREST {
g.opts.transports = []transport{grpc}
}
if err := g.gen(s); err != nil {
return &g.resp, err
}
Expand All @@ -112,6 +119,12 @@ func Gen(genReq *plugin.CodeGeneratorRequest) (*plugin.CodeGeneratorResponse, er
}
g.imports[pbinfo.ImportSpec{Name: g.opts.pkgName, Path: g.opts.pkgPath}] = true
g.commit(outFile+"_client_example_test.go", g.opts.pkgName+"_test")

// Replace original set of transports for the next service that may have
// REST-able RPCs.
if !hasREST {
g.opts.transports = transports
}
}

g.reset()
Expand Down
13 changes: 13 additions & 0 deletions internal/gengapic/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ func hasMethod(service *descriptor.ServiceDescriptorProto, method string) bool {
return false
}

// hasRESTMethod reports if there is at least one RPC on the Service that
// has a gRPC-HTTP transcoding, or REST, annotation on it.
func hasRESTMethod(service *descriptor.ServiceDescriptorProto) bool {
for _, m := range service.GetMethod() {
eHTTP := proto.GetExtension(m.GetOptions(), annotations.E_Http)
if h := eHTTP.(*annotations.HttpRule); h.GetPattern() != nil {
return true
}
}

return false
}

// getMethod returns the MethodDescriptorProto for the given service RPC and simple method name.
func getMethod(service *descriptor.ServiceDescriptorProto, method string) *descriptor.MethodDescriptorProto {
for _, m := range service.GetMethod() {
Expand Down
23 changes: 23 additions & 0 deletions internal/gengapic/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,26 @@ func TestGetHeaderName(t *testing.T) {
}
}
}

func TestHasRESTMethod(t *testing.T) {
for _, tst := range []struct {
name string
want bool
}{
{"has_rest_method", true},
{"does_not_have_rest_method", false},
} {
opts := &descriptor.MethodOptions{}
if tst.want {
proto.SetExtension(opts, annotations.E_Http, &annotations.HttpRule{Pattern: &annotations.HttpRule_Get{Get: "/foo"}})
}
s := &descriptor.ServiceDescriptorProto{
Method: []*descriptor.MethodDescriptorProto{
{Options: opts},
},
}
if got := hasRESTMethod(s); got != tst.want {
t.Fatalf("%s: expected %v, got %v", tst.name, tst.want, got)
}
}
}
3 changes: 3 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ generate --go_gapic_opt 'go-gapic-package=cloud.google.com/go/storage/internal/a
echo "Generating Cloud Retail v2"
generate --go_gapic_opt 'go-gapic-package=cloud.google.com/go/retail/apiv2;retail,transport=rest' $GOOGLEAPIS/google/cloud/retail/v2/*.proto

echo "Generating Apigee Connect v1 - Dual Transport, partial REGAPIC"
generate --go_gapic_opt 'go-gapic-package=cloud.google.com/go/apigeeconnect/apiv1;apigeeconnect,transport=grpc+rest' $GOOGLEAPIS/google/cloud/apigeeconnect/v1/*.proto

echo "Generation complete"

echo "Running gofmt to check for syntax errors"
Expand Down

0 comments on commit 0f063f1

Please sign in to comment.