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 2 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
10 changes: 6 additions & 4 deletions packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ const AllScopes = Symbol.for("@azure-core/typespec-client-generator-core/all-sco
function getScopedDecoratorData(context: TCGCContext, key: symbol, target: Type): any {
const retval: Record<string | symbol, any> = context.program.stateMap(key).get(target);
if (retval === undefined) return retval;
if (Object.keys(retval).includes(context.emitterName)) return retval[context.emitterName];
const scopes = Object.keys(retval);
if (scopes.includes(context.emitterName)) return retval[context.emitterName];
return retval[AllScopes]; // in this case it applies to all languages
}

Expand All @@ -77,8 +78,9 @@ function setScopedDecoratorData(
const targetEntry = context.program.stateMap(key).get(target);
// 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 splitScopes = scope?.split(",") || [AllScopes];
const newObject = Object.fromEntries(splitScopes.map((scope) => [scope, value]));
context.program.stateMap(key).set(target, newObject);
return true;
}

Expand All @@ -93,7 +95,7 @@ function setScopedDecoratorData(
return false;
}
if (!Reflect.ownKeys(targetEntry).includes(AllScopes) && !scope) {
context.program.stateMap(key).set(target, { AllScopes: value });
context.program.stateMap(key).set(target, { [AllScopes]: value });
}
return false;
}
Expand Down
28 changes: 28 additions & 0 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,34 @@ describe("typespec-client-generator-core: decorators", () => {
strictEqual(models[0].access, "public");
strictEqual(models[1].access, "public");
});
it("override generic access with language specific access", 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");
});
});

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