Skip to content

Commit

Permalink
support Azure.Core.@useFinalStateVia and add test (#1396)
Browse files Browse the repository at this point in the history
* wip

* wip

* support Azure.Core.@useFinalStateVia and add test

* mod tidy

---------

Co-authored-by: Joel Hendrix <[email protected]>
  • Loading branch information
tadelesh and jhendrixMSFT authored Jul 2, 2024
1 parent e5df084 commit 4c4fbf9
Show file tree
Hide file tree
Showing 33 changed files with 5,462 additions and 26 deletions.
15 changes: 8 additions & 7 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/typespec-go/.scripts/tspcompile.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ generate('armrandom', armrandom, 'test/armrandom');
const armcommunitymanagement = pkgRoot + 'test/tsp/Community.Management';
generate('armcommunitymanagement', armcommunitymanagement, 'test/armcommunitymanagement');

const armmongocluster = pkgRoot + 'test/tsp/MongoCluster.Management';
generate('armmongocluster', armmongocluster, 'test/armmongocluster');

for (const module in cadlRanch) {
const values = cadlRanch[module];
let perTestOptions;
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-go/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@azure-tools/codegen": "~2.9.2",
"@azure-tools/linq": "~3.1.0",
"@azure-tools/typespec-azure-core": ">=0.43.0 <1.0.0",
"@azure-tools/typespec-client-generator-core": "0.43.0",
"@azure-tools/typespec-client-generator-core": "0.43.1",
"@typespec/compiler": "0.57.0",
"@typespec/http": "0.57.0",
"@typespec/rest": "0.57.0",
Expand Down
15 changes: 11 additions & 4 deletions packages/typespec-go/src/tcgcadapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ function fixStutteringTypeNames(sdkPackage: tcgc.SdkPackage<tcgc.SdkHttpOperatio

// ensure that enum, client, and struct type names don't stutter

for (const sdkClient of sdkPackage.clients) {
sdkClient.name = trimPackagePrefix(stutteringPrefix, sdkClient.name);
const recursiveWalkClients = function(client: tcgc.SdkClientType<tcgc.SdkHttpOperation>): void {
client.name = trimPackagePrefix(stutteringPrefix, client.name);

// fix up the synthesized type names for page responses
for (const sdkMethod of sdkClient.methods) {
if (sdkMethod.kind !== 'paging') {
for (const sdkMethod of client.methods) {
if (sdkMethod.kind === 'clientaccessor') {
recursiveWalkClients(sdkMethod.response);
continue;
} else if (sdkMethod.kind !== 'paging') {
continue;
}

Expand All @@ -86,6 +89,10 @@ function fixStutteringTypeNames(sdkPackage: tcgc.SdkPackage<tcgc.SdkHttpOperatio
httpResp.type.name = trimPackagePrefix(stutteringPrefix, httpResp.type.name);
}
}
};

for (const sdkClient of sdkPackage.clients) {
recursiveWalkClients(sdkClient);
}

// check if the name collides with an existing name. we only do
Expand Down
13 changes: 13 additions & 0 deletions packages/typespec-go/src/tcgcadapter/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ export class clientAdapter {
}
} else if (sdkMethod.kind === 'lro') {
method = new go.LROMethod(methodName, goClient, sdkMethod.operation.path, sdkMethod.operation.verb, statusCodes, naming);
const lroOptions = this.hasDecorator('Azure.Core.@useFinalStateVia', sdkMethod.decorators);
if (lroOptions) {
(<go.LROMethod>method).finalStateVia = lroOptions['finalState'];
}
} else {
throw new Error(`method kind ${sdkMethod.kind} NYI`);
}
Expand All @@ -210,6 +214,15 @@ export class clientAdapter {
this.populateMethod(sdkMethod, method);
}

private hasDecorator(name: string, decorators: Array<tcgc.DecoratorInfo>): Record<string, any> | undefined {
for (const decorator of decorators) {
if (decorator.name === name) {
return decorator.arguments;
}
}
return undefined;
}

private populateMethod(sdkMethod: tcgc.SdkServiceMethod<tcgc.SdkHttpOperation>, method: go.Method | go.NextPageMethod) {
if (go.isMethod(method)) {
let prefix = method.client.name;
Expand Down
45 changes: 31 additions & 14 deletions packages/typespec-go/src/tcgcadapter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ export class typeAdapter {
// returns the synthesized paged response types
private getPagedResponses(sdkContext: tcgc.SdkContext): Array<tcgc.SdkModelType> {
const pagedResponses = new Array<tcgc.SdkModelType>();
for (const sdkClient of sdkContext.experimental_sdkPackage.clients) {
for (const sdkMethod of sdkClient.methods) {
if (sdkMethod.kind !== 'paging') {
const recursiveWalkClients = function(client: tcgc.SdkClientType<tcgc.SdkHttpOperation>): void {
for (const sdkMethod of client.methods) {
if (sdkMethod.kind === 'clientaccessor') {
recursiveWalkClients(sdkMethod.response);
continue;
} else if (sdkMethod.kind !== 'paging') {
continue;
}

Expand All @@ -135,6 +138,10 @@ export class typeAdapter {
}
}
}
};

for (const sdkClient of sdkContext.experimental_sdkPackage.clients) {
recursiveWalkClients(sdkClient);
}
return pagedResponses;
}
Expand Down Expand Up @@ -794,18 +801,10 @@ export class typeAdapter {
}
};

// traverse all client initialization params and methods to find the set of referenced enums and models
for (const client of sdkContext.experimental_sdkPackage.clients) {
for (const param of client.initialization.properties) {
if (param.kind === 'endpoint' && param.type.kind === 'endpoint') {
for (const templateArg of param.type.templateArguments) {
recursiveAddReferencedType(templateArg.type);
}
}
}

const recursiveWalkClients = function(client: tcgc.SdkClientType<tcgc.SdkHttpOperation>): void {
for (const method of client.methods) {
if (method.kind === 'clientaccessor') {
recursiveWalkClients(method.response);
continue;
}

Expand All @@ -817,6 +816,19 @@ export class typeAdapter {
recursiveAddReferencedType(method.response.type);
}
}
};

// traverse all client initialization params and methods to find the set of referenced enums and models
for (const client of sdkContext.experimental_sdkPackage.clients) {
for (const param of client.initialization.properties) {
if (param.kind === 'endpoint' && param.type.kind === 'endpoint') {
for (const templateArg of param.type.templateArguments) {
recursiveAddReferencedType(templateArg.type);
}
}
}

recursiveWalkClients(client);
}

const pagedResponses = this.getPagedResponses(sdkContext);
Expand Down Expand Up @@ -893,9 +905,10 @@ export class typeAdapter {
// traverse all methods to find any references to a base model type.
// NOTE: it's possible for there to be no base types.
if (baseModels.size > 0) {
for (const client of sdkContext.experimental_sdkPackage.clients) {
const recursiveWalkClients = function(client: tcgc.SdkClientType<tcgc.SdkHttpOperation>): void {
for (const method of client.methods) {
if (method.kind === 'clientaccessor') {
recursiveWalkClients(method.response);
continue;
}

Expand All @@ -907,6 +920,10 @@ export class typeAdapter {
recursiveAddReferencedBaseModel(method.response.type);
}
}
};

for (const client of sdkContext.experimental_sdkPackage.clients) {
recursiveWalkClients(client);
}

const pagedResponses = this.getPagedResponses(sdkContext);
Expand Down
Loading

0 comments on commit 4c4fbf9

Please sign in to comment.