Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tcgc] allow csv list of scopes #1038

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

add support for list of scopes
18 changes: 12 additions & 6 deletions packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,31 @@ function setScopedDecoratorData(
transitivity: boolean = false
): boolean {
const targetEntry = context.program.stateMap(key).get(target);
const splitScopes = scope?.split(",").map((s) => s.trim()) || [AllScopes];

// If target doesn't exist in decorator map, create a new entry
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
if (!targetEntry) {
// value is going to be a list of tuples, each tuple is a value and a list of scopes
context.program.stateMap(key).set(target, { [scope ?? AllScopes]: value });
const newObject = Object.fromEntries(splitScopes.map((scope) => [scope, value]));
context.program.stateMap(key).set(target, newObject);
return true;
}

// If target exists, but there's a specified scope and it doesn't exist in the target entry, add mapping of scope and value to target entry
const scopes = Reflect.ownKeys(targetEntry);
if (!scopes.includes(AllScopes) && scope && !scopes.includes(scope)) {
targetEntry[scope] = value;
if (!scopes.includes(AllScopes) && scope && !splitScopes.some((s) => scopes.includes(s))) {
const newObject = Object.fromEntries(splitScopes.map((scope) => [scope, value]));
context.program.stateMap(key).set(target, { ...targetEntry, ...newObject });
return true;
}
// we only want to allow multiple decorators if they each specify a different scope
if (!transitivity) {
validateDecoratorUniqueOnNode(context, target, decorator);
return false;
}
if (!Reflect.ownKeys(targetEntry).includes(AllScopes) && !scope) {
context.program.stateMap(key).set(target, { AllScopes: value });
// for transitivity situation, we could allow scope extension
if (!scopes.includes(AllScopes) && !scope) {
const newObject = Object.fromEntries(splitScopes.map((scope) => [scope, value]));
context.program.stateMap(key).set(target, { ...targetEntry, ...newObject });
}
return false;
}
Expand Down
77 changes: 77 additions & 0 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,7 @@ describe("typespec-client-generator-core: decorators", () => {
code: "duplicate-decorator",
});
});

it("duplicate-decorator diagnostic for multiple same scope", async () => {
const diagnostics = await runner.diagnose(`
@test
Expand All @@ -1552,6 +1553,82 @@ describe("typespec-client-generator-core: decorators", () => {
code: "duplicate-decorator",
});
});

it("csv scope list", async () => {
function getCodeTemplate(language: string) {
return `
@test
@access(Access.internal, "${language}")
model Test {
prop: string;
}
`;
}
const pythonRunner = await createSdkTestRunner({
emitterName: "@azure-tools/typespec-python",
});
const javaRunner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-java" });
const csharpRunner = await createSdkTestRunner({
emitterName: "@azure-tools/typespec-csharp",
});

const testCode = getCodeTemplate("python,csharp");
const { Test: TestPython } = (await pythonRunner.compile(testCode)) as { Test: Model };
strictEqual(getAccess(pythonRunner.context, TestPython), "internal");

const { Test: TestCSharp } = (await csharpRunner.compile(testCode)) as { Test: Model };
strictEqual(getAccess(csharpRunner.context, TestCSharp), "internal");

const { Test: TestJava } = (await javaRunner.compile(testCode)) as { Test: Model };
strictEqual(getAccess(javaRunner.context, TestJava), "public");
});

it("csv scope list augment", async () => {
function getCodeTemplate(language: string) {
return `
@test
model Test {
prop: string;
}

@@access(Test, Access.public, "java, ts");
@@access(Test, Access.internal, "${language}");
`;
}
const pythonRunner = await createSdkTestRunner({
emitterName: "@azure-tools/typespec-python",
});
const javaRunner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-java" });
const csharpRunner = await createSdkTestRunner({
emitterName: "@azure-tools/typespec-csharp",
});

const testCode = getCodeTemplate("python,csharp");
const { Test: TestPython } = (await pythonRunner.compile(testCode)) as { Test: Model };
strictEqual(getAccess(pythonRunner.context, TestPython), "internal");

const { Test: TestCSharp } = (await csharpRunner.compile(testCode)) as { Test: Model };
strictEqual(getAccess(csharpRunner.context, TestCSharp), "internal");

const { Test: TestJava } = (await javaRunner.compile(testCode)) as { Test: Model };
strictEqual(getAccess(javaRunner.context, TestJava), "public");
});

it("duplicate-decorator diagnostic for csv scope list", async () => {
const diagnostics = await runner.diagnose(`
@test
@access(Access.internal, "csharp,ts")
@access(Access.internal, "csharp")
op func(
@query("createdAt")
createdAt: utcDateTime;
): void;
`);

expectDiagnostics(diagnostics, {
code: "duplicate-decorator",
});
});
});

describe("@access", () => {
Expand Down
Loading