@@ -147,7 +147,9 @@ func (g *generator) exampleMethodBody(pkgName, servName string, m *descriptor.Me
147
147
return err
148
148
}
149
149
if pf != nil {
150
- g .examplePagingCall (m )
150
+ if err := g .examplePagingCall (m ); err != nil {
151
+ return err
152
+ }
151
153
} else if g .isLRO (m ) || g .isCustomOp (m , httpInfo ) {
152
154
g .exampleLROCall (m )
153
155
} else if * m .OutputType == emptyType {
@@ -211,10 +213,20 @@ func (g *generator) exampleEmptyCall(m *descriptor.MethodDescriptorProto) {
211
213
p ("}" )
212
214
}
213
215
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
+
215
227
p := g .printf
216
228
217
- p ("it := c.%s(ctx, req)" , * m . Name )
229
+ p ("it := c.%s(ctx, req)" , m . GetName () )
218
230
p ("for {" )
219
231
p (" resp, err := it.Next()" )
220
232
p (" if err == iterator.Done {" )
@@ -225,9 +237,17 @@ func (g *generator) examplePagingCall(m *descriptor.MethodDescriptorProto) {
225
237
p (" }" )
226
238
p (" // TODO: Use resp." )
227
239
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 ())
228
246
p ("}" )
229
247
230
248
g .imports [pbinfo.ImportSpec {Path : "google.golang.org/api/iterator" }] = true
249
+ g .imports [outSpec ] = true
250
+ return nil
231
251
}
232
252
233
253
func (g * generator ) exampleBidiCall (m * descriptor.MethodDescriptorProto , inType pbinfo.ProtoType , inSpec pbinfo.ImportSpec ) {
0 commit comments