Skip to content

Commit

Permalink
filter model generation by labeling
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh committed Oct 23, 2023
1 parent 1a7e93f commit 0287500
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { plural } from "pluralize";
import { generateDecorators } from "../utils/decorators";
import { generateDocs } from "../utils/docs";
import { getModelPropertiesDeclarations } from "../utils/model-generation";
import { generateOperation } from "./generate-operations";

export function generateArmResource(resource: TspArmResource): string {
let definitions: string[] = [];
Expand Down Expand Up @@ -38,13 +39,16 @@ function generateArmResourceOperation(resource: TspArmResource): string {
const definitions: string[] = [];
definitions.push("@armResourceOperations");
definitions.push(`interface ${plural(resource.name)} {`);
for (const operation of resource.operations) {
for (const operation of resource.resourceOperations) {
for (const fixme of operation.fixMe ?? []) {
definitions.push(fixme);
}
definitions.push(generateDocs(operation));
definitions.push(`${operation.name} is ${operation.kind}<${operation.templateParameters.join()}>`);
}
for (const operation of resource.normalOperations) {
definitions.push(generateOperation(operation));
}
definitions.push("}");

return definitions.join("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CadlOperation, CadlOperationGroup, CadlParameter } from "../interfaces"
import { generateDocs, generateSummary } from "../utils/docs";
import { generateParameter } from "./generate-parameter";

export function generateOperation(operation: CadlOperation, operationGroup: CadlOperationGroup) {
export function generateOperation(operation: CadlOperation, operationGroup?: CadlOperationGroup) {
const doc = generateDocs(operation);
const summary = generateSummary(operation);
const { verb, name, route, responses, parameters } = operation;
Expand Down Expand Up @@ -36,7 +36,7 @@ export function generateOperation(operation: CadlOperation, operationGroup: Cadl
);
const parametersString = !resourceParameters ? "" : `, { parameters: ${resourceParameters}}`;
statements.push(
`${operationGroup.name ? "" : "op "}`,
`${operationGroup?.name ? "" : "op "}`,
`${name} is Azure.Core.${resource.kind}<${resource.response.name} ${parametersString}>;\n\n\n`,
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/extensions/openapi-to-cadl/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ export interface TspArmResourceNonListOperation extends TspArmResourceOperationB

export interface TspArmResourceListOperation extends TspArmResourceOperationBase {
kind: "ArmResourceListByParent" | "ArmListBySubscription" | "ArmResourceListAtScope";
resultSchemaName?: string;
}

export type MSIType = "ManagedServiceIdentity" | "ManagedSystemAssignedIdentity";
export interface TspArmResource extends CadlObject {
resourceKind: ArmResourceKind;
propertiesModelName: string;
resourceParent?: TspArmResource;
operations: TspArmResourceOperation[];
resourceOperations: TspArmResourceOperation[];
normalOperations: CadlOperation[];
msiType?: MSIType;
}
14 changes: 6 additions & 8 deletions packages/extensions/openapi-to-cadl/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
ArmResourceSchema,
filterResourceRelatedObjects,
isResourceSchema,
isResourceUpdateSchema,
} from "./utils/resource-discovery";
import { isChoiceSchema } from "./utils/schemas";

Expand Down Expand Up @@ -52,14 +51,13 @@ function transformModel(codeModel: CodeModel): CadlProgram {

const { isArm } = getOptions();

const armResources =
// objects need to be converted first because they are used in operation convertion
const cadlObjects = (codeModel.schemas.objects ?? []).map((o) => transformObject(o, codeModel));

const armResources = isArm ?
codeModel.schemas.objects
?.filter((o) => isResourceSchema(o))
.map((o) => transformTspArmResource(codeModel, o as ArmResourceSchema)) ?? [];

const cadlObjects = (
(isArm ? filterResourceRelatedObjects(codeModel.schemas.objects, armResources) : codeModel.schemas.objects) ?? []
).map((o) => transformObject(o, codeModel));
.map((o) => transformTspArmResource(codeModel, o as ArmResourceSchema)) ?? [] : [];

const serviceInformation = transformServiceInformation(codeModel);

Expand All @@ -76,7 +74,7 @@ function transformModel(codeModel: CodeModel): CadlProgram {
serviceInformation,
models: {
enums: caldEnums,
objects: cadlObjects,
objects: isArm ? filterResourceRelatedObjects(cadlObjects) : cadlObjects,
armResources,
},
operationGroups: cadlOperationGroups,
Expand Down
Loading

0 comments on commit 0287500

Please sign in to comment.