Skip to content

Commit 02e6c65

Browse files
authored
fix(internal/gengapic): add iter response access example (googleapis#1468)
* fix(internal/gengapic): add iter response access example * update example language
1 parent 41b625b commit 02e6c65

7 files changed

+107
-3
lines changed

internal/gengapic/example.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ func (g *generator) exampleMethodBody(pkgName, servName string, m *descriptor.Me
147147
return err
148148
}
149149
if pf != nil {
150-
g.examplePagingCall(m)
150+
if err := g.examplePagingCall(m); err != nil {
151+
return err
152+
}
151153
} else if g.isLRO(m) || g.isCustomOp(m, httpInfo) {
152154
g.exampleLROCall(m)
153155
} else if *m.OutputType == emptyType {
@@ -211,10 +213,20 @@ func (g *generator) exampleEmptyCall(m *descriptor.MethodDescriptorProto) {
211213
p("}")
212214
}
213215

214-
func (g *generator) examplePagingCall(m *descriptor.MethodDescriptorProto) {
216+
func (g *generator) examplePagingCall(m *descriptor.MethodDescriptorProto) error {
217+
outType := g.descInfo.Type[m.GetOutputType()]
218+
if outType == nil {
219+
return fmt.Errorf("cannot find type %q, malformed descriptor?", m.GetOutputType())
220+
}
221+
222+
outSpec, err := g.descInfo.ImportSpec(outType)
223+
if err != nil {
224+
return err
225+
}
226+
215227
p := g.printf
216228

217-
p("it := c.%s(ctx, req)", *m.Name)
229+
p("it := c.%s(ctx, req)", m.GetName())
218230
p("for {")
219231
p(" resp, err := it.Next()")
220232
p(" if err == iterator.Done {")
@@ -225,9 +237,17 @@ func (g *generator) examplePagingCall(m *descriptor.MethodDescriptorProto) {
225237
p(" }")
226238
p(" // TODO: Use resp.")
227239
p(" _ = resp")
240+
p("")
241+
p(" // If you need to access the underlying RPC response,")
242+
p(" // you can do so by casting the `Response` as below.")
243+
p(" // Otherwise, remove this line. Only populated after")
244+
p(" // first call to Next(). Not safe for concurrent access.")
245+
p(" _ = it.Response.(*%s.%s)", outSpec.Name, outType.GetName())
228246
p("}")
229247

230248
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/iterator"}] = true
249+
g.imports[outSpec] = true
250+
return nil
231251
}
232252

233253
func (g *generator) exampleBidiCall(m *descriptor.MethodDescriptorProto, inType pbinfo.ProtoType, inSpec pbinfo.ImportSpec) {

internal/gengapic/testdata/custom_op_example.want

+6
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ func ExampleFooClient_GetManyThings() {
121121
}
122122
// TODO: Use resp.
123123
_ = resp
124+
125+
// If you need to access the underlying RPC response,
126+
// you can do so by casting the `Response` as below.
127+
// Otherwise, remove this line. Only populated after
128+
// first call to Next(). Not safe for concurrent access.
129+
_ = it.Response.(*mypackagepb.PageOutputType)
124130
}
125131
}
126132

internal/gengapic/testdata/empty_example.want

+18
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ func ExampleClient_GetManyThings() {
138138
}
139139
// TODO: Use resp.
140140
_ = resp
141+
142+
// If you need to access the underlying RPC response,
143+
// you can do so by casting the `Response` as below.
144+
// Otherwise, remove this line. Only populated after
145+
// first call to Next(). Not safe for concurrent access.
146+
_ = it.Response.(*mypackagepb.PageOutputType)
141147
}
142148
}
143149

@@ -292,6 +298,12 @@ func ExampleClient_ListLocations() {
292298
}
293299
// TODO: Use resp.
294300
_ = resp
301+
302+
// If you need to access the underlying RPC response,
303+
// you can do so by casting the `Response` as below.
304+
// Otherwise, remove this line. Only populated after
305+
// first call to Next(). Not safe for concurrent access.
306+
_ = it.Response.(*locationpb.ListLocationsResponse)
295307
}
296308
}
297309

@@ -423,6 +435,12 @@ func ExampleClient_ListOperations() {
423435
}
424436
// TODO: Use resp.
425437
_ = resp
438+
439+
// If you need to access the underlying RPC response,
440+
// you can do so by casting the `Response` as below.
441+
// Otherwise, remove this line. Only populated after
442+
// first call to Next(). Not safe for concurrent access.
443+
_ = it.Response.(*longrunningpb.ListOperationsResponse)
426444
}
427445
}
428446

internal/gengapic/testdata/empty_example_grpc.want

