Skip to content

Commit

Permalink
Mgmt rest client (#47665)
Browse files Browse the repository at this point in the history
  • Loading branch information
live1206 authored Jan 7, 2025
1 parent 2fe6c8b commit 91af14e
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 102 deletions.
2 changes: 1 addition & 1 deletion eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
</ItemGroup>

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

<!--
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Generator.CSharp.ClientModel;
using Microsoft.Generator.CSharp.ClientModel.Providers;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
using System.IO;

namespace Azure.Generator
{
// only apply for MPG
internal class AzureArmVisitor : ScmLibraryVisitor
{
/// <inheritdoc/>
protected override TypeProvider? Visit(TypeProvider type)
{
if (type is ClientProvider)
{
type.Update(modifiers: TransfromPublicModifiersToInternal(type), relativeFilePath: TransformRelativeFilePathForClient(type));
}
// TODO: uncomment this once resources are generated
//if (type is RestClientProvider)
//{
// type.Update(modifiers: TransfromModifiers(type), relativeFilePath: TransformRelativeFilePathForRestClient(type));
//}
return type;
}

private static string TransformRelativeFilePathForClient(TypeProvider type)
=> Path.Combine("src", "Generated", "RestOperations", $"{type.Name}RestOperations.cs");

private static string TransformRelativeFilePathForRestClient(TypeProvider type)
=> Path.Combine("src", "Generated", "RestOperations", $"{type.Name}.RestClient.cs");

private static TypeSignatureModifiers TransfromPublicModifiersToInternal(TypeProvider type)
{
var modifiers = type.DeclarationModifiers;
if (modifiers.HasFlag(TypeSignatureModifiers.Public))
{
modifiers &= ~TypeSignatureModifiers.Public;
modifiers |= TypeSignatureModifiers.Internal;
}

return modifiers;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public override void Configure()
AddMetadataReference(MetadataReference.CreateFromFile(typeof(Response).Assembly.Location));
var sharedSourceDirectory = Path.Combine(Path.GetDirectoryName(typeof(AzureClientPlugin).Assembly.Location)!, "Shared", "Core");
AddSharedSourceDirectory(sharedSourceDirectory);
if (IsAzureArm.Value)
{
AddVisitor(new AzureArmVisitor());
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Generator.InputTransformation;
using Azure.Generator.Primitives;
using Azure.Generator.Providers;
using Azure.Generator.Providers.Abstraction;
Expand All @@ -13,7 +14,6 @@
using Microsoft.Generator.CSharp.Statements;
using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text.Json;

namespace Azure.Generator
Expand Down Expand Up @@ -107,20 +107,7 @@ public override MethodBodyStatement SerializeJsonValue(Type valueType, ValueExpr
}

/// <inheritdoc/>
protected override ClientProvider CreateClientCore(InputClient inputClient) => base.CreateClientCore(TransformInputClient(inputClient));

private InputClient TransformInputClient(InputClient client)
{
var operationsToKeep = new List<InputOperation>();
foreach (var operation in client.Operations)
{
// operations_list has been covered in Azure.ResourceManager already, we don't need to generate it in the client
if (operation.CrossLanguageDefinitionId != "Azure.ResourceManager.Operations.list")
{
operationsToKeep.Add(operation);
}
}
return new InputClient(client.Name, client.Summary, client.Doc, operationsToKeep, client.Parameters, client.Parent);
}
protected override ClientProvider CreateClientCore(InputClient inputClient)
=> AzureClientPlugin.Instance.IsAzureArm.Value ? base.CreateClientCore(InputClientTransformer.TransformInputClient(inputClient)) : base.CreateClientCore(inputClient);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Generator.CSharp.Input;
using System;
using System.Collections.Generic;

namespace Azure.Generator.InputTransformation
{
internal static class InputClientTransformer
{
public static InputClient TransformInputClient(InputClient client)
{
var operationsToKeep = new List<InputOperation>();
foreach (var operation in client.Operations)
{
// operations_list has been covered in Azure.ResourceManager already, we don't need to generate it in the client
if (operation.CrossLanguageDefinitionId != "Azure.ResourceManager.Operations.list")
{
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);
operationsToKeep.Add(transformedOperation);
}
}
return new InputClient(client.Name, client.Summary, client.Doc, operationsToKeep, client.Parameters, client.Parent);
}

private static IReadOnlyList<InputParameter> TransformInputOperationParameters(InputOperation operation)
{
var parameters = new List<InputParameter>();
foreach (var parameter in operation.Parameters)
{
if (parameter.NameInRequest.Equals("subscriptionId", StringComparison.OrdinalIgnoreCase))
{
// Always set subscriptionId to method parameter
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));
}
else
{
parameters.Add(parameter);
}
}
return parameters;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 91af14e

Please sign in to comment.