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

add a cached getHttpOperation helper function #395

Merged
merged 3 commits into from
Mar 11, 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
7 changes: 7 additions & 0 deletions .chronus/changes/cached-operation-2024-2-11-17-52-48.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

add a cached getHttpOperation helper function
10 changes: 5 additions & 5 deletions packages/typespec-client-generator-core/src/internal-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import {
getDoc,
getNamespaceFullName,
getSummary,
ignoreDiagnostics,
} from "@typespec/compiler";
import { HttpOperation, getHttpOperation } from "@typespec/http";
import { HttpOperation } from "@typespec/http";
import { getAddedOnVersions, getRemovedOnVersions, getVersions } from "@typespec/versioning";
import {
SdkBuiltInKinds,
Expand All @@ -22,7 +21,7 @@ import {
SdkType,
SdkUnionType,
} from "./interfaces.js";
import { isApiVersion } from "./public-utils.js";
import { getHttpOperationWithCache, isApiVersion } from "./public-utils.js";

/**
*
Expand Down Expand Up @@ -206,7 +205,7 @@ export function isAcceptHeader(param: SdkModelPropertyType): boolean {

export function isMultipartOperation(context: TCGCContext, operation?: Operation): boolean {
if (!operation) return false;
const httpOperation = ignoreDiagnostics(getHttpOperation(context.program, operation));
const httpOperation = getHttpOperationWithCache(context, operation);
const httpBody = httpOperation.parameters.body;
if (httpBody && httpBody.type.kind === "Model") {
return httpBody.contentTypes.some((x) => x.startsWith("multipart/"));
Expand All @@ -215,7 +214,7 @@ export function isMultipartOperation(context: TCGCContext, operation?: Operation
}

export function isHttpOperation(context: TCGCContext, obj: any): obj is HttpOperation {
return !!obj && obj.kind === "Operation" && !getHttpOperation(context.program, obj)[1].length;
return obj?.kind === "Operation" && getHttpOperationWithCache(context, obj) !== undefined;
}
export interface TCGCContext {
program: Program;
Expand All @@ -227,6 +226,7 @@ export interface TCGCContext {
arm?: boolean;
modelsMap?: Map<Type, SdkModelType | SdkEnumType>;
operationModelsMap?: Map<Operation, Map<Type, SdkModelType | SdkEnumType>>;
httpOperationCache?: Map<Operation, HttpOperation>;
generatedNames?: Set<string>;
unionsMap?: Map<Union, SdkUnionType>;
__api_version_parameter?: SdkParameter;
Expand Down
11 changes: 3 additions & 8 deletions packages/typespec-client-generator-core/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ import {
getService,
isErrorModel,
} from "@typespec/compiler";
import {
HttpOperation,
getHeaderFieldName,
getHttpOperation,
getServers,
isContentTypeHeader,
} from "@typespec/http";
import { HttpOperation, getHeaderFieldName, getServers, isContentTypeHeader } from "@typespec/http";
import { resolveVersions } from "@typespec/versioning";
import {
getAccess,
Expand Down Expand Up @@ -65,6 +59,7 @@ import {
import {
getClientNamespaceString,
getDefaultApiVersion,
getHttpOperationWithCache,
getLibraryName,
getPropertyNames,
} from "./public-utils.js";
Expand Down Expand Up @@ -276,7 +271,7 @@ function getSdkServiceOperation<
methodParameters: SdkMethodParameter[]
): [TServiceOperation, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
const httpOperation = diagnostics.pipe(getHttpOperation(context.program, operation));
const httpOperation = getHttpOperationWithCache(context, operation);
if (httpOperation) {
const sdkHttpOperation = diagnostics.pipe(
getSdkHttpOperation(context, httpOperation, methodParameters)
Expand Down
19 changes: 17 additions & 2 deletions packages/typespec-client-generator-core/src/public-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
resolveEncodedName,
} from "@typespec/compiler";
import {
HttpOperation,
HttpOperationParameter,
getHeaderFieldName,
getHttpOperation,
Expand Down Expand Up @@ -301,13 +302,12 @@ function getContextPath(
root: Operation | Model,
typeToFind: Model | Union
): ContextNode[] {
const program = context.program;
// use visited set to avoid cycle model reference
const visited: Set<Type> = new Set<Type>();
let result: ContextNode[];

if (root.kind === "Operation") {
const httpOperation = ignoreDiagnostics(getHttpOperation(program, root));
const httpOperation = getHttpOperationWithCache(context, root);

if (httpOperation.parameters.body) {
visited.clear();
Expand Down Expand Up @@ -495,3 +495,18 @@ export function isErrorOrChildOfError(context: TCGCContext, model: Model): boole
}
return false;
}

export function getHttpOperationWithCache(
context: TCGCContext,
operation: Operation
): HttpOperation {
if (context.httpOperationCache === undefined) {
context.httpOperationCache = new Map<Operation, HttpOperation>();
}
if (context.httpOperationCache.has(operation)) {
return context.httpOperationCache.get(operation)!;
}
const httpOperation = ignoreDiagnostics(getHttpOperation(context.program, operation));
context.httpOperationCache.set(operation, httpOperation);
return httpOperation;
}
8 changes: 4 additions & 4 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import {
getAuthentication,
getHeaderFieldName,
getHeaderFieldOptions,
getHttpOperation,
getPathParamName,
getQueryParamName,
getQueryParamOptions,
Expand Down Expand Up @@ -103,6 +102,7 @@ import {
getCrossLanguageDefinitionId,
getEffectivePayloadType,
getGeneratedName,
getHttpOperationWithCache,
getLibraryName,
getPropertyNames,
} from "./public-utils.js";
Expand Down Expand Up @@ -486,7 +486,7 @@ function addDiscriminatorToModelType(
function isOperationBodyType(context: TCGCContext, type: Model, operation?: Operation): boolean {
if (!isHttpOperation(context, operation)) return false;
const httpBody = operation
? ignoreDiagnostics(getHttpOperation(context.program, operation)).parameters.body
? getHttpOperationWithCache(context, operation).parameters.body
: undefined;
return (
!!httpBody &&
Expand Down Expand Up @@ -1019,7 +1019,7 @@ export function getSdkModelPropertyType(
// I'm a body model property
let operationIsMultipart = false;
if (options.operation) {
const httpOperation = ignoreDiagnostics(getHttpOperation(context.program, options.operation));
const httpOperation = getHttpOperationWithCache(context, options.operation);
operationIsMultipart = Boolean(
httpOperation && httpOperation.parameters.body?.contentTypes.includes("multipart/form-data")
);
Expand Down Expand Up @@ -1232,7 +1232,7 @@ function updateTypesFromOperation(
): [void, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
const program = context.program;
const httpOperation = diagnostics.pipe(getHttpOperation(program, operation));
const httpOperation = getHttpOperationWithCache(context, operation);
const generateConvenient = shouldGenerateConvenient(context, operation);
for (const param of operation.parameters.properties.values()) {
const paramTypes = diagnostics.pipe(checkAndGetClientType(context, param.type, operation));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1243,10 +1243,7 @@ describe("typespec-client-generator-core: public-utils", () => {
);
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
// we could not identify the anonymous model from alias spread
// bc each time we try to get body, we will get a new type from compiler
// so we will keep the empty name
ok(models.find((x) => (x as SdkModelType).generatedName === ""));
ok(models.find((x) => (x as SdkModelType).generatedName === "TestRequest"));
});

it("anonymous model for body parameter", async () => {
Expand Down
Loading