From 02e6c65e2df34e5bcb1c85460a2a422db971d843 Mon Sep 17 00:00:00 2001 From: Noah Dietz Date: Fri, 8 Mar 2024 08:16:08 -0800 Subject: [PATCH] fix(internal/gengapic): add iter response access example (#1468) * fix(internal/gengapic): add iter response access example * update example language --- internal/gengapic/example.go | 26 ++++++++++++++++--- .../gengapic/testdata/custom_op_example.want | 6 +++++ internal/gengapic/testdata/empty_example.want | 18 +++++++++++++ .../gengapic/testdata/empty_example_grpc.want | 18 +++++++++++++ internal/gengapic/testdata/foo_example.want | 18 +++++++++++++ .../gengapic/testdata/foo_example_rest.want | 18 +++++++++++++ .../testdata/snippet_GetManyThings.want | 6 +++++ 7 files changed, 107 insertions(+), 3 deletions(-) diff --git a/internal/gengapic/example.go b/internal/gengapic/example.go index fc2dbe2ef..dcf1a3bb5 100644 --- a/internal/gengapic/example.go +++ b/internal/gengapic/example.go @@ -147,7 +147,9 @@ func (g *generator) exampleMethodBody(pkgName, servName string, m *descriptor.Me return err } if pf != nil { - g.examplePagingCall(m) + if err := g.examplePagingCall(m); err != nil { + return err + } } else if g.isLRO(m) || g.isCustomOp(m, httpInfo) { g.exampleLROCall(m) } else if *m.OutputType == emptyType { @@ -211,10 +213,20 @@ func (g *generator) exampleEmptyCall(m *descriptor.MethodDescriptorProto) { p("}") } -func (g *generator) examplePagingCall(m *descriptor.MethodDescriptorProto) { +func (g *generator) examplePagingCall(m *descriptor.MethodDescriptorProto) error { + outType := g.descInfo.Type[m.GetOutputType()] + if outType == nil { + return fmt.Errorf("cannot find type %q, malformed descriptor?", m.GetOutputType()) + } + + outSpec, err := g.descInfo.ImportSpec(outType) + if err != nil { + return err + } + p := g.printf - p("it := c.%s(ctx, req)", *m.Name) + p("it := c.%s(ctx, req)", m.GetName()) p("for {") p(" resp, err := it.Next()") p(" if err == iterator.Done {") @@ -225,9 +237,17 @@ func (g *generator) examplePagingCall(m *descriptor.MethodDescriptorProto) { p(" }") p(" // TODO: Use resp.") p(" _ = resp") + p("") + p(" // If you need to access the underlying RPC response,") + p(" // you can do so by casting the `Response` as below.") + p(" // Otherwise, remove this line. Only populated after") + p(" // first call to Next(). Not safe for concurrent access.") + p(" _ = it.Response.(*%s.%s)", outSpec.Name, outType.GetName()) p("}") g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/iterator"}] = true + g.imports[outSpec] = true + return nil } func (g *generator) exampleBidiCall(m *descriptor.MethodDescriptorProto, inType pbinfo.ProtoType, inSpec pbinfo.ImportSpec) { diff --git a/internal/gengapic/testdata/custom_op_example.want b/internal/gengapic/testdata/custom_op_example.want index c5dbdf9bb..605f66ace 100644 --- a/internal/gengapic/testdata/custom_op_example.want +++ b/internal/gengapic/testdata/custom_op_example.want @@ -121,6 +121,12 @@ func ExampleFooClient_GetManyThings() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*mypackagepb.PageOutputType) } } diff --git a/internal/gengapic/testdata/empty_example.want b/internal/gengapic/testdata/empty_example.want index 0b5193199..e4987397c 100644 --- a/internal/gengapic/testdata/empty_example.want +++ b/internal/gengapic/testdata/empty_example.want @@ -138,6 +138,12 @@ func ExampleClient_GetManyThings() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*mypackagepb.PageOutputType) } } @@ -292,6 +298,12 @@ func ExampleClient_ListLocations() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*locationpb.ListLocationsResponse) } } @@ -423,6 +435,12 @@ func ExampleClient_ListOperations() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*longrunningpb.ListOperationsResponse) } } diff --git a/internal/gengapic/testdata/empty_example_grpc.want b/internal/gengapic/testdata/empty_example_grpc.want index a44e7a1e1..e83848bf0 100644 --- a/internal/gengapic/testdata/empty_example_grpc.want +++ b/internal/gengapic/testdata/empty_example_grpc.want @@ -121,6 +121,12 @@ func ExampleClient_GetManyThings() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*mypackagepb.PageOutputType) } } @@ -275,6 +281,12 @@ func ExampleClient_ListLocations() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*locationpb.ListLocationsResponse) } } @@ -406,6 +418,12 @@ func ExampleClient_ListOperations() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*longrunningpb.ListOperationsResponse) } } diff --git a/internal/gengapic/testdata/foo_example.want b/internal/gengapic/testdata/foo_example.want index b96622004..8c8488485 100644 --- a/internal/gengapic/testdata/foo_example.want +++ b/internal/gengapic/testdata/foo_example.want @@ -138,6 +138,12 @@ func ExampleFooClient_GetManyThings() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*mypackagepb.PageOutputType) } } @@ -292,6 +298,12 @@ func ExampleFooClient_ListLocations() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*locationpb.ListLocationsResponse) } } @@ -423,6 +435,12 @@ func ExampleFooClient_ListOperations() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*longrunningpb.ListOperationsResponse) } } diff --git a/internal/gengapic/testdata/foo_example_rest.want b/internal/gengapic/testdata/foo_example_rest.want index d0f4682ed..dd47e9560 100644 --- a/internal/gengapic/testdata/foo_example_rest.want +++ b/internal/gengapic/testdata/foo_example_rest.want @@ -121,6 +121,12 @@ func ExampleFooClient_GetManyThings() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*mypackagepb.PageOutputType) } } @@ -275,6 +281,12 @@ func ExampleFooClient_ListLocations() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*locationpb.ListLocationsResponse) } } @@ -406,6 +418,12 @@ func ExampleFooClient_ListOperations() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*longrunningpb.ListOperationsResponse) } } diff --git a/internal/gengapic/testdata/snippet_GetManyThings.want b/internal/gengapic/testdata/snippet_GetManyThings.want index d0eb6d6e7..c7d2fa3bd 100644 --- a/internal/gengapic/testdata/snippet_GetManyThings.want +++ b/internal/gengapic/testdata/snippet_GetManyThings.want @@ -26,6 +26,12 @@ func main() { } // TODO: Use resp. _ = resp + + // If you need to access the underlying RPC response, + // you can do so by casting the `Response` as below. + // Otherwise, remove this line. Only populated after + // first call to Next(). Not safe for concurrent access. + _ = it.Response.(*mypackagepb.PageOutputType) } }