+18
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ func ExampleClient_GetManyThings() {
121121
}
122122
// TODO: Use resp.
123123
_ = resp
124+
125+
// If you need to access the underlying RPC response,
126+
// you can do so by casting the `Response` as below.
127+
// Otherwise, remove this line. Only populated after
128+
// first call to Next(). Not safe for concurrent access.
129+
_ = it.Response.(*mypackagepb.PageOutputType)
124130
}
125131
}
126132

@@ -275,6 +281,12 @@ func ExampleClient_ListLocations() {
275281
}
276282
// TODO: Use resp.
277283
_ = resp
284+
285+
// If you need to access the underlying RPC response,
286+
// you can do so by casting the `Response` as below.
287+
// Otherwise, remove this line. Only populated after
288+
// first call to Next(). Not safe for concurrent access.
289+
_ = it.Response.(*locationpb.ListLocationsResponse)
278290
}
279291
}
280292

@@ -406,6 +418,12 @@ func ExampleClient_ListOperations() {
406418
}
407419
// TODO: Use resp.
408420
_ = resp
421+
422+
// If you need to access the underlying RPC response,
423+
// you can do so by casting the `Response` as below.
424+
// Otherwise, remove this line. Only populated after
425+
// first call to Next(). Not safe for concurrent access.
426+
_ = it.Response.(*longrunningpb.ListOperationsResponse)
409427
}
410428
}
411429

internal/gengapic/testdata/foo_example.want

+18
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ func ExampleFooClient_GetManyThings() {
138138
}
139139
// TODO: Use resp.
140140
_ = resp
141+
142+
// If you need to access the underlying RPC response,
143+
// you can do so by casting the `Response` as below.
144+
// Otherwise, remove this line. Only populated after
145+
// first call to Next(). Not safe for concurrent access.
146+
_ = it.Response.(*mypackagepb.PageOutputType)
141147
}
142148
}
143149

@@ -292,6 +298,12 @@ func ExampleFooClient_ListLocations() {
292298
}
293299
// TODO: Use resp.
294300
_ = resp
301+
302+
// If you need to access the underlying RPC response,
303+
// you can do so by casting the `Response` as below.
304+
// Otherwise, remove this line. Only populated after
305+
// first call to Next(). Not safe for concurrent access.
306+
_ = it.Response.(*locationpb.ListLocationsResponse)
295307
}
296308
}
297309

@@ -423,6 +435,12 @@ func ExampleFooClient_ListOperations() {
423435
}
424436
// TODO: Use resp.
425437
_ = resp
438+
439+
// If you need to access the underlying RPC response,
440+
// you can do so by casting the `Response` as below.
441+
// Otherwise, remove this line. Only populated after
442+
// first call to Next(). Not safe for concurrent access.
443+
_ = it.Response.(*longrunningpb.ListOperationsResponse)
426444
}
427445
}
428446

internal/gengapic/testdata/foo_example_rest.want

+18
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ func ExampleFooClient_GetManyThings() {
121121
}
122122
// TODO: Use resp.
123123
_ = resp
124+
125+
// If you need to access the underlying RPC response,
126+
// you can do so by casting the `Response` as below.
127+
// Otherwise, remove this line. Only populated after
128+
// first call to Next(). Not safe for concurrent access.
129+
_ = it.Response.(*mypackagepb.PageOutputType)
124130
}
125131
}
126132

@@ -275,6 +281,12 @@ func ExampleFooClient_ListLocations() {
275281
}
276282
// TODO: Use resp.
277283
_ = resp
284+
285+
// If you need to access the underlying RPC response,
286+
// you can do so by casting the `Response` as below.
287+
// Otherwise, remove this line. Only populated after
288+
// first call to Next(). Not safe for concurrent access.
289+
_ = it.Response.(*locationpb.ListLocationsResponse)
278290
}
279291
}
280292

@@ -406,6 +418,12 @@ func ExampleFooClient_ListOperations() {
406418
}
407419
// TODO: Use resp.
408420
_ = resp
421+
422+
// If you need to access the underlying RPC response,
423+
// you can do so by casting the `Response` as below.
424+
// Otherwise, remove this line. Only populated after
425+
// first call to Next(). Not safe for concurrent access.
426+
_ = it.Response.(*longrunningpb.ListOperationsResponse)
409427
}
410428
}
411429

internal/gengapic/testdata/snippet_GetManyThings.want

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ func main() {
2626
}
2727
// TODO: Use resp.
2828
_ = resp
29+
30+
// If you need to access the underlying RPC response,
31+
// you can do so by casting the `Response` as below.
32+
// Otherwise, remove this line. Only populated after
33+
// first call to Next(). Not safe for concurrent access.
34+
_ = it.Response.(*mypackagepb.PageOutputType)
2935
}
3036
}
3137

0 commit comments

Comments
 (0)