Skip to content

Commit

Permalink
replace tspNamespace with crossLanguageDefinitionId in enum, mode…
Browse files Browse the repository at this point in the history
…l, union and array types (#1075)

Fixes #1073

Per offline discussion, `crossLanguageDefinitionId` is actually a much
better candidate of telling people the originate of this type, therefore
this should solve all of our requirement and we no longer need a
`tspNamespace` anymore.

---------

Co-authored-by: Chenjie Shi <[email protected]>
  • Loading branch information
ArcturusZhang and tadelesh authored Jun 26, 2024
1 parent 660b587 commit 3f911bb
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

Replace `tspNamespace` with `crossLanguageDefinitionId`.
- Remove `tspNamespace` in `SdkEnumType`, `SdkModelType`, `SdkUnionType`, `SdkArrayType`.
- Add `crossLanguageDefinitionId` to `SdkUnionType` and `SdkArrayType`.
7 changes: 2 additions & 5 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ export interface SdkDurationType extends SdkTypeBase {
export interface SdkArrayType extends SdkTypeBase {
kind: "array";
name: string;
tspNamespace?: string;
valueType: SdkType;
crossLanguageDefinitionId: string;
}

export interface SdkTupleType extends SdkTypeBase {
Expand All @@ -275,7 +275,6 @@ export interface SdkNullableType extends SdkTypeBase {
export interface SdkEnumType extends SdkTypeBase {
kind: "enum";
name: string;
tspNamespace?: string;
isGeneratedName: boolean;
valueType: SdkBuiltInType;
values: SdkEnumValueType[];
Expand All @@ -291,7 +290,6 @@ export interface SdkEnumType extends SdkTypeBase {
export interface SdkEnumValueType extends SdkTypeBase {
kind: "enumvalue";
name: string;
tspNamespace?: string;
value: string | number;
enumType: SdkEnumType;
valueType: SdkBuiltInType;
Expand All @@ -306,10 +304,10 @@ export interface SdkConstantType extends SdkTypeBase {

export interface SdkUnionType extends SdkTypeBase {
name: string;
tspNamespace?: string;
isGeneratedName: boolean;
kind: "union";
values: SdkType[];
crossLanguageDefinitionId: string;
}

export type AccessFlags = "internal" | "public";
Expand All @@ -318,7 +316,6 @@ export interface SdkModelType extends SdkTypeBase {
kind: "model";
properties: SdkModelPropertyType[];
name: string;
tspNamespace?: string;
/**
* @deprecated This property is deprecated. Check the bitwise and value of UsageFlags.MultipartFormData and the `.usage` property on this model.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/typespec-client-generator-core/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ function getSdkMethodResponse<
values: allResponseBodies,
name: createGeneratedName(context, operation, "UnionResponse"),
isGeneratedName: true,
crossLanguageDefinitionId: getCrossLanguageDefinitionId(context, operation),
decorators: {},
};
} else if (responseTypes) {
Expand Down
17 changes: 3 additions & 14 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
IntrinsicType,
Model,
ModelProperty,
Namespace,
NumericLiteral,
Operation,
Scalar,
Expand Down Expand Up @@ -114,13 +113,6 @@ function getEncodeHelper(context: TCGCContext, type: Type, kind: string): string
return kind;
}

function getNamespaceHelper(ns: Namespace | undefined): string | undefined {
if (ns) {
return getNamespaceFullName(ns);
}
return undefined;
}

/**
* Add format info onto an sdk type. Since the format decorator
* decorates the ModelProperty, we add the format info onto the property's internal
Expand Down Expand Up @@ -311,8 +303,8 @@ export function getSdkArrayOrDictWithDiagnostics(
return diagnostics.wrap({
...diagnostics.pipe(getSdkTypeBaseHelper(context, type, "array")),
name: getLibraryName(context, type),
tspNamespace: getNamespaceHelper(type.namespace),
valueType: valueType,
crossLanguageDefinitionId: getCrossLanguageDefinitionId(context, type),
});
}
}
Expand Down Expand Up @@ -383,11 +375,11 @@ export function getSdkUnionWithDiagnostics(
retval = {
...diagnostics.pipe(getSdkTypeBaseHelper(context, type, "union")),
name: getLibraryName(context, type) || getGeneratedName(context, type),
tspNamespace: getNamespaceHelper(type.namespace),
isGeneratedName: !type.name,
values: nonNullOptions.map((x) =>
diagnostics.pipe(getClientTypeWithDiagnostics(context, x, operation))
),
crossLanguageDefinitionId: getCrossLanguageDefinitionId(context, type),
};
}

Expand Down Expand Up @@ -567,7 +559,6 @@ export function getSdkModelWithDiagnostics(
sdkType = {
...diagnostics.pipe(getSdkTypeBaseHelper(context, type, "model")),
name: name,
tspNamespace: getNamespaceHelper(type.namespace),
isGeneratedName: !type.name,
description: docWrapper.description,
details: docWrapper.details,
Expand Down Expand Up @@ -712,7 +703,6 @@ function getSdkEnumWithDiagnostics(
sdkType = {
...diagnostics.pipe(getSdkTypeBaseHelper(context, type, "enum")),
name: getLibraryName(context, type),
tspNamespace: getNamespaceHelper(type.namespace),
isGeneratedName: false,
description: docWrapper.description,
details: docWrapper.details,
Expand Down Expand Up @@ -749,7 +739,6 @@ function getSdkUnionEnumValues(
values.push({
...diagnostics.pipe(getSdkTypeBaseHelper(context, member.type, "enumvalue")),
name: name ? name : `${member.value}`,
tspNamespace: enumType.tspNamespace,
description: docWrapper.description,
details: docWrapper.details,
value: member.value,
Expand Down Expand Up @@ -779,7 +768,6 @@ function getSdkUnionEnumWithDiagnostics(
sdkType = {
...diagnostics.pipe(getSdkTypeBaseHelper(context, type.union, "enum")),
name,
tspNamespace: getNamespaceHelper(type.union.namespace),
isGeneratedName: !type.union.name,
description: docWrapper.description,
details: docWrapper.details,
Expand Down Expand Up @@ -1007,6 +995,7 @@ function getSdkCredentialType(
values: credentialTypes,
name: createGeneratedName(context, client.service, "CredentialUnion"),
isGeneratedName: true,
crossLanguageDefinitionId: getCrossLanguageDefinitionId(context, client.service),
decorators: {},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("typespec-client-generator-core: array types", () => {
strictEqual(method.response.kind, "method");
strictEqual(method.response.type?.kind, "array");
strictEqual(method.response.type?.name, "TestArray");
strictEqual(method.response.type?.tspNamespace, "TestClient");
strictEqual(method.response.type?.crossLanguageDefinitionId, "TestClient.TestArray");
strictEqual(method.response.type?.valueType.kind, "model");
strictEqual(method.response.type?.valueType.name, "TestModel");
});
Expand Down Expand Up @@ -62,7 +62,7 @@ describe("typespec-client-generator-core: array types", () => {
const property = model.properties[0];
strictEqual(property.type.kind, "array");
strictEqual(property.type.name, "EmbeddingVector");
strictEqual(property.type.tspNamespace, "Azure.Core");
strictEqual(property.type.crossLanguageDefinitionId, "Azure.Core.EmbeddingVector");
strictEqual(property.type.valueType.kind, "int32");
});

Expand Down Expand Up @@ -91,7 +91,7 @@ describe("typespec-client-generator-core: array types", () => {
const property = model.properties[0];
strictEqual(property.type.kind, "array");
strictEqual(property.type.name, "EmbeddingVector");
strictEqual(property.type.tspNamespace, "Azure.Core");
strictEqual(property.type.crossLanguageDefinitionId, "Azure.Core.EmbeddingVector");
strictEqual(property.type.valueType.kind, "int32");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("typespec-client-generator-core: enum types", () => {
const sdkType = runner.context.experimental_sdkPackage.enums[0];
strictEqual(sdkType.isFixed, true);
strictEqual(sdkType.name, "DaysOfWeekExtensibleEnum");
strictEqual(sdkType.tspNamespace, "TestService");
strictEqual(sdkType.crossLanguageDefinitionId, "TestService.DaysOfWeekExtensibleEnum");
strictEqual(sdkType.valueType.kind, "string");
strictEqual(sdkType.usage & UsageFlags.ApiVersionEnum, 0); // not a versioning enum
strictEqual(sdkType.isUnionAsEnum, false);
Expand Down Expand Up @@ -88,7 +88,7 @@ describe("typespec-client-generator-core: enum types", () => {
const sdkType = runner.context.experimental_sdkPackage.enums[0];
strictEqual(sdkType.isFixed, true);
strictEqual(sdkType.name, "Integers");
strictEqual(sdkType.tspNamespace, "TestService");
strictEqual(sdkType.crossLanguageDefinitionId, "TestService.Integers");
strictEqual(sdkType.valueType.kind, "int32");
const values = sdkType.values;
strictEqual(values.length, 5);
Expand Down Expand Up @@ -123,7 +123,7 @@ describe("typespec-client-generator-core: enum types", () => {
ok(sdkType);
strictEqual(sdkType.isFixed, true);
strictEqual(sdkType.name, "Floats");
strictEqual(sdkType.tspNamespace, "TestService");
strictEqual(sdkType.crossLanguageDefinitionId, "TestService.Floats");
strictEqual(sdkType.valueType.kind, "float32");
const values = sdkType.values;
strictEqual(values.length, 3);
Expand Down Expand Up @@ -158,7 +158,7 @@ describe("typespec-client-generator-core: enum types", () => {
const sdkType = runner.context.experimental_sdkPackage.enums[0];
strictEqual(sdkType.isFixed, false);
strictEqual(sdkType.name, "Floats");
strictEqual(sdkType.tspNamespace, "TestService");
strictEqual(sdkType.crossLanguageDefinitionId, "TestService.Floats");
strictEqual(sdkType.valueType.kind, "float");
const values = sdkType.values;
strictEqual(values.length, 3);
Expand Down Expand Up @@ -199,7 +199,7 @@ describe("typespec-client-generator-core: enum types", () => {
ok(sdkType);
strictEqual(sdkType.isFixed, false);
strictEqual(sdkType.name, "ExtendedEnum");
strictEqual(sdkType.tspNamespace, "TestService");
strictEqual(sdkType.crossLanguageDefinitionId, "TestService.ExtendedEnum");
strictEqual(sdkType.valueType.kind, "int32");
const values = sdkType.values;
strictEqual(values.length, 3);
Expand Down Expand Up @@ -245,7 +245,7 @@ describe("typespec-client-generator-core: enum types", () => {
const sdkType = runnerWithCore.context.experimental_sdkPackage.enums[0];
strictEqual(sdkType.isFixed, true);
strictEqual(sdkType.name, "DaysOfWeekFixedEnum");
strictEqual(sdkType.tspNamespace, "My.Service");
strictEqual(sdkType.crossLanguageDefinitionId, "My.Service.DaysOfWeekFixedEnum");
strictEqual(sdkType.valueType.kind, "string");
const values = sdkType.values;
strictEqual(values.length, 7);
Expand Down Expand Up @@ -422,7 +422,7 @@ describe("typespec-client-generator-core: enum types", () => {
const enumType = getClientType(runner.context, TestUnion);
strictEqual(enumType.kind, "enum");
strictEqual(enumType.name, "TestUnionRename");
strictEqual(enumType.tspNamespace, "N");
strictEqual(enumType.crossLanguageDefinitionId, "N.TestUnion");
strictEqual(enumType.isUnionAsEnum, true);
strictEqual(enumType.values[0].name, "ARename");
strictEqual(enumType.values[1].name, "BRename");
Expand Down Expand Up @@ -465,7 +465,7 @@ describe("typespec-client-generator-core: enum types", () => {
const enumType = nullableType.type;
strictEqual(enumType.kind, "enum");
strictEqual(enumType.name, "Test");
strictEqual(enumType.tspNamespace, "N");
strictEqual(enumType.crossLanguageDefinitionId, "N.Test");
strictEqual(enumType.isUnionAsEnum, true);
const values = enumType.values;
strictEqual(values.length, 4);
Expand Down Expand Up @@ -518,14 +518,14 @@ describe("typespec-client-generator-core: enum types", () => {

strictEqual(unionType.kind, "union");
strictEqual(unionType.name, "Test");
strictEqual(unionType.tspNamespace, "N");
strictEqual(unionType.crossLanguageDefinitionId, "N.Test");

const values = unionType.values;
strictEqual(values.length, 3);
const a = values[0] as SdkEnumType;
strictEqual(a.kind, "enum");
strictEqual(a.name, "A");
strictEqual(a.tspNamespace, "N");
strictEqual(a.crossLanguageDefinitionId, "N.A");
strictEqual(a.isUnionAsEnum, true);
strictEqual(a.values[0].name, "A1");
strictEqual(a.values[0].value, "A1");
Expand All @@ -535,15 +535,15 @@ describe("typespec-client-generator-core: enum types", () => {
const b = values[1] as SdkEnumType;
strictEqual(b.kind, "enum");
strictEqual(b.name, "B");
strictEqual(b.tspNamespace, "N");
strictEqual(b.crossLanguageDefinitionId, "N.B");
strictEqual(b.isUnionAsEnum, true);
strictEqual(b.values[0].name, "B");
strictEqual(b.values[0].value, "B");

const c = values[2] as SdkEnumType;
strictEqual(c.kind, "enum");
strictEqual(c.name, "C");
strictEqual(c.tspNamespace, "N");
strictEqual(c.crossLanguageDefinitionId, "N.C");
strictEqual(c.isUnionAsEnum, false);
strictEqual(c.values[0].name, "C");
strictEqual(c.values[0].value, "C");
Expand Down Expand Up @@ -575,7 +575,7 @@ describe("typespec-client-generator-core: enum types", () => {
const modelType = getClientType(runner.context, Test) as SdkModelType;
const enumType = modelType.properties[0].type as SdkEnumType;
strictEqual(enumType.name, "TestColor");
strictEqual(enumType.tspNamespace, undefined); // implicitly defined union does not have a namespace
strictEqual(enumType.crossLanguageDefinitionId, "Test.color.anonymous");
strictEqual(enumType.isGeneratedName, true);
strictEqual(enumType.isUnionAsEnum, true);
// no cross language def id bc it's not a defined object in tsp
Expand Down Expand Up @@ -625,19 +625,19 @@ describe("typespec-client-generator-core: enum types", () => {
const modelType = getClientType(runner.context, Test) as SdkModelType;
const unionType = modelType.properties[0].type as SdkUnionType;
strictEqual(unionType.name, "TestColor");
strictEqual(unionType.tspNamespace, undefined); // implicitly defined union does not have a namespace
strictEqual(unionType.crossLanguageDefinitionId, "Test.color.anonymous");
strictEqual(unionType.isGeneratedName, true);
const values = unionType.values;
const lr = values[0] as SdkEnumType;
strictEqual(lr.name, "LR");
strictEqual(lr.tspNamespace, "N");
strictEqual(lr.crossLanguageDefinitionId, "N.LR");
strictEqual(lr.isUnionAsEnum, false);
strictEqual(lr.values[0].name, "left");
strictEqual(lr.values[1].name, "right");
strictEqual(lr.isFixed, true);
const ud = values[1] as SdkEnumType;
strictEqual(ud.name, "UD");
strictEqual(ud.tspNamespace, "N");
strictEqual(ud.crossLanguageDefinitionId, "N.UD");
strictEqual(ud.isUnionAsEnum, false);
strictEqual(ud.values[0].name, "up");
strictEqual(ud.values[1].name, "down");
Expand All @@ -660,7 +660,7 @@ describe("typespec-client-generator-core: enum types", () => {
const enums = runner.context.experimental_sdkPackage.enums;
strictEqual(enums.length, 1);
strictEqual(enums[0].name, "Versions");
strictEqual(enums[0].tspNamespace, "DemoService");
strictEqual(enums[0].crossLanguageDefinitionId, "DemoService.Versions");
strictEqual(enums[0].usage, UsageFlags.ApiVersionEnum);
deepStrictEqual(
enums[0].values.map((x) => x.value),
Expand Down Expand Up @@ -689,7 +689,7 @@ describe("typespec-client-generator-core: enum types", () => {
const enums = runnerWithVersion.context.experimental_sdkPackage.enums;
strictEqual(enums.length, 1);
strictEqual(enums[0].name, "Versions");
strictEqual(enums[0].tspNamespace, "DemoService");
strictEqual(enums[0].crossLanguageDefinitionId, "DemoService.Versions");
strictEqual(enums[0].usage, UsageFlags.ApiVersionEnum);
deepStrictEqual(
enums[0].values.map((x) => x.value),
Expand Down Expand Up @@ -718,7 +718,7 @@ describe("typespec-client-generator-core: enum types", () => {
const enums = runnerWithVersion.context.experimental_sdkPackage.enums;
strictEqual(enums.length, 1);
strictEqual(enums[0].name, "Versions");
strictEqual(enums[0].tspNamespace, "DemoService");
strictEqual(enums[0].crossLanguageDefinitionId, "DemoService.Versions");
strictEqual(enums[0].usage, UsageFlags.ApiVersionEnum);
deepStrictEqual(
enums[0].values.map((x) => x.value),
Expand Down Expand Up @@ -747,7 +747,7 @@ describe("typespec-client-generator-core: enum types", () => {
const enums = runnerWithVersion.context.experimental_sdkPackage.enums;
strictEqual(enums.length, 1);
strictEqual(enums[0].name, "Versions");
strictEqual(enums[0].tspNamespace, "DemoService");
strictEqual(enums[0].crossLanguageDefinitionId, "DemoService.Versions");
strictEqual(enums[0].usage, UsageFlags.ApiVersionEnum);
deepStrictEqual(
enums[0].values.map((x) => x.value),
Expand Down Expand Up @@ -781,10 +781,10 @@ describe("typespec-client-generator-core: enum types", () => {
const enums = runner.context.experimental_sdkPackage.enums;
strictEqual(enums.length, 2);
strictEqual(enums[0].name, "LR");
strictEqual(enums[0].tspNamespace, "N");
strictEqual(enums[0].crossLanguageDefinitionId, "N.LR");
strictEqual(enums[0].usage, UsageFlags.Input);
strictEqual(enums[1].name, "UD");
strictEqual(enums[1].tspNamespace, "N");
strictEqual(enums[1].crossLanguageDefinitionId, "N.UD");
strictEqual(enums[1].usage, UsageFlags.Input);
});
});
Loading

0 comments on commit 3f911bb

Please sign in to comment.