Skip to content

Commit 1fc1051

Browse files
authored
Adjust operation generation for root client (Azure#52394)
* Adjust operation for root client * update comment * typo
1 parent 957d0b3 commit 1fc1051

File tree

6 files changed

+2194
-2182
lines changed

6 files changed

+2194
-2182
lines changed

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Visitors/RestClientVisitor.cs

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using Azure.Core.Pipeline;
5+
using Azure.Generator.Management.Utilities;
46
using Microsoft.TypeSpec.Generator.ClientModel;
57
using Microsoft.TypeSpec.Generator.ClientModel.Providers;
68
using Microsoft.TypeSpec.Generator.Primitives;
79
using Microsoft.TypeSpec.Generator.Providers;
10+
using Microsoft.TypeSpec.Generator.Statements;
11+
using System;
812
using System.IO;
13+
using System.Linq;
14+
using static Microsoft.TypeSpec.Generator.Snippets.Snippet;
915

1016
namespace Azure.Generator.Management.Visitors;
1117

@@ -16,11 +22,20 @@ internal class RestClientVisitor : ScmLibraryVisitor
1622
{
1723
if (type is not null && type is ClientProvider client)
1824
{
19-
// omit methods for ClientProvider, MPG will implement its own client methods
20-
// put create request methods to client directly and remove RestClientProvider
21-
type.Update(methods: [.. client.RestClient.Methods], modifiers: TransfromPublicModifiersToInternal(type), relativeFilePath: TransformRelativeFilePathForClient(type));
25+
// handle root client
26+
if (client.ClientOptions is not null)
27+
{
28+
UpdateRootClient(client);
29+
}
30+
else
31+
{
32+
// omit methods for ClientProvider, MPG will implement its own client methods
33+
// put create request methods to client directly
34+
type.Update(methods: [.. client.RestClient.Methods], modifiers: TransformPublicModifiersToInternal(type), relativeFilePath: TransformRelativeFilePathForClient(type));
35+
}
2236
}
2337

38+
// remove RestClientProvider
2439
if (type is RestClientProvider)
2540
{
2641
return null;
@@ -29,10 +44,58 @@ internal class RestClientVisitor : ScmLibraryVisitor
2944
return type;
3045
}
3146

47+
private void UpdateRootClient(ClientProvider rootClient)
48+
{
49+
// fields
50+
var apiVersionField = new FieldProvider(FieldModifiers.Private | FieldModifiers.ReadOnly, typeof(string), "_apiVersion", rootClient);
51+
var endpointField = new FieldProvider(FieldModifiers.Private | FieldModifiers.ReadOnly, typeof(Uri), "_endpoint", rootClient);
52+
53+
// properties
54+
var pipelineProperty = new PropertyProvider(
55+
description: $"The HTTP pipeline for sending and receiving REST requests and responses.",
56+
modifiers: MethodSignatureModifiers.Public,
57+
type: typeof(HttpPipeline),
58+
name: "Pipeline",
59+
body: new AutoPropertyBody(false),
60+
enclosingType: rootClient);
61+
var clientDiagnosticsProperty = new PropertyProvider(
62+
description: $"The ClientDiagnostics is used to provide tracing support for the client library.",
63+
modifiers: MethodSignatureModifiers.Internal,
64+
type: typeof(ClientDiagnostics),
65+
name: "ClientDiagnostics",
66+
body: new AutoPropertyBody(false),
67+
enclosingType: rootClient);
68+
69+
// constructor
70+
var clientDiagnosticsParam = new ParameterProvider("clientDiagnostics", $"The ClientDiagnostics is used to provide tracing support for the client library.", typeof(ClientDiagnostics));
71+
var pipelineParam = new ParameterProvider("pipeline", $"The HTTP pipeline for sending and receiving REST requests and responses.", typeof(HttpPipeline));
72+
var endpointParam = new ParameterProvider("endpoint", $"Service endpoint.", typeof(Uri), null);
73+
var apiVersionParam = new ParameterProvider("apiVersion", $"The API version to use for this client.", typeof(string));
74+
var ctorBody = new MethodBodyStatement[]
75+
{
76+
clientDiagnosticsProperty.Assign(clientDiagnosticsParam).Terminate(),
77+
endpointField.Assign(endpointParam).Terminate(),
78+
pipelineProperty.Assign(pipelineParam).Terminate(),
79+
apiVersionField.Assign(apiVersionParam).Terminate(),
80+
};
81+
var ctor = new ConstructorProvider(
82+
new ConstructorSignature(rootClient.Type, null, MethodSignatureModifiers.Public, [clientDiagnosticsParam, pipelineParam, endpointParam, apiVersionParam]),
83+
ctorBody,
84+
rootClient);
85+
86+
rootClient.Update(
87+
fields:[apiVersionField, endpointField],
88+
methods: [.. rootClient.RestClient.Methods], // put create request methods to client directly
89+
modifiers: TransformPublicModifiersToInternal(rootClient),
90+
relativeFilePath: TransformRelativeFilePathForClient(rootClient),
91+
properties: [pipelineProperty, clientDiagnosticsProperty],
92+
constructors: [ctor, ConstructorProviderHelpers.BuildMockingConstructor(rootClient)]);
93+
}
94+
3295
private static string TransformRelativeFilePathForClient(TypeProvider type)
3396
=> Path.Combine("src", "Generated", "RestOperations", $"{type.Name}RestOperations.cs");
3497

35-
private static TypeSignatureModifiers TransfromPublicModifiersToInternal(TypeProvider type)
98+
private static TypeSignatureModifiers TransformPublicModifiersToInternal(TypeProvider type)
3699
{
37100
var modifiers = type.DeclarationModifiers;
38101
if (modifiers.HasFlag(TypeSignatureModifiers.Public))

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/routes.tsp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,20 @@ using Azure.ResourceManager;
99

1010
namespace MgmtTypeSpec;
1111

12-
interface FooTasks {
13-
/**
14-
* Runs the input conditions against input object metadata properties and designates matched objects in response.
15-
*/
16-
@autoRoute
17-
previewActions is ArmProviderActionSync<
18-
Request = FooPreviewAction,
19-
Response = FooPreviewAction,
20-
Scope = SubscriptionActionScope,
21-
Parameters = {
22-
@path
23-
@segment("locations")
24-
location: Azure.Core.azureLocation;
25-
}
26-
>;
27-
}
12+
/**
13+
* Runs the input conditions against input object metadata properties and designates matched objects in response.
14+
*/
15+
@autoRoute
16+
op previewActions is ArmProviderActionSync<
17+
Request = FooPreviewAction,
18+
Response = FooPreviewAction,
19+
Scope = SubscriptionActionScope,
20+
Parameters = {
21+
@path
22+
@segment("locations")
23+
location: Azure.Core.azureLocation;
24+
}
25+
>;
2826

2927
model FooPreviewAction {
3028
/** The action to be performed. */

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/Extensions/MockableMgmtTypeSpecSubscriptionResource.cs

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/MgmtTypeSpecModelFactory.cs

Lines changed: 8 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)