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

[tcgc] add @clientInitialization decorator #1398

Merged
merged 30 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
35d7068
add clientInitialization decorator
Aug 16, 2024
70c1e00
remove onClient
Aug 19, 2024
d0b9fee
add decorator and initial test
Aug 19, 2024
48a726b
base test passing
Aug 19, 2024
420ca61
start working on subclients
Aug 19, 2024
78c701b
format
Aug 19, 2024
11668ac
add changeset
Aug 19, 2024
c40d9a3
fix build
Aug 19, 2024
c1b0574
pnpm format
Aug 19, 2024
d193e66
Merge branch 'main' of https://github.com/Azure/typespec-azure into a…
Aug 20, 2024
54a72b7
directly return client param in cases of client initialization
Aug 20, 2024
6a4667b
add more tests
Aug 20, 2024
b8dca8f
format
Aug 20, 2024
ea89417
Merge branch 'main' of https://github.com/Azure/typespec-azure into a…
Aug 21, 2024
3ee9d50
Merge branch 'main' of https://github.com/Azure/typespec-azure into a…
Aug 27, 2024
0f5fe39
add tests for og groups with parent clients
Aug 27, 2024
15ced83
Merge branch 'main' of https://github.com/Azure/typespec-azure into a…
Aug 28, 2024
61ab4f1
format
Aug 28, 2024
29eb08d
only throw error if explicitly decorated with @operationGroup
Aug 28, 2024
9137d21
add alias decorator
Aug 28, 2024
a04c95b
introduce @paramAlias
Aug 29, 2024
12dc1d1
format
Aug 29, 2024
1b3da09
fix initialization access for inferred ogs
Aug 30, 2024
c6041e6
fix initialization access for inferred ogs
Aug 30, 2024
ba1e2b3
Merge branch 'main' of https://github.com/Azure/typespec-azure into a…
Aug 30, 2024
657b17d
format
Aug 30, 2024
9cf543b
Merge branch 'main' of https://github.com/Azure/typespec-azure into a…
Sep 3, 2024
ad2d001
remove check for og
Sep 3, 2024
e456785
add test for client reorganization
Sep 3, 2024
8629642
rebuild
Sep 3, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

add `@clientInitialization` decorator
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,45 @@ interface MyInterface {}
interface MyInterface {}
```

### `@clientInitialization` {#@Azure.ClientGenerator.Core.clientInitialization}

Client parameters you would like to add to the client. By default, we apply endpoint, credential, and api-version parameters. If you add clientInitialization, we will append those to the default list of parameters.

```typespec
@Azure.ClientGenerator.Core.clientInitialization(options: Model, scope?: valueof string)
```

#### Target

`Namespace | Interface`

#### Parameters

| Name | Type | Description |
| ------- | ---------------- | ------------------------------------------------------------------------------------------------------------- |
| options | `Model` | |
| scope | `valueof string` | The language scope you want this decorator to apply to. If not specified, will apply to all language emitters |

#### Examples

```typespec
// main.tsp
namespace MyService;

op upload(blobName: string): void;
op download(blobName: string): void;

// client.tsp
namespace MyCustomizations;
model MyServiceClientOptions {
blobName: string;
}

@@clientInitialization(MyService, MyServiceClientOptions)
// The generated client will have `blobName` on it. We will also
// elevate the existing `blobName` parameter to the client level.
```

### `@clientName` {#@Azure.ClientGenerator.Core.clientName}

Changes the name of a method, parameter, property, or model generated in the client SDK
Expand Down Expand Up @@ -378,6 +417,46 @@ op myOperationCustomization(params: Params): void;
// method signature is now `op myOperation(params: Params)` just for csharp
```

### `@paramAlias` {#@Azure.ClientGenerator.Core.paramAlias}

Alias the name of a client parameter to a different name. This permits you to have a different name for the parameter in client initialization then on individual methods and still refer to the same parameter.

```typespec
@Azure.ClientGenerator.Core.paramAlias(paramAlias: valueof string, scope?: valueof string)
```

#### Target

`ModelProperty`

#### Parameters

| Name | Type | Description |
| ---------- | ---------------- | ------------------------------------------------------------------------------------------------------------- |
| paramAlias | `valueof string` | |
| scope | `valueof string` | The language scope you want this decorator to apply to. If not specified, will apply to all language emitters |

#### Examples

```typespec
// main.tsp
namespace MyService;

op upload(blobName: string): void;

// client.tsp
namespace MyCustomizations;
model MyServiceClientOptions {
blob: string;
}

@@clientInitialization(MyService, MyServiceClientOptions)
@@paramAlias(MyServiceClientOptions.blob, "blobName")
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved

// The generated client will have `blobName` on it. We will also
// elevate the existing `blob` parameter to the client level.
```

### `@protocolAPI` {#@Azure.ClientGenerator.Core.protocolAPI}

Whether you want to generate an operation as a protocol operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ npm install --save-peer @azure-tools/typespec-client-generator-core

- [`@access`](./decorators.md#@Azure.ClientGenerator.Core.access)
- [`@client`](./decorators.md#@Azure.ClientGenerator.Core.client)
- [`@clientInitialization`](./decorators.md#@Azure.ClientGenerator.Core.clientInitialization)
- [`@clientName`](./decorators.md#@Azure.ClientGenerator.Core.clientName)
- [`@convenientAPI`](./decorators.md#@Azure.ClientGenerator.Core.convenientAPI)
- [`@flattenProperty`](./decorators.md#@Azure.ClientGenerator.Core.flattenProperty)
- [`@operationGroup`](./decorators.md#@Azure.ClientGenerator.Core.operationGroup)
- [`@override`](./decorators.md#@Azure.ClientGenerator.Core.override)
- [`@paramAlias`](./decorators.md#@Azure.ClientGenerator.Core.paramAlias)
- [`@protocolAPI`](./decorators.md#@Azure.ClientGenerator.Core.protocolAPI)
- [`@usage`](./decorators.md#@Azure.ClientGenerator.Core.usage)
- [`@useSystemTextJsonConverter`](./decorators.md#@Azure.ClientGenerator.Core.useSystemTextJsonConverter)
81 changes: 81 additions & 0 deletions packages/typespec-client-generator-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ npm install @azure-tools/typespec-client-generator-core

- [`@access`](#@access)
- [`@client`](#@client)
- [`@clientInitialization`](#@clientinitialization)
- [`@clientName`](#@clientname)
- [`@convenientAPI`](#@convenientapi)
- [`@flattenProperty`](#@flattenproperty)
- [`@operationGroup`](#@operationgroup)
- [`@override`](#@override)
- [`@paramAlias`](#@paramalias)
- [`@protocolAPI`](#@protocolapi)
- [`@usage`](#@usage)
- [`@useSystemTextJsonConverter`](#@usesystemtextjsonconverter)
Expand Down Expand Up @@ -216,6 +218,45 @@ interface MyInterface {}
interface MyInterface {}
```

#### `@clientInitialization`

Client parameters you would like to add to the client. By default, we apply endpoint, credential, and api-version parameters. If you add clientInitialization, we will append those to the default list of parameters.

```typespec
@Azure.ClientGenerator.Core.clientInitialization(options: Model, scope?: valueof string)
```

##### Target

`Namespace | Interface`

##### Parameters

| Name | Type | Description |
| ------- | ---------------- | ------------------------------------------------------------------------------------------------------------- |
| options | `Model` | |
| scope | `valueof string` | The language scope you want this decorator to apply to. If not specified, will apply to all language emitters |

##### Examples

```typespec
// main.tsp
namespace MyService;

op upload(blobName: string): void;
op download(blobName: string): void;

// client.tsp
namespace MyCustomizations;
model MyServiceClientOptions {
blobName: string;
}

@@clientInitialization(MyService, MyServiceClientOptions)
// The generated client will have `blobName` on it. We will also
// elevate the existing `blobName` parameter to the client level.
```

#### `@clientName`

Changes the name of a method, parameter, property, or model generated in the client SDK
Expand Down Expand Up @@ -391,6 +432,46 @@ op myOperationCustomization(params: Params): void;
// method signature is now `op myOperation(params: Params)` just for csharp
```

#### `@paramAlias`

Alias the name of a client parameter to a different name. This permits you to have a different name for the parameter in client initialization then on individual methods and still refer to the same parameter.

```typespec
@Azure.ClientGenerator.Core.paramAlias(paramAlias: valueof string, scope?: valueof string)
```

##### Target

`ModelProperty`

##### Parameters

| Name | Type | Description |
| ---------- | ---------------- | ------------------------------------------------------------------------------------------------------------- |
| paramAlias | `valueof string` | |
| scope | `valueof string` | The language scope you want this decorator to apply to. If not specified, will apply to all language emitters |

##### Examples

```typespec
// main.tsp
namespace MyService;

op upload(blobName: string): void;

// client.tsp
namespace MyCustomizations;
model MyServiceClientOptions {
blob: string;
}

@@clientInitialization(MyService, MyServiceClientOptions)
@@paramAlias(MyServiceClientOptions.blob, "blobName")

// The generated client will have `blobName` on it. We will also
// elevate the existing `blob` parameter to the client level.
```

#### `@protocolAPI`

Whether you want to generate an operation as a protocol operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,67 @@ export type UseSystemTextJsonConverterDecorator = (
scope?: string
) => void;

/**
* Client parameters you would like to add to the client. By default, we apply endpoint, credential, and api-version parameters. If you add clientInitialization, we will append those to the default list of parameters.
*
* @param scope The language scope you want this decorator to apply to. If not specified, will apply to all language emitters
* @example
* ```typespec
* // main.tsp
* namespace MyService;
*
* op upload(blobName: string): void;
* op download(blobName: string): void;
*
* // client.tsp
* namespace MyCustomizations;
* model MyServiceClientOptions {
* blobName: string;
* }
*
* @@clientInitialization(MyService, MyServiceClientOptions)
* // The generated client will have `blobName` on it. We will also
* // elevate the existing `blobName` parameter to the client level.
* ```
*/
export type ClientInitializationDecorator = (
context: DecoratorContext,
target: Namespace | Interface,
options: Model,
tadelesh marked this conversation as resolved.
Show resolved Hide resolved
scope?: string
) => void;

/**
* Alias the name of a client parameter to a different name. This permits you to have a different name for the parameter in client initialization then on individual methods and still refer to the same parameter.
*
* @param scope The language scope you want this decorator to apply to. If not specified, will apply to all language emitters
* @example
* ```typespec
* // main.tsp
* namespace MyService;
*
* op upload(blobName: string): void;
*
* // client.tsp
* namespace MyCustomizations;
* model MyServiceClientOptions {
* blob: string;
* }
*
* @@clientInitialization(MyService, MyServiceClientOptions)
* @@paramAlias(MyServiceClientOptions.blob, "blobName")
*
* // The generated client will have `blobName` on it. We will also
* // elevate the existing `blob` parameter to the client level.
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
* ```
*/
export type ParamAliasDecorator = (
context: DecoratorContext,
original: ModelProperty,
paramAlias: string,
scope?: string
) => void;

export type AzureClientGeneratorCoreDecorators = {
clientName: ClientNameDecorator;
convenientAPI: ConvenientAPIDecorator;
Expand All @@ -444,4 +505,6 @@ export type AzureClientGeneratorCoreDecorators = {
flattenProperty: FlattenPropertyDecorator;
override: OverrideDecorator;
useSystemTextJsonConverter: UseSystemTextJsonConverterDecorator;
clientInitialization: ClientInitializationDecorator;
paramAlias: ParamAliasDecorator;
};
55 changes: 55 additions & 0 deletions packages/typespec-client-generator-core/lib/decorators.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,58 @@ extern dec override(original: Operation, override: Operation, scope?: valueof st
* ```
*/
extern dec useSystemTextJsonConverter(target: Model, scope?: valueof string);

/**
* Client parameters you would like to add to the client. By default, we apply endpoint, credential, and api-version parameters. If you add clientInitialization, we will append those to the default list of parameters.
* @param scope The language scope you want this decorator to apply to. If not specified, will apply to all language emitters
*
* @example
* ```typespec
* // main.tsp
* namespace MyService;
*
* op upload(blobName: string): void;
* op download(blobName: string): void;
*
* // client.tsp
* namespace MyCustomizations;
* model MyServiceClientOptions {
* blobName: string;
* }
*
* @@clientInitialization(MyService, MyServiceClientOptions)
* // The generated client will have `blobName` on it. We will also
* // elevate the existing `blobName` parameter to the client level.
* ```
*/
extern dec clientInitialization(
target: Namespace | Interface,
options: Model,
scope?: valueof string
);

/**
* Alias the name of a client parameter to a different name. This permits you to have a different name for the parameter in client initialization then on individual methods and still refer to the same parameter.
* @param scope The language scope you want this decorator to apply to. If not specified, will apply to all language emitters
*
* @example
* ```typespec
* // main.tsp
* namespace MyService;
*
* op upload(blobName: string): void;
*
* // client.tsp
* namespace MyCustomizations;
* model MyServiceClientOptions {
* blob: string;
* }
*
* @@clientInitialization(MyService, MyServiceClientOptions)
* @@paramAlias(MyServiceClientOptions.blob, "blobName")
*
* // The generated client will have `blobName` on it. We will also
* // elevate the existing `blob` parameter to the client level.
* ```
*/
extern dec paramAlias(original: ModelProperty, paramAlias: valueof string, scope?: valueof string);
Loading
Loading