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

Mgmt rest client #47665

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
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.20241225.2" />
</ItemGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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: TransfromModifiers(type), relativeFilePath: TransformRelativeFilePathForClient(type));
}
//if (type is RestClientProvider)
Copy link
Member Author

@live1206 live1206 Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we make both client and restclient internal, all the models referenced will be internalized during the postprocess.
Once we have the resource and collection in place, public reference will be in place and the referenced models will be kept public.
So, keep restclient public for now to make the referenced models public.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we put this comment as a comment in the code? just in case we could remember it and enable this piece in the future when we have the resource/collection types to keep those models public

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added TODO

//{
// 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 TransfromModifiers(TypeProvider type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should this be: TransformPublicToInternal or similar to be explicit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed

{
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
Expand Up @@ -107,7 +107,8 @@ public override MethodBodyStatement SerializeJsonValue(Type valueType, ValueExpr
}

/// <inheritdoc/>
protected override ClientProvider CreateClientCore(InputClient inputClient) => base.CreateClientCore(TransformInputClient(inputClient));
protected override ClientProvider CreateClientCore(InputClient inputClient)
=> AzureClientPlugin.Instance.IsAzureArm.Value ? base.CreateClientCore(TransformInputClient(inputClient)) : base.CreateClientCore(inputClient);

private InputClient TransformInputClient(InputClient client)
{
Expand All @@ -117,10 +118,29 @@ private InputClient TransformInputClient(InputClient client)
// 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);
// TODO: have a helper method in InputOperation to update the parameters
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 IReadOnlyList<InputParameter> TransformInputOperationParameters(InputOperation operation)
{
var parameters = new List<InputParameter>();
foreach (var parameter in operation.Parameters)
{
if (parameter.NameInRequest.Equals("subscriptionId", StringComparison.OrdinalIgnoreCase))
{
parameters.Add(new InputParameter(parameter.Name, parameter.NameInRequest, parameter.Summary, parameter.Doc, InputPrimitiveType.String, 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
Loading