Skip to content

Commit ea3ff39

Browse files
codegen.go: support seeding pager with initial nextLink
If a user wishes to resume paging from a particular link, they may pass a parameter to the list operation method on their client. This next link will be used for the first paging operation. Signed-off-by: Steve Kuznetsov <[email protected]>
1 parent f797b20 commit ea3ff39

File tree

79 files changed

+416
-143
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+416
-143
lines changed

packages/codegen.go/src/fake/servers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,8 @@ function parseHeaderPathQueryParams(clientPkg: string, method: go.Method, import
831831
// client params and parameter literals aren't passed to APIs
832832
continue;
833833
}
834-
if (go.isResumeTokenParameter(param)) {
835-
// skip the ResumeToken param as we don't send that back to the caller
834+
if (go.isResumeTokenParameter(param) || go.isNextLinkParameter(param)) {
835+
// skip the ResumeToken and NextLink params as we don't send them back to the caller
836836
continue;
837837
}
838838

@@ -1183,7 +1183,7 @@ function populateApiParams(clientPkg: string, method: go.Method, paramValues: Ma
11831183
if (helpers.isParameterGroup(param)) {
11841184
if (param.groupName === method.optionalParamsGroup.groupName) {
11851185
// this is the optional params type. in some cases we just pass nil
1186-
const countParams = values(param.params).where((each: go.Parameter) => { return !go.isResumeTokenParameter(each); }).count();
1186+
const countParams = values(param.params).where((each: go.Parameter) => { return !go.isResumeTokenParameter(each) && !go.isNextLinkParameter(each); }).count();
11871187
if (countParams === 0) {
11881188
// if the options param is empty or only contains the resume token param just pass nil
11891189
params.push('nil');

packages/codegen.go/src/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ export function getCreateRequestParametersSig(method: go.Method | go.NextPageMet
104104
for (const methodParam of values(methodParams)) {
105105
let paramName = uncapitalize(methodParam.name);
106106
// when creating the method sig for fooCreateRequest, if the options type is empty
107-
// or only contains the ResumeToken param use _ for the param name to quiet the linter
108-
if (isParameterGroup(methodParam) && (methodParam.params.length === 0 || (methodParam.params.length === 1 && go.isResumeTokenParameter(methodParam.params[0])))) {
107+
// or only contains the ResumeToken or NextLink param use _ for the param name to quiet the linter
108+
if (isParameterGroup(methodParam) && (methodParam.params.length === 0 || (methodParam.params.length === 1 && (go.isResumeTokenParameter(methodParam.params[0]) || go.isNextLinkParameter(methodParam.params[0]))))) {
109109
paramName = '_';
110110
}
111111
params.push(`${paramName} ${formatParameterTypeName(methodParam)}`);

packages/codegen.go/src/operations.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,11 @@ function emitPagerDefinition(client: go.Client, method: go.PageableMethod, impor
358358
if (!go.isLROMethod(method)) {
359359
text += '\t\t\tnextLink := ""\n';
360360
nextLinkVar = 'nextLink';
361-
text += '\t\t\tif page != nil {\n';
362-
text += `\t\t\t\tnextLink = *page.${method.nextLinkName}\n\t\t\t}\n`;
361+
text += `\t\t\tif page != nil && page.${method.nextLinkName} != nil {\n`;
362+
text += `\t\t\t\tnextLink = *page.${method.nextLinkName}\n`;
363+
text += `\t\t\t} else if options != nil && options.NextLink != "" {\n`;
364+
text += `\t\t\t\tnextLink = options.NextLink\n`;
365+
text += `\t\t\t}\n`;
363366
} else {
364367
nextLinkVar = `*page.${method.nextLinkName}`;
365368
}
@@ -1234,7 +1237,7 @@ function generateLROBeginMethod(client: go.Client, method: go.LROMethod, imports
12341237
let pollerType = 'nil';
12351238
let pollerTypeParam = `[${method.responseEnvelope.name}]`;
12361239
if (go.isPageableMethod(method)) {
1237-
// for paged LROs, we construct a pager and pass it to the LRO ctor.
1240+
// for paged LROs, we construct a pager and pass it to the LRO actor.
12381241
pollerTypeParam = `[*runtime.Pager${pollerTypeParam}]`;
12391242
pollerType = '&pager';
12401243
text += '\tpager := ';

packages/codemodel.go/src/param.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ export interface ResumeTokenParameter extends Parameter {
181181
isResumeToken: true;
182182
}
183183

184+
export interface NextLinkParameter extends Parameter {
185+
isNextLink: true;
186+
}
187+
184188
export function isBodyParameter(param: Parameter): param is BodyParameter {
185189
return (<BodyParameter>param).bodyFormat !== undefined;
186190
}
@@ -237,6 +241,10 @@ export function isResumeTokenParameter(param: Parameter): param is ResumeTokenPa
237241
return (<ResumeTokenParameter>param).isResumeToken !== undefined;
238242
}
239243

244+
export function isNextLinkParameter(param: Parameter): param is NextLinkParameter {
245+
return (<NextLinkParameter>param).isNextLink !== undefined;
246+
}
247+
240248
export function isRequiredParameter(param: Parameter): boolean {
241249
// parameters with a client-side default value are always optional
242250
if (isClientSideDefault(param.kind)) {
@@ -380,6 +388,14 @@ export class ResumeTokenParameter extends Parameter implements ResumeTokenParame
380388
}
381389
}
382390

391+
export class NextLinkParameter extends Parameter implements NextLinkParameter {
392+
constructor() {
393+
super('NextLink', new type.PrimitiveType('string'), 'optional', true, 'method');
394+
this.isNextLink = true;
395+
this.docs.summary = 'Resumes the paging operation from the provided link.';
396+
}
397+
}
398+
383399
export class ClientSideDefault implements ClientSideDefault {
384400
constructor(defaultValue: type.LiteralValue) {
385401
this.defaultValue = defaultValue;

packages/typespec-go/src/tcgcadapter/clients.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ export class clientAdapter {
303303
if (go.isLROMethod(method)) {
304304
optionalGroup.params.push(new go.ResumeTokenParameter());
305305
}
306+
if (go.isPageableMethod(method)) {
307+
optionalGroup.params.push(new go.NextLinkParameter());
308+
}
306309
}
307310

308311
// stuff all of the operation parameters into one array for easy traversal

packages/typespec-go/test/azure-http-specs/azure/core/azurepagegroup/zz_options.go

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/typespec-go/test/azure-http-specs/azure/core/azurepagegroup/zz_page_client.go

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/typespec-go/test/azure-http-specs/azure/core/azurepagegroup/zz_pagetwomodelsaspageitem_client.go

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/typespec-go/test/azure-http-specs/azure/core/basicgroup/zz_basic_client.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/typespec-go/test/azure-http-specs/azure/core/basicgroup/zz_options.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)