Skip to content

Commit 91af14e

Browse files
authored
Mgmt rest client (Azure#47665)
1 parent 2fe6c8b commit 91af14e

File tree

10 files changed

+194
-102
lines changed

10 files changed

+194
-102
lines changed

eng/Packages.Data.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@
259259
</ItemGroup>
260260

261261
<ItemGroup Condition="'$(IsGeneratorLibrary)' == 'true'">
262-
<PackageReference Update="Microsoft.Generator.CSharp.ClientModel" Version="1.0.0-alpha.20241223.1" />
262+
<PackageReference Update="Microsoft.Generator.CSharp.ClientModel" Version="1.0.0-alpha.20250106.3" />
263263
</ItemGroup>
264264

265265
<!--
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Generator.CSharp.ClientModel;
5+
using Microsoft.Generator.CSharp.ClientModel.Providers;
6+
using Microsoft.Generator.CSharp.Primitives;
7+
using Microsoft.Generator.CSharp.Providers;
8+
using System.IO;
9+
10+
namespace Azure.Generator
11+
{
12+
// only apply for MPG
13+
internal class AzureArmVisitor : ScmLibraryVisitor
14+
{
15+
/// <inheritdoc/>
16+
protected override TypeProvider? Visit(TypeProvider type)
17+
{
18+
if (type is ClientProvider)
19+
{
20+
type.Update(modifiers: TransfromPublicModifiersToInternal(type), relativeFilePath: TransformRelativeFilePathForClient(type));
21+
}
22+
// TODO: uncomment this once resources are generated
23+
//if (type is RestClientProvider)
24+
//{
25+
// type.Update(modifiers: TransfromModifiers(type), relativeFilePath: TransformRelativeFilePathForRestClient(type));
26+
//}
27+
return type;
28+
}
29+
30+
private static string TransformRelativeFilePathForClient(TypeProvider type)
31+
=> Path.Combine("src", "Generated", "RestOperations", $"{type.Name}RestOperations.cs");
32+
33+
private static string TransformRelativeFilePathForRestClient(TypeProvider type)
34+
=> Path.Combine("src", "Generated", "RestOperations", $"{type.Name}.RestClient.cs");
35+
36+
private static TypeSignatureModifiers TransfromPublicModifiersToInternal(TypeProvider type)
37+
{
38+
var modifiers = type.DeclarationModifiers;
39+
if (modifiers.HasFlag(TypeSignatureModifiers.Public))
40+
{
41+
modifiers &= ~TypeSignatureModifiers.Public;
42+
modifiers |= TypeSignatureModifiers.Internal;
43+
}
44+
45+
return modifiers;
46+
}
47+
}
48+
}

eng/packages/http-client-csharp/generator/Azure.Generator/src/AzureClientPlugin.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public override void Configure()
4949
AddMetadataReference(MetadataReference.CreateFromFile(typeof(Response).Assembly.Location));
5050
var sharedSourceDirectory = Path.Combine(Path.GetDirectoryName(typeof(AzureClientPlugin).Assembly.Location)!, "Shared", "Core");
5151
AddSharedSourceDirectory(sharedSourceDirectory);
52+
if (IsAzureArm.Value)
53+
{
54+
AddVisitor(new AzureArmVisitor());
55+
}
5256
}
5357

5458
/// <summary>

eng/packages/http-client-csharp/generator/Azure.Generator/src/AzureTypeFactory.cs

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

4+
using Azure.Generator.InputTransformation;
45
using Azure.Generator.Primitives;
56
using Azure.Generator.Providers;
67
using Azure.Generator.Providers.Abstraction;
@@ -13,7 +14,6 @@
1314
using Microsoft.Generator.CSharp.Statements;
1415
using System;
1516
using System.ClientModel.Primitives;
16-
using System.Collections.Generic;
1717
using System.Text.Json;
1818

1919
namespace Azure.Generator
@@ -107,20 +107,7 @@ public override MethodBodyStatement SerializeJsonValue(Type valueType, ValueExpr
107107
}
108108

109109
/// <inheritdoc/>
110-
protected override ClientProvider CreateClientCore(InputClient inputClient) => base.CreateClientCore(TransformInputClient(inputClient));
111-
112-
private InputClient TransformInputClient(InputClient client)
113-
{
114-
var operationsToKeep = new List<InputOperation>();
115-
foreach (var operation in client.Operations)
116-
{
117-
// operations_list has been covered in Azure.ResourceManager already, we don't need to generate it in the client
118-
if (operation.CrossLanguageDefinitionId != "Azure.ResourceManager.Operations.list")
119-
{
120-
operationsToKeep.Add(operation);
121-
}
122-
}
123-
return new InputClient(client.Name, client.Summary, client.Doc, operationsToKeep, client.Parameters, client.Parent);
124-
}
110+
protected override ClientProvider CreateClientCore(InputClient inputClient)
111+
=> AzureClientPlugin.Instance.IsAzureArm.Value ? base.CreateClientCore(InputClientTransformer.TransformInputClient(inputClient)) : base.CreateClientCore(inputClient);
125112
}
126113
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Generator.CSharp.Input;
5+
using System;
6+
using System.Collections.Generic;
7+
8+
namespace Azure.Generator.InputTransformation
9+
{
10+
internal static class InputClientTransformer
11+
{
12+
public static InputClient TransformInputClient(InputClient client)
13+
{
14+
var operationsToKeep = new List<InputOperation>();
15+
foreach (var operation in client.Operations)
16+
{
17+
// operations_list has been covered in Azure.ResourceManager already, we don't need to generate it in the client
18+
if (operation.CrossLanguageDefinitionId != "Azure.ResourceManager.Operations.list")
19+
{
20+
var transformedOperation = new InputOperation(operation.Name, operation.ResourceName, operation.Summary, operation.Doc, operation.Deprecated, operation.Accessibility, TransformInputOperationParameters(operation), operation.Responses, operation.HttpMethod, operation.RequestBodyMediaType, operation.Uri, operation.Path, operation.ExternalDocsUrl, operation.RequestMediaTypes, operation.BufferResponse, operation.LongRunning, operation.Paging, operation.GenerateProtocolMethod, operation.GenerateConvenienceMethod, operation.CrossLanguageDefinitionId);
21+
operationsToKeep.Add(transformedOperation);
22+
}
23+
}
24+
return new InputClient(client.Name, client.Summary, client.Doc, operationsToKeep, client.Parameters, client.Parent);
25+
}
26+
27+
private static IReadOnlyList<InputParameter> TransformInputOperationParameters(InputOperation operation)
28+
{
29+
var parameters = new List<InputParameter>();
30+
foreach (var parameter in operation.Parameters)
31+
{
32+
if (parameter.NameInRequest.Equals("subscriptionId", StringComparison.OrdinalIgnoreCase))
33+
{
34+
// Always set subscriptionId to method parameter
35+
parameters.Add(new InputParameter(parameter.Name, parameter.NameInRequest, parameter.Summary, parameter.Doc, parameter.Type, parameter.Location, parameter.DefaultValue, InputOperationParameterKind.Method, parameter.IsRequired, parameter.IsApiVersion, parameter.IsResourceParameter, parameter.IsContentType, parameter.IsEndpoint, parameter.SkipUrlEncoding, parameter.Explode, parameter.ArraySerializationDelimiter, parameter.HeaderCollectionPrefix));
36+
}
37+
else
38+
{
39+
parameters.Add(parameter);
40+
}
41+
}
42+
return parameters;
43+
}
44+
}
45+
}

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

Lines changed: 9 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/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/PrivateLinks.RestClient.cs

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

0 commit comments

Comments
 (0)