Skip to content

Commit

Permalink
feat(gengapic): diregapic lro examples use wrapper (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdietz authored Feb 1, 2022
1 parent dffe3ec commit ccaa033
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 49 deletions.
5 changes: 3 additions & 2 deletions internal/gengapic/custom_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func (g *generator) isCustomOp(m *descriptor.MethodDescriptorProto, info *httpIn
return g.opts.diregapic && // Generator in DIREGAPIC mode.
g.aux.customOp != nil && // API Defines a custom operation.
m.GetOutputType() == g.customOpProtoName() && // Method returns the custom operation.
info.verb != "get" && // Method is not a GET (polling methods).
m.GetName() != "Wait" // Method is not a Wait (uses POST).
m.GetName() != "Wait" && // Method is not a Wait (uses POST).
info != nil && // Must have google.api.http.
info.verb != "get" // Method is not a GET (polling methods).
}

// customOpProtoName builds the fully-qualified proto name for the custom
Expand Down
6 changes: 4 additions & 2 deletions internal/gengapic/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func (g *generator) exampleMethodBody(pkgName, servName string, m *descriptor.Me
return err
}

httpInfo := getHTTPInfo(m)

g.imports[inSpec] = true
// Pick the first transport for simplicity. We don't need examples
// of each method for both transports when they have the same surface.
Expand All @@ -129,7 +131,7 @@ func (g *generator) exampleMethodBody(pkgName, servName string, m *descriptor.Me
}
if pf != nil {
g.examplePagingCall(m)
} else if g.isLRO(m) {
} else if g.isLRO(m) || g.isCustomOp(m, httpInfo) {
g.exampleLROCall(m)
} else if *m.OutputType == emptyType {
g.exampleEmptyCall(m)
Expand All @@ -149,7 +151,7 @@ func (g *generator) exampleLROCall(m *descriptor.MethodDescriptorProto) {
// if response_type is google.protobuf.Empty, don't generate a "resp" var
eLRO := proto.GetExtension(m.Options, longrunning.E_OperationInfo)
opInfo := eLRO.(*longrunning.OperationInfo)
if opInfo.GetResponseType() == emptyValue {
if opInfo.GetResponseType() == emptyValue || opInfo == nil {
// no new variables when this is used
// therefore don't attempt to delcare it
retVars = "err ="
Expand Down
91 changes: 70 additions & 21 deletions internal/gengapic/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/googleapis/gapic-generator-go/internal/pbinfo"
"github.com/googleapis/gapic-generator-go/internal/txtdiff"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/genproto/googleapis/api/serviceconfig"
"google.golang.org/genproto/googleapis/longrunning"
"google.golang.org/protobuf/proto"
Expand All @@ -30,13 +31,14 @@ import (

func TestExample(t *testing.T) {
var g generator
g.opts = &options{}
g.imports = map[pbinfo.ImportSpec]bool{}
g.mixins = mixins{
g.aux = &auxTypes{}
mix := mixins{
"google.longrunning.Operations": operationsMethods(),
"google.cloud.location.Locations": locationMethods(),
"google.iam.v1.IAMPolicy": iamPolicyMethods(),
}
g.mixins = mix
g.serviceConfig = &serviceconfig.Service{
Apis: []*apipb.Api{
{Name: "foo.bar.Baz"},
Expand Down Expand Up @@ -84,10 +86,15 @@ func TestExample(t *testing.T) {
},
}

cop := &descriptor.DescriptorProto{
Name: proto.String("Operation"),
}

file := &descriptor.FileDescriptorProto{
Options: &descriptor.FileOptions{
GoPackage: proto.String("mypackage"),
},
Package: proto.String("my.pkg"),
}

emptyLRO := &longrunning.OperationInfo{
Expand All @@ -102,11 +109,18 @@ func TestExample(t *testing.T) {
respLROOpts := &descriptor.MethodOptions{}
proto.SetExtension(respLROOpts, longrunning.E_OperationInfo, respLRO)

customOpOpts := &descriptor.MethodOptions{}
proto.SetExtension(customOpOpts, annotations.E_Http, &annotations.HttpRule{
Pattern: &annotations.HttpRule_Post{
Post: "/customOp",
},
})

commonTypes(&g)
for _, typ := range []*descriptor.DescriptorProto{
inputType, outputType, pageInputType, pageOutputType,
inputType, outputType, pageInputType, pageOutputType, cop,
} {
g.descInfo.Type[".my.pkg."+*typ.Name] = typ
g.descInfo.Type[".my.pkg."+typ.GetName()] = typ
g.descInfo.ParentFile[typ] = file
}

Expand All @@ -127,6 +141,7 @@ func TestExample(t *testing.T) {
Name: proto.String("GetBigThing"),
InputType: proto.String(".my.pkg.InputType"),
OutputType: proto.String(".google.longrunning.Operation"),
Options: respLROOpts,
},
{
Name: proto.String("GetManyThings"),
Expand All @@ -152,17 +167,26 @@ func TestExample(t *testing.T) {
OutputType: proto.String(".google.longrunning.Operation"),
Options: respLROOpts,
},
{
Name: proto.String("CustomOp"),
InputType: proto.String(".my.pkg.InputType"),
OutputType: proto.String(".my.pkg.Operation"),
Options: customOpOpts,
},
},
}
for _, tst := range []struct {
tstName, pkgName string
transports []transport
imports map[pbinfo.ImportSpec]bool
tstName string
imports map[pbinfo.ImportSpec]bool
options options
op *customOp
}{
{
tstName: "empty_example",
pkgName: "Foo",
transports: []transport{grpc, rest},
tstName: "empty_example",
options: options{
pkgName: "Foo",
transports: []transport{grpc, rest},
},
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/api/iterator"}: true,
Expand All @@ -174,9 +198,11 @@ func TestExample(t *testing.T) {
},
},
{
tstName: "empty_example_grpc",
pkgName: "Foo",
transports: []transport{grpc},
tstName: "empty_example_grpc",
options: options{
pkgName: "Foo",
transports: []transport{grpc},
},
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/api/iterator"}: true,
Expand All @@ -188,9 +214,11 @@ func TestExample(t *testing.T) {
},
},
{
tstName: "foo_example",
pkgName: "Bar",
transports: []transport{grpc, rest},
tstName: "foo_example",
options: options{
pkgName: "Bar",
transports: []transport{grpc, rest},
},
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/api/iterator"}: true,
Expand All @@ -202,9 +230,11 @@ func TestExample(t *testing.T) {
},
},
{
tstName: "foo_example_rest",
pkgName: "Bar",
transports: []transport{rest},
tstName: "foo_example_rest",
options: options{
pkgName: "Bar",
transports: []transport{rest},
},
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/api/iterator"}: true,
Expand All @@ -215,10 +245,29 @@ func TestExample(t *testing.T) {
{Name: "mypackagepb", Path: "mypackage"}: true,
},
},
{
tstName: "custom_op_example",
options: options{
pkgName: "Bar",
transports: []transport{rest},
diregapic: true,
},
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/api/iterator"}: true,
{Path: "io"}: true,
{Name: "mypackagepb", Path: "mypackage"}: true,
},
op: &customOp{message: cop},
},
} {
g.reset()
g.opts.pkgName = tst.pkgName
g.opts.transports = tst.transports
g.opts = &tst.options
g.mixins = mix
if tst.options.diregapic {
g.mixins = nil
}
g.aux.customOp = tst.op
g.genExampleFile(serv)
if diff := cmp.Diff(g.imports, tst.imports); diff != "" {
t.Errorf("TestExample(%s): imports got(-),want(+):\n%s", tst.tstName, diff)
Expand Down
5 changes: 1 addition & 4 deletions internal/gengapic/gengapic.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,7 @@ func (g *generator) returnType(m *descriptor.MethodDescriptorProto) (string, err
if err != nil {
return "", err
}
info, err := getHTTPInfo(m)
if err != nil {
return "", err
}
info := getHTTPInfo(m)

// Regular return type.
retTyp := fmt.Sprintf("*%s.%s", outSpec.Name, outType.GetName())
Expand Down
31 changes: 11 additions & 20 deletions internal/gengapic/genrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ type httpInfo struct {

func (g *generator) pathParams(m *descriptor.MethodDescriptorProto) map[string]*descriptor.FieldDescriptorProto {
pathParams := map[string]*descriptor.FieldDescriptorProto{}
info, err := getHTTPInfo(m)
if info == nil || err != nil {
info := getHTTPInfo(m)
if info == nil {
return pathParams
}

Expand All @@ -218,8 +218,8 @@ func (g *generator) pathParams(m *descriptor.MethodDescriptorProto) map[string]*

func (g *generator) queryParams(m *descriptor.MethodDescriptorProto) map[string]*descriptor.FieldDescriptorProto {
queryParams := map[string]*descriptor.FieldDescriptorProto{}
info, err := getHTTPInfo(m)
if info == nil || err != nil {
info := getHTTPInfo(m)
if info == nil {
return queryParams
}
if info.body == "*" {
Expand Down Expand Up @@ -395,10 +395,7 @@ func (g *generator) generateQueryString(m *descriptor.MethodDescriptorProto) {
}

func (g *generator) generateURLString(m *descriptor.MethodDescriptorProto) error {
info, err := getHTTPInfo(m)
if err != nil {
return err
}
info := getHTTPInfo(m)
if info == nil {
return errors.E(nil, "method has no http info: %s", m.GetName())
}
Expand Down Expand Up @@ -427,9 +424,9 @@ func (g *generator) generateURLString(m *descriptor.MethodDescriptorProto) error
return nil
}

func getHTTPInfo(m *descriptor.MethodDescriptorProto) (*httpInfo, error) {
func getHTTPInfo(m *descriptor.MethodDescriptorProto) *httpInfo {
if m == nil || m.GetOptions() == nil {
return nil, nil
return nil
}

eHTTP := proto.GetExtension(m.GetOptions(), annotations.E_Http)
Expand All @@ -455,7 +452,7 @@ func getHTTPInfo(m *descriptor.MethodDescriptorProto) (*httpInfo, error) {
info.url = httpRule.GetDelete()
}

return &info, nil
return &info
}

// genRESTMethod generates a single method from a client. m must be a method declared in serv.
Expand Down Expand Up @@ -561,7 +558,7 @@ func (g *generator) pagingRESTCall(servName string, m *descriptor.MethodDescript
if err != nil {
return err
}
info, err := getHTTPInfo(m)
info := getHTTPInfo(m)
if err != nil {
return err
}
Expand Down Expand Up @@ -692,10 +689,7 @@ func (g *generator) lroRESTCall(servName string, m *descriptor.MethodDescriptorP
}

func (g *generator) emptyUnaryRESTCall(servName string, m *descriptor.MethodDescriptorProto) error {
info, err := getHTTPInfo(m)
if err != nil {
return err
}
info := getHTTPInfo(m)
if info == nil {
return errors.E(nil, "method has no http info: %s", m.GetName())
}
Expand Down Expand Up @@ -771,10 +765,7 @@ func (g *generator) emptyUnaryRESTCall(servName string, m *descriptor.MethodDesc
}

func (g *generator) unaryRESTCall(servName string, m *descriptor.MethodDescriptorProto) error {
info, err := getHTTPInfo(m)
if err != nil {
return err
}
info := getHTTPInfo(m)
if info == nil {
return errors.E(nil, "method has no http info: %s", m.GetName())
}
Expand Down
Loading

0 comments on commit ccaa033

Please sign in to comment.