Skip to content

Commit

Permalink
fix(internal/gengapic): add iter response access example (#1468)
Browse files Browse the repository at this point in the history
* fix(internal/gengapic): add iter response access example

* update example language
  • Loading branch information
noahdietz authored Mar 8, 2024
1 parent 41b625b commit 02e6c65
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 3 deletions.
26 changes: 23 additions & 3 deletions internal/gengapic/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {")
Expand All @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions internal/gengapic/testdata/custom_op_example.want
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
18 changes: 18 additions & 0 deletions internal/gengapic/testdata/empty_example.want
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
18 changes: 18 additions & 0 deletions internal/gengapic/testdata/empty_example_grpc.want
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
18 changes: 18 additions & 0 deletions internal/gengapic/testdata/foo_example.want
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
18 changes: 18 additions & 0 deletions internal/gengapic/testdata/foo_example_rest.want
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
6 changes: 6 additions & 0 deletions internal/gengapic/testdata/snippet_GetManyThings.want
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 02e6c65

Please sign in to comment.