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

Prototype for azure emitter #4769

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
77 changes: 74 additions & 3 deletions packages/http-client-csharp/emitter/src/emitter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

import { createSdkContext, UsageFlags } from "@azure-tools/typespec-client-generator-core";
import { createSdkContext, SdkClientType, SdkHttpOperation, SdkServiceMethod, UsageFlags } from "@azure-tools/typespec-client-generator-core";
import {
EmitContext,
getDirectoryPath,
Expand All @@ -23,6 +23,9 @@
import { NetEmitterOptions, resolveOptions, resolveOutputFolder } from "./options.js";
import { defaultSDKContextOptions } from "./sdk-context-options.js";
import { Configuration } from "./type/configuration.js";
import { CodeModel } from "./type/code-model.js";
import { InputClient } from "./type/input-client.js";
import { InputOperation } from "./type/input-operation.js";

/**
* Look for the project root by looking up until a `package.json` is found.
Expand All @@ -44,7 +47,75 @@
}
}

export async function $onEmit(context: EmitContext<NetEmitterOptions>) {
/** Start: all these in azure emitter */
interface AzureCodeModel extends CodeModel {

Check warning on line 51 in packages/http-client-csharp/emitter/src/emitter.ts

View workflow job for this annotation

GitHub Actions / Lint

'AzureCodeModel' is defined but never used. Allowed unused vars must match /^_/u
AzureStuff: string;
}

interface AzureInputClient extends InputClient {
AzureStuff: string;
}

interface AzureInputOperation extends InputOperation {
AzureStuff: string;
}

interface ResoureceMetadata {
models?: {
operations: string[]
}
}

type AzureModelType = {
client: AzureInputClient;
operation: AzureInputOperation;
resources: ResoureceMetadata;
}

const azureHook: Hook<AzureModelType> = {
emitClient: (client: SdkClientType<SdkHttpOperation>, unbrandingClient: InputClient) => {
// Get something from TCGC client raw.
const azureStuff = "hello azure";
return {
...unbrandingClient,
AzureStuff: azureStuff
};
},
emitOperation: (method: SdkServiceMethod<SdkHttpOperation>, unbrandingOperation: InputOperation) => {
// Get something from TCGC operation raw.
const azureStuff = "hello azure";
return {
...unbrandingOperation,
AzureStuff: azureStuff
};
},
emitResources: () => {
// Get data from arm
return { "models": { "operations": []} };
}
}

export async function $onEmitInAzureEmitter(context: EmitContext<NetEmitterOptions>) {
$onEmit<AzureModelType>({
...context,
"hook": azureHook
});
}
/** End: all these in azure emitter */


export type CodeModelType = Record<"client"| "operation" | "resources", any>; // I think "resources" should be renamed to something like "others", just telling unbranding emitter just append these data.

export interface Hook<T extends CodeModelType> {
emitClient?: (client: SdkClientType<SdkHttpOperation>, unbrandingClient: InputClient) => T["client"];
emitOperation?: (method: SdkServiceMethod<SdkHttpOperation>, unbrandingOperation: InputOperation) => T["operation"];
emitResources?: () => T["resources"];
}

export type ContextType<T extends CodeModelType> = EmitContext<NetEmitterOptions> & {"hook"?: Hook<T>};

export async function $onEmit<T extends CodeModelType>(context: ContextType<T>) {
context.hook = azureHook as Hook<T>; // Ignore this line. Test purpose
const program: Program = context.program;
const options = resolveOptions(context);
const outputFolder = resolveOutputFolder(context);
Expand All @@ -59,7 +130,7 @@
"@typespec/http-client-csharp",
defaultSDKContextOptions,
);
const root = createModel(sdkContext);
const root = createModel<T>(sdkContext, context.hook); // context.hook is already in sdkContext. We should change createSdkContext signature to somehow accept hook type
if (
context.program.diagnostics.length > 0 &&
context.program.diagnostics.filter((digs) => digs.severity === "error").length > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import { Logger } from "./logger.js";
import { navigateModels } from "./model.js";
import { fromSdkServiceMethod, getParameterDefaultValue } from "./operation-converter.js";
import { processServiceAuthentication } from "./service-authentication.js";
import { CodeModelType, Hook } from "../emitter.js";

export function createModel(sdkContext: SdkContext<NetEmitterOptions>): CodeModel {
export function createModel<T extends CodeModelType>(sdkContext: SdkContext<NetEmitterOptions>, hook?: Hook<T>): CodeModel {
const sdkPackage = sdkContext.sdkPackage;

const sdkTypeMap: SdkTypeMap = {
Expand All @@ -49,6 +50,7 @@ export function createModel(sdkContext: SdkContext<NetEmitterOptions>): CodeMode
sdkPackage.clients.filter((c) => c.initialization.access === "public"),
inputClients,
[],
hook
);

const clientModel: CodeModel = {
Expand All @@ -61,30 +63,31 @@ export function createModel(sdkContext: SdkContext<NetEmitterOptions>): CodeMode
};
return clientModel;

function fromSdkClients(
function fromSdkClients<T extends CodeModelType>(
clients: SdkClientType<SdkHttpOperation>[],
inputClients: InputClient[],
parentClientNames: string[],
hook?: Hook<T>
) {
for (const client of clients) {
const inputClient = emitClient(client, parentClientNames);
const inputClient = emitClient(client, parentClientNames, hook);
inputClients.push(inputClient);
const subClients = client.methods
.filter((m) => m.kind === "clientaccessor")
.map((m) => m.response as SdkClientType<SdkHttpOperation>);
parentClientNames.push(inputClient.Name);
fromSdkClients(subClients, inputClients, parentClientNames);
fromSdkClients(subClients, inputClients, parentClientNames, hook);
parentClientNames.pop();
}
}

function emitClient(client: SdkClientType<SdkHttpOperation>, parentNames: string[]): InputClient {
function emitClient<T extends CodeModelType>(client: SdkClientType<SdkHttpOperation>, parentNames: string[], hook?: Hook<T>): InputClient {
const endpointParameter = client.initialization.properties.find(
(p) => p.kind === "endpoint",
) as SdkEndpointParameter;
const uri = getMethodUri(endpointParameter);
const clientParameters = fromSdkEndpointParameter(endpointParameter);
return {
const unbrandingClient = {
Name: getClientName(client, parentNames),
Description: client.description,
Operations: client.methods
Expand All @@ -97,13 +100,15 @@ export function createModel(sdkContext: SdkContext<NetEmitterOptions>): CodeMode
rootApiVersions,
sdkContext,
sdkTypeMap,
hook
),
),
Protocol: {},
Parent: parentNames.length > 0 ? parentNames[parentNames.length - 1] : undefined,
Parameters: clientParameters,
Decorators: client.decorators,
};
return hook?.emitClient ? hook.emitClient(client, unbrandingClient) : unbrandingClient;
}

function getClientName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ import { fromSdkHttpExamples } from "./example-converter.js";
import { Logger } from "./logger.js";
import { getInputType } from "./model.js";
import { capitalize, isSdkPathParameter } from "./utils.js";
import { CodeModelType, Hook } from "../emitter.js";

export function fromSdkServiceMethod(
export function fromSdkServiceMethod<T extends CodeModelType>(
method: SdkServiceMethod<SdkHttpOperation>,
uri: string,
clientParameters: InputParameter[],
rootApiVersions: string[],
sdkContext: SdkContext<NetEmitterOptions>,
typeMap: SdkTypeMap,
hook?: Hook<T>
): InputOperation {
let generateConvenience = shouldGenerateConvenient(sdkContext, method.operation.__raw.operation);
if (method.operation.verb === "patch" && generateConvenience) {
Expand All @@ -69,7 +71,7 @@ export function fromSdkServiceMethod(
sdkContext,
typeMap,
);
return {
const unbrandingOperation = {
Name: method.name,
ResourceName:
getResourceOperation(sdkContext.program, method.operation.__raw.operation)?.resourceType
Expand Down Expand Up @@ -106,6 +108,7 @@ export function fromSdkServiceMethod(
)
: undefined,
};
return hook?.emitOperation ? hook.emitOperation(method, unbrandingOperation) : unbrandingOperation;
}

export function getParameterDefaultValue(
Expand Down
Loading
Loading