Skip to content

Commit

Permalink
[tcgc] Fix generated name (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft authored Mar 19, 2024
1 parent 42f95a3 commit 7bdf1b7
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 315 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/fix_generated_name-2024-2-19-14-44-7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: breaking
packages:
- "@azure-tools/typespec-client-generator-core"
---

SdkUnionType, SdkEnumType, and SdkModelType will now always have a `.name` property. `.generatedName` is now a boolean that expresses whether the `.name` was generated or described in the tsp
8 changes: 4 additions & 4 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export interface SdkDictionaryType extends SdkTypeBase {
export interface SdkEnumType extends SdkTypeBase {
kind: "enum";
name: string;
generatedName?: string;
generatedName: boolean;
valueType: SdkBuiltInType;
values: SdkEnumValueType[];
isFixed: boolean;
Expand Down Expand Up @@ -252,8 +252,8 @@ export interface SdkConstantType extends SdkTypeBase {
}

export interface SdkUnionType extends SdkTypeBase {
name?: string;
generatedName?: string;
name: string;
generatedName: boolean;
kind: "union";
values: SdkType[];
}
Expand All @@ -266,7 +266,7 @@ export interface SdkModelType extends SdkTypeBase {
name: string;
isFormDataType: boolean;
isError: boolean;
generatedName?: string;
generatedName: boolean;
description?: string;
details?: string;
access?: AccessFlags;
Expand Down
16 changes: 15 additions & 1 deletion packages/typespec-client-generator-core/src/internal-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import {
SdkType,
SdkUnionType,
} from "./interfaces.js";
import { getHttpOperationWithCache, isApiVersion } from "./public-utils.js";
import {
getCrossLanguageDefinitionId,
getHttpOperationWithCache,
isApiVersion,
} from "./public-utils.js";

/**
*
Expand Down Expand Up @@ -241,3 +245,13 @@ export function createTCGCContext(program: Program): TCGCContext {
emitterName: "__TCGC_INTERNAL__",
};
}

/**
* Use this if you are trying to create a generated name for something without an original TypeSpec type.
*
* Otherwise, you should use the `getGeneratedName` function.
* @param context
*/
export function createGeneratedName(type: Namespace | Operation, suffix: string): string {
return `${getCrossLanguageDefinitionId(type).split(".").at(-1)}${suffix}`;
}
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 @@ -48,6 +48,7 @@ import {
UsageFlags,
} from "./interfaces.js";
import {
createGeneratedName,
getAvailableApiVersions,
getClientNamespaceStringHelper,
getDocHelper,
Expand Down Expand Up @@ -396,6 +397,8 @@ function getSdkMethodResponse(
kind: "union",
values: allResponseBodies,
nullable,
name: createGeneratedName(operation, "UnionResponse"),
generatedName: true,
};
} else if (responseTypes) {
type = allResponseBodies[0];
Expand Down Expand Up @@ -740,6 +743,7 @@ function getSdkInitializationType<
kind: "model",
properties,
name,
generatedName: true,
access: "public",
usage: UsageFlags.Input,
nullable: false,
Expand Down
17 changes: 11 additions & 6 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import {
isSdkBuiltInKind,
} from "./interfaces.js";
import {
createGeneratedName,
getAvailableApiVersions,
getDocHelper,
getSdkTypeBaseHelper,
Expand Down Expand Up @@ -362,8 +363,8 @@ export function getSdkUnionWithDiagnostics(

return diagnostics.wrap({
...getSdkTypeBaseHelper(context, type, "union"),
name: getLibraryName(context, type),
generatedName: type.name ? undefined : getGeneratedName(context, type),
name: getLibraryName(context, type) || getGeneratedName(context, type),
generatedName: !type.name,
values: nonNullOptions.map((x) =>
diagnostics.pipe(getClientTypeWithDiagnostics(context, x, operation))
),
Expand Down Expand Up @@ -521,8 +522,8 @@ export function getSdkModelWithDiagnostics(
const docWrapper = getDocHelper(context, type);
sdkType = {
...getSdkTypeBaseHelper(context, type, "model"),
name: getLibraryName(context, type),
generatedName: type.name === "" ? getGeneratedName(context, type) : undefined,
name: getLibraryName(context, type) || getGeneratedName(context, type),
generatedName: !type.name,
description: docWrapper.description,
details: docWrapper.details,
properties: [],
Expand Down Expand Up @@ -635,6 +636,7 @@ export function getSdkEnum(context: TCGCContext, type: Enum, operation?: Operati
sdkType = {
...getSdkTypeBaseHelper(context, type, "enum"),
name: getLibraryName(context, type),
generatedName: false,
description: docWrapper.description,
details: docWrapper.details,
valueType: getSdkEnumValueType(context, type.members.values()),
Expand Down Expand Up @@ -684,8 +686,8 @@ function getSdkUnionEnum(context: TCGCContext, type: UnionEnum, operation?: Oper
const docWrapper = getDocHelper(context, union);
sdkType = {
...getSdkTypeBaseHelper(context, type.union, "enum"),
name: getLibraryName(context, type.union),
generatedName: type.union.name ? undefined : getGeneratedName(context, type.union),
name: getLibraryName(context, type.union) || getGeneratedName(context, type.union),
generatedName: !type.union.name,
description: docWrapper.description,
details: docWrapper.details,
valueType:
Expand Down Expand Up @@ -725,6 +727,7 @@ function getKnownValuesEnum(
sdkType = {
...getSdkTypeBaseHelper(context, type, "enum"),
name: getLibraryName(context, type),
generatedName: false,
description: docWrapper.description,
details: docWrapper.details,
valueType: getSdkEnumValueType(context, knownValues.members.values()),
Expand Down Expand Up @@ -917,6 +920,8 @@ function getSdkCredentialType(
kind: "union",
values: credentialTypes,
nullable: false,
name: createGeneratedName(client.service, "CredentialUnion"),
generatedName: false,
};
}
return credentialTypes[0];
Expand Down
Loading

0 comments on commit 7bdf1b7

Please sign in to comment.