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

Get cross language package #350

Merged
merged 3 commits into from
Mar 1, 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 helper function getCrossLanguagePackageId. getCrossLanguagePackageId returns a package id that is consistent across languages, allowing emitters to identify that they are generating from the same service tsp
6 changes: 6 additions & 0 deletions packages/typespec-client-generator-core/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ export const $lib = createTypeSpecLibrary({
default: paramMessage`Unsupported kind ${"kind"}`,
},
},
"multiple-services": {
severity: "warning",
messages: {
default: paramMessage`Multiple services found in definition. Only one service is supported, so we will choose the first one ${"service"}`,
},
},
},
});

Expand Down
26 changes: 25 additions & 1 deletion packages/typespec-client-generator-core/src/public-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
createDiagnosticCollector,
Diagnostic,
getDeprecationDetails,
getDoc,
getEffectiveModelType,
Expand Down Expand Up @@ -36,7 +38,7 @@ import {
listOperationsInOperationGroup,
} from "./decorators.js";
import { parseEmitterName, TCGCContext } from "./internal-utils.js";
import { reportDiagnostic } from "./lib.js";
import { createDiagnostic, reportDiagnostic } from "./lib.js";

/**
* Return the default api version for a versioned service. Will return undefined if one does not exist
Expand Down Expand Up @@ -284,6 +286,28 @@ export function getCrossLanguageDefinitionId(type: {
return retval;
}

/**
* Helper function return the cross langauge package id for a package
*/
export function getCrossLanguagePackageId(context: TCGCContext): [string, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
const services = listServices(context.program);
if (services.length === 0) return diagnostics.wrap("");
const serviceNamespace = getNamespaceFullName(services[0].type);
if (services.length > 1) {
diagnostics.add(
createDiagnostic({
code: "multiple-services",
target: services[0].type,
format: {
service: serviceNamespace,
},
})
);
}
return diagnostics.wrap(serviceNamespace);
}

/**
* Create a name for anonymous model
* @param context
Expand Down
30 changes: 28 additions & 2 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Enum, Interface, Model, Namespace, Operation, UsageFlags } from "@typespec/compiler";
import {
Enum,
Interface,
Model,
Namespace,
Operation,
UsageFlags,
ignoreDiagnostics,
} from "@typespec/compiler";
import { expectDiagnostics } from "@typespec/compiler/testing";
import { deepStrictEqual, ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
Expand All @@ -14,7 +22,7 @@ import {
shouldGenerateProtocol,
} from "../src/decorators.js";
import { SdkOperationGroup } from "../src/interfaces.js";
import { getCrossLanguageDefinitionId } from "../src/public-utils.js";
import { getCrossLanguageDefinitionId, getCrossLanguagePackageId } from "../src/public-utils.js";
import { getAllModels } from "../src/types.js";
import { SdkTestRunner, createSdkContextTestHelper, createSdkTestRunner } from "./test-host.js";

Expand Down Expand Up @@ -307,6 +315,24 @@ describe("typespec-client-generator-core: decorators", () => {
strictEqual(getCrossLanguageDefinitionId(one), "MyClient.SubNamespace.Widgets.one");
});

it("crossLanguagePackageId", async () => {
await runner.compile(`
@client({name: "MyPackageClient"})
@service({})
namespace My.Package.Namespace;

namespace SubNamespace {
interface Widgets {
@test op one(): void;
}
}
`);
strictEqual(
ignoreDiagnostics(getCrossLanguagePackageId(runner.context)),
"My.Package.Namespace"
);
});

it("@operationGroup with scope", async () => {
const testCode = `
@service({
Expand Down
Loading