in packages/codegen.go/src/operations.ts [340:412]
function emitPagerDefinition(client: go.Client, method: go.PageableMethod, imports: ImportManager, injectSpans: boolean, generateFakes: boolean): string {
imports.add('context');
let text = `runtime.NewPager(runtime.PagingHandler[${method.responseEnvelope.name}]{\n`;
text += `\t\tMore: func(page ${method.responseEnvelope.name}) bool {\n`;
// there is no advancer for single-page pagers
if (method.nextLinkName) {
text += `\t\t\treturn page.${method.nextLinkName} != nil && len(*page.${method.nextLinkName}) > 0\n`;
text += '\t\t},\n';
} else {
text += '\t\t\treturn false\n';
text += '\t\t},\n';
}
text += `\t\tFetcher: func(ctx context.Context, page *${method.responseEnvelope.name}) (${method.responseEnvelope.name}, error) {\n`;
const reqParams = helpers.getCreateRequestParameters(method);
if (generateFakes) {
text += `\t\tctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "${client.name}.${fixUpMethodName(method)}")\n`;
}
if (method.nextLinkName) {
let nextLinkVar: string;
if (!go.isLROMethod(method)) {
text += '\t\t\tnextLink := ""\n';
nextLinkVar = 'nextLink';
text += '\t\t\tif page != nil {\n';
text += `\t\t\t\tnextLink = *page.${method.nextLinkName}\n\t\t\t}\n`;
} else {
nextLinkVar = `*page.${method.nextLinkName}`;
}
text += `\t\t\tresp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), ${nextLinkVar}, func(ctx context.Context) (*policy.Request, error) {\n`;
text += `\t\t\t\treturn client.${method.naming.requestMethod}(${reqParams})\n\t\t\t}, `;
// nextPageMethod might be absent in some cases, see https://github.com/Azure/autorest/issues/4393
if (method.nextPageMethod) {
const nextOpParams = helpers.getCreateRequestParametersSig(method.nextPageMethod).split(',');
// keep the parameter names from the name/type tuples and find nextLink param
for (let i = 0; i < nextOpParams.length; ++i) {
const paramName = nextOpParams[i].trim().split(' ')[0];
const paramType = nextOpParams[i].trim().split(' ')[1];
if (paramName.startsWith('next') && paramType === 'string') {
nextOpParams[i] = 'encodedNextLink';
} else {
nextOpParams[i] = paramName;
}
}
// add a definition for the nextReq func that uses the nextLinkOperation
text += '&runtime.FetcherForNextLinkOptions{\n\t\t\t\tNextReq: func(ctx context.Context, encodedNextLink string) (*policy.Request, error) {\n';
text += `\t\t\t\t\treturn client.${method.nextPageMethod.name}(${nextOpParams.join(', ')})\n\t\t\t\t},\n\t\t\t})\n`;
} else {
text += 'nil)\n';
}
text += `\t\t\tif err != nil {\n\t\t\t\treturn ${method.responseEnvelope.name}{}, err\n\t\t\t}\n`;
text += `\t\t\treturn client.${method.naming.responseMethod}(resp)\n`;
text += '\t\t\t},\n';
} else {
// this is the singular page case, no fetcher helper required
text += `\t\t\treq, err := client.${method.naming.requestMethod}(${reqParams})\n`;
text += '\t\t\tif err != nil {\n';
text += `\t\t\t\treturn ${method.responseEnvelope.name}{}, err\n`;
text += '\t\t\t}\n';
text += '\t\t\tresp, err := client.internal.Pipeline().Do(req)\n';
text += '\t\t\tif err != nil {\n';
text += `\t\t\t\treturn ${method.responseEnvelope.name}{}, err\n`;
text += '\t\t\t}\n';
text += '\t\t\tif !runtime.HasStatusCode(resp, http.StatusOK) {\n';
text += `\t\t\t\treturn ${method.responseEnvelope.name}{}, runtime.NewResponseError(resp)\n`;
text += '\t\t\t}\n';
text += `\t\t\treturn client.${method.naming.responseMethod}(resp)\n`;
text += '\t\t},\n';
}
if (injectSpans) {
text += '\t\tTracer: client.internal.Tracer(),\n';
}
text += '\t})\n';
return text;
}