Skip to content

Commit

Permalink
[tcgc] add .generateProtocol and .generateConvenient to service m…
Browse files Browse the repository at this point in the history
…ethods (#1152)

fixes #980

---------

Co-authored-by: iscai-msft <[email protected]>
  • Loading branch information
iscai-msft and iscai-msft committed Jul 12, 2024
1 parent fcebc28 commit b38b4ca
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

add `.generateConvenient` and `.generateProtocol` for service methods. These booleans tell emitters whether to generate convenient and protocol versions for the method
2 changes: 2 additions & 0 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ interface SdkServiceMethodBase<TServiceOperation extends SdkServiceOperation>
getResponseMapping(): string | undefined;
response: SdkMethodResponse;
exception?: SdkMethodResponse;
generateConvenient: boolean;
generateProtocol: boolean;
}

export interface SdkBasicServiceMethod<TServiceOperation extends SdkServiceOperation>
Expand Down
4 changes: 4 additions & 0 deletions packages/typespec-client-generator-core/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
listClients,
listOperationGroups,
listOperationsInOperationGroup,
shouldGenerateConvenient,
shouldGenerateProtocol,
} from "./decorators.js";
import { getCorrespondingMethodParams, getSdkHttpOperation, getSdkHttpParameter } from "./http.js";
import {
Expand Down Expand Up @@ -282,6 +284,8 @@ function getSdkBasicServiceMethod<
},
crossLanguageDefintionId: getCrossLanguageDefinitionId(context, operation),
decorators: diagnostics.pipe(getTypeDecorators(context, operation)),
generateConvenient: shouldGenerateConvenient(context, operation),
generateProtocol: shouldGenerateProtocol(context, operation),
});
}

Expand Down
40 changes: 35 additions & 5 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ describe("typespec-client-generator-core: decorators", () => {
@test
op test(): void;
`;
const { test } = await runner.compile(testCode);
const { test } = await runner.compileWithBuiltInService(testCode);

const actual = shouldGenerateProtocol(
createSdkContextTestHelper(runner.context.program, {
Expand All @@ -1320,7 +1320,12 @@ describe("typespec-client-generator-core: decorators", () => {
}),
test as Operation
);

const method = runner.context.sdkPackage.clients[0].methods[0];
strictEqual(method.name, "test");
strictEqual(method.kind, "basic");
strictEqual(actual, protocolValue);
strictEqual(method.generateProtocol, protocolValue);
}
describe("@protocolAPI", () => {
it("generateProtocolMethodsTrue, operation marked protocolAPI true", async () => {
Expand All @@ -1347,7 +1352,7 @@ describe("typespec-client-generator-core: decorators", () => {
@test
op test(): void;
`;
const { test } = await runner.compile(testCode);
const { test } = await runner.compileWithBuiltInService(testCode);

const actual = shouldGenerateConvenient(
createSdkContextTestHelper(runner.program, {
Expand All @@ -1357,6 +1362,11 @@ describe("typespec-client-generator-core: decorators", () => {
test as Operation
);
strictEqual(actual, convenientValue);

const method = runner.context.sdkPackage.clients[0].methods[0];
strictEqual(method.name, "test");
strictEqual(method.kind, "basic");
strictEqual(method.generateConvenient, convenientValue);
}

describe("@convenientAPI", () => {
Expand All @@ -1374,7 +1384,7 @@ describe("typespec-client-generator-core: decorators", () => {
});

it("mark an operation as convenientAPI default, pass in sdkContext with generateConvenienceMethods false", async () => {
const { test } = await runner.compile(`
const { test } = await runner.compileWithBuiltInService(`
@convenientAPI
@test
op test(): void;
Expand All @@ -1388,6 +1398,10 @@ describe("typespec-client-generator-core: decorators", () => {
test as Operation
);
strictEqual(actual, true);
const method = runner.context.sdkPackage.clients[0].methods[0];
strictEqual(method.name, "test");
strictEqual(method.kind, "basic");
strictEqual(method.generateConvenient, true);
});
});

Expand All @@ -1403,25 +1417,41 @@ describe("typespec-client-generator-core: decorators", () => {
// java should get protocolAPI=true and convenientAPI=false
{
const runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-java" });
const { test } = (await runner.compile(testCode)) as { test: Operation };

const { test } = (await runner.compileWithBuiltInService(testCode)) as { test: Operation };

const method = runner.context.sdkPackage.clients[0].methods[0];
strictEqual(method.name, "test");
strictEqual(method.kind, "basic");

strictEqual(shouldGenerateProtocol(runner.context, test), true);
strictEqual(method.generateProtocol, true);

strictEqual(
shouldGenerateConvenient(runner.context, test),
false,
"convenientAPI should be false for java"
);
strictEqual(method.generateConvenient, false, "convenientAPI should be false for java");
}

// csharp should get protocolAPI=false and convenientAPI=true
{
const runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-csharp" });
const { test } = (await runner.compile(testCode)) as { test: Operation };
const { test } = (await runner.compileWithBuiltInService(testCode)) as { test: Operation };
const method = runner.context.sdkPackage.clients[0].methods[0];
strictEqual(method.name, "test");
strictEqual(method.kind, "basic");

strictEqual(
shouldGenerateProtocol(runner.context, test),
false,
"protocolAPI should be false for csharp"
);
strictEqual(method.generateProtocol, false, "protocolAPI should be false for csharp");

strictEqual(shouldGenerateConvenient(runner.context, test), true);
strictEqual(method.generateConvenient, true);
}
});
});
Expand Down

0 comments on commit b38b4ca

Please sign in to comment.