Skip to content
Merged
Prev Previous commit
Next Next commit
refine.
  • Loading branch information
haiyuazhang committed Sep 15, 2025
commit 1cc5793e42a875375964e73cbb78d981a8d32db5
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private MethodProvider BuildServiceMethod(InputServiceMethod method, InputClient
List<FieldProvider> pathParameterFields = new List<FieldProvider>();
return method switch
{
InputPagingServiceMethod pagingMethod => new PageableOperationMethodProvider(this, _contextualPath, clientInfo, pagingMethod, pathParameterFields, isAsync, methodName),
InputPagingServiceMethod pagingMethod => new PageableOperationMethodProvider(this, _contextualPath, clientInfo, pagingMethod, isAsync, methodName),
_ => new ResourceOperationMethodProvider(this, _contextualPath, clientInfo, method, isAsync, methodName: methodName)
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,19 @@ internal class PageableOperationMethodProvider
private readonly string _methodName;
private readonly MethodSignature _signature;
private readonly MethodBodyStatement[] _bodyStatements;
private readonly IReadOnlyList<FieldProvider> _pathParameterFields;

public PageableOperationMethodProvider(
TypeProvider enclosingType,
RequestPathPattern contextualPath,
RestClientInfo restClientInfo,
InputPagingServiceMethod method,
IReadOnlyList<FieldProvider> pathParameterFields,
bool isAsync,
string? methodName = null)
{
_enclosingType = enclosingType;
_contextualPath = contextualPath;
_restClientInfo = restClientInfo;
_method = method;
_pathParameterFields = pathParameterFields;
_convenienceMethod = restClientInfo.RestClientProvider.GetConvenienceMethodByOperation(_method.Operation, isAsync);
_isAsync = isAsync;
_itemType = _convenienceMethod.Signature.ReturnType!.Arguments[0]; // a paging method's return type should be `Pageable<T>` or `AsyncPageable<T>`, so we can safely access the first argument as the item type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Azure.Generator.Management.Extensions;
using Azure.Generator.Management.Models;
using Azure.Generator.Management.Primitives;
using Azure.Generator.Management.Providers;
using Azure.Generator.Management.Snippets;
using Azure.Generator.Management.Utilities;
using Azure.ResourceManager;
Expand All @@ -18,7 +17,6 @@
using Microsoft.TypeSpec.Generator.Statements;
using System;
using System.Collections.Generic;
using System.Linq;
using static Microsoft.TypeSpec.Generator.Snippets.Snippet;

namespace Azure.Generator.Management.Providers.OperationMethodProviders
Expand Down Expand Up @@ -181,22 +179,9 @@ private TryExpression BuildTryExpression()
ResourceMethodSnippets.CreateRequestContext(cancellationTokenParameter, out var contextVariable)
};

// Get contextual parameters from the request path pattern
// Populate arguments for the REST client method call
var arguments = _contextualPath.PopulateArguments(This.As<ArmResource>().Id(), requestMethod.Signature.Parameters, contextVariable, _signature.Parameters, _enclosingType);

// // Get path field parameters if the enclosing type is ResourceCollectionClientProvider
// var pathFieldsParameters = new List<ValueExpression>();
// if (_enclosingType is ResourceCollectionClientProvider collectionProvider)
// {
// foreach (var pathField in collectionProvider.PathParameterFields)
// {
// pathFieldsParameters.Add(pathField);
// }
// }

// // Combine contextual parameters and path field parameters into final arguments
// var arguments = parameters.Concat(pathFieldsParameters).ToList();

tryStatements.Add(ResourceMethodSnippets.CreateHttpMessage(_restClientField, requestMethod.Signature.Name, arguments, out var messageVariable));

tryStatements.AddRange(BuildClientPipelineProcessing(messageVariable, contextVariable, out var responseVariable));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Generic;

using Azure.Generator.Management.Models;
using Microsoft.TypeSpec.Generator.Input;
using Microsoft.TypeSpec.Generator.Providers;

namespace Azure.Generator.Management.Providers.OperationMethodProviders
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,6 @@ private MethodSignature BuildFactoryMethodSignature()
}
}

private List<FieldProvider> BuildPathParameterFields()
{
var fields = new List<FieldProvider>();
// Path parameter fields are only needed for ResourceCollectionClientProvider, not ResourceClientProvider
return fields;
}

protected override FieldProvider[] BuildFields()
{
List<FieldProvider> fields = new();
Expand All @@ -196,7 +189,6 @@ protected override FieldProvider[] BuildFields()
}
fields.Add(_dataField);
fields.Add(_resourceTypeField);
fields.AddRange(BuildPathParameterFields());

return fields.ToArray();
}
Expand Down Expand Up @@ -312,7 +304,7 @@ private ConstructorProvider BuildResourceIdentifierConstructor()
}

// TODO -- this is temporary. We should change this to find the corresponding parameters in ContextualParameters after it is refactored to consume parent resources.
private CSharpType GetPathParameterType(string parameterName)
public CSharpType GetPathParameterType(string parameterName)
{
foreach (var resourceMethod in _resourceServiceMethods)
{
Expand Down Expand Up @@ -422,8 +414,8 @@ protected override MethodProvider[] BuildMethods()
if (method is InputPagingServiceMethod pagingMethod)
{
// Use PageableOperationMethodProvider for InputPagingServiceMethod
operationMethods.Add(new PageableOperationMethodProvider(this, _contextualPath, restClientInfo, pagingMethod, BuildPathParameterFields(), true, methodName: ResourceHelpers.GetOperationMethodName(methodKind, true)));
operationMethods.Add(new PageableOperationMethodProvider(this, _contextualPath, restClientInfo, pagingMethod, BuildPathParameterFields(), false, methodName: ResourceHelpers.GetOperationMethodName(methodKind, false)));
operationMethods.Add(new PageableOperationMethodProvider(this, _contextualPath, restClientInfo, pagingMethod, true, methodName: ResourceHelpers.GetOperationMethodName(methodKind, true)));
operationMethods.Add(new PageableOperationMethodProvider(this, _contextualPath, restClientInfo, pagingMethod, false, methodName: ResourceHelpers.GetOperationMethodName(methodKind, false)));

continue;
}
Expand All @@ -442,9 +434,9 @@ protected override MethodProvider[] BuildMethods()
else
{
var asyncMethodName = ResourceHelpers.GetOperationMethodName(methodKind, true);
operationMethods.Add(new ResourceOperationMethodProvider(this, _contextualPath, restClientInfo, method, true, methodName: asyncMethodName, forceLro: isFakeLro));
operationMethods.Add(new ResourceOperationMethodProvider(this, _contextualPath, restClientInfo, method, true, asyncMethodName, forceLro: isFakeLro));
var methodName = ResourceHelpers.GetOperationMethodName(methodKind, false);
operationMethods.Add(new ResourceOperationMethodProvider(this, _contextualPath, restClientInfo, method, false, methodName: methodName, forceLro: isFakeLro));
operationMethods.Add(new ResourceOperationMethodProvider(this, _contextualPath, restClientInfo, method, false, methodName, forceLro: isFakeLro));
}
}

Expand All @@ -467,12 +459,12 @@ protected override MethodProvider[] BuildMethods()
var getRestClientInfo = _clientInfos[getClient];

methods.AddRange([
new AddTagMethodProvider(this, _contextualPath, updateMethodProvider, getMethod, BuildPathParameterFields(), updateRestClientInfo, getRestClientInfo, isPatch, true),
new AddTagMethodProvider(this, _contextualPath, updateMethodProvider, getMethod, BuildPathParameterFields(), updateRestClientInfo, getRestClientInfo, isPatch, false),
new SetTagsMethodProvider(this, _contextualPath, updateMethodProvider, getMethod, BuildPathParameterFields(), updateRestClientInfo, getRestClientInfo, isPatch, true),
new SetTagsMethodProvider(this, _contextualPath, updateMethodProvider, getMethod, BuildPathParameterFields(), updateRestClientInfo, getRestClientInfo, isPatch, false),
new RemoveTagMethodProvider(this, _contextualPath, updateMethodProvider, getMethod, BuildPathParameterFields(), updateRestClientInfo, getRestClientInfo, isPatch, true),
new RemoveTagMethodProvider(this, _contextualPath, updateMethodProvider, getMethod, BuildPathParameterFields(), updateRestClientInfo, getRestClientInfo, isPatch, false)
new AddTagMethodProvider(this, _contextualPath, updateMethodProvider, getMethod, updateRestClientInfo, getRestClientInfo, isPatch, true),
new AddTagMethodProvider(this, _contextualPath, updateMethodProvider, getMethod, updateRestClientInfo, getRestClientInfo, isPatch, false),
new SetTagsMethodProvider(this, _contextualPath, updateMethodProvider, getMethod, updateRestClientInfo, getRestClientInfo, isPatch, true),
new SetTagsMethodProvider(this, _contextualPath, updateMethodProvider, getMethod, updateRestClientInfo, getRestClientInfo, isPatch, false),
new RemoveTagMethodProvider(this, _contextualPath, updateMethodProvider, getMethod, updateRestClientInfo, getRestClientInfo, isPatch, true),
new RemoveTagMethodProvider(this, _contextualPath, updateMethodProvider, getMethod, updateRestClientInfo, getRestClientInfo, isPatch, false)
]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Microsoft.TypeSpec.Generator.Providers;
using Microsoft.TypeSpec.Generator.Snippets;
using Microsoft.TypeSpec.Generator.Statements;
using Microsoft.TypeSpec.Generator.Expressions;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -157,43 +156,6 @@ protected override PropertyProvider[] BuildProperties()
return [.. properties];
}

private CSharpType GetPathParameterType(string parameterName)
{
foreach (var resourceMethod in _resourceServiceMethods)
{
if (!resourceMethod.Kind.IsCrudKind())
{
continue; // Skip non-CRUD operations
}
// iterate through all parameters in this method to find a matching parameter
foreach (var parameter in resourceMethod.InputMethod.Operation.Parameters)
{
if (parameter is not InputPathParameter)
{
continue; // Skip parameters that are not in the path
}
if (parameter.Name == parameterName)
{
var csharpType = ManagementClientGenerator.Instance.TypeFactory.CreateCSharpType(parameter.Type) ?? typeof(string);
return parameterName switch
{
"subscriptionId" when csharpType.Equals(typeof(Guid)) => typeof(string),
// Cases will be added later
_ => csharpType
};
}
}
}

// what if we did not find the parameter in any method?
ManagementClientGenerator.Instance.Emitter.ReportDiagnostic(
"general-warning",
$"Cannot find parameter {parameterName} in any registered operations in resource {ResourceName}."
);

return typeof(string); // Default to string if not found
}

private List<FieldProvider> BuildPathParameterFields()
{
var fields = new List<FieldProvider>();
Expand All @@ -208,7 +170,7 @@ private List<FieldProvider> BuildPathParameterFields()

foreach (var seg in variableSegments)
{
var field = new FieldProvider(FieldModifiers.Private | FieldModifiers.ReadOnly, GetPathParameterType(seg.VariableName), $"_{seg.VariableName}", this, description: $"The {seg.VariableName}.");
var field = new FieldProvider(FieldModifiers.Private | FieldModifiers.ReadOnly, Resource.GetPathParameterType(seg.VariableName), $"_{seg.VariableName}", this, description: $"The {seg.VariableName}.");
fields.Add(field);
}

Expand Down Expand Up @@ -380,7 +342,7 @@ private MethodProvider BuildGetAllMethod(ResourceMethod getAll, bool isAsync)
var methodName = ResourceHelpers.GetOperationMethodName(ResourceOperationKind.List, isAsync);
return getAll.InputMethod switch
{
InputPagingServiceMethod pagingGetAll => new PageableOperationMethodProvider(this, _contextualPath, restClientInfo, pagingGetAll, _pathParameterFields, isAsync, methodName),
InputPagingServiceMethod pagingGetAll => new PageableOperationMethodProvider(this, _contextualPath, restClientInfo, pagingGetAll, isAsync, methodName),
_ => new ResourceOperationMethodProvider(this, _contextualPath, restClientInfo, getAll.InputMethod, isAsync, methodName: methodName)
};
}
Expand Down Expand Up @@ -441,12 +403,6 @@ private List<MethodProvider> BuildGetIfExistsMethods()
return result;
}

/// <summary>
/// Tries to find a matching path parameter field for the given parameter.
/// </summary>
/// <param name="parameter">The parameter to find a matching field for.</param>
/// <param name="matchingField">The matching field if found.</param>
/// <returns>True if a matching field was found, false otherwise.</returns>
public bool TryGetPrivateFieldParameter(ParameterProvider parameter, out FieldProvider? matchingField)
{
matchingField = PathParameterFields.FirstOrDefault(field =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ public AddTagMethodProvider(
RequestPathPattern contextualPath,
ResourceOperationMethodProvider updateMethodProvider,
InputServiceMethod getMethod,
List<FieldProvider> pathParameterFields,
RestClientInfo updateRestClientInfo,
RestClientInfo getRestClientInfo,
bool isPatch,
bool isAsync)
: base(resource, contextualPath, updateMethodProvider, getMethod, pathParameterFields, updateRestClientInfo, getRestClientInfo, isPatch, isAsync,
: base(resource, contextualPath, updateMethodProvider, getMethod, updateRestClientInfo, getRestClientInfo, isPatch, isAsync,
isAsync ? "AddTagAsync" : "AddTag",
"Add a tag to the current resource.")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ internal abstract class BaseTagMethodProvider
protected readonly bool _isLongRunningUpdateOperation;
protected static readonly ParameterProvider _keyParameter = new ParameterProvider("key", $"The key for the tag.", typeof(string), validation: ParameterValidationType.AssertNotNull);
protected static readonly ParameterProvider _valueParameter = new ParameterProvider("value", $"The value for the tag.", typeof(string), validation: ParameterValidationType.AssertNotNull);
protected readonly List<FieldProvider> _pathParameterFields;

// TODO: make a struct to group the input parameters
protected BaseTagMethodProvider(
ResourceClientProvider resource,
RequestPathPattern contextualPath,
ResourceOperationMethodProvider updateMethodProvider,
InputServiceMethod getMethod,
List<FieldProvider> pathParameterFields,
RestClientInfo updateRestClientInfo,
RestClientInfo getRestClientInfo,
bool isPatch,
Expand All @@ -66,7 +64,6 @@ protected BaseTagMethodProvider(
_isLongRunningUpdateOperation = updateMethodProvider.IsLongRunningOperation;
_updateClientDiagnosticsField = updateRestClientInfo.DiagnosticsField;
_getRestClientField = getRestClientInfo.RestClientField;
_pathParameterFields = pathParameterFields;

_signature = CreateMethodSignature(methodName, methodDescription);
_bodyStatements = BuildBodyStatements();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ public RemoveTagMethodProvider(
RequestPathPattern contextualPath,
ResourceOperationMethodProvider updateMethodProvider,
InputServiceMethod getMethod,
List<FieldProvider> pathParameterFields,
RestClientInfo updateRestClientInfo,
RestClientInfo getRestClientInfo,
bool isPatch,
bool isAsync)
: base(resource, contextualPath, updateMethodProvider, getMethod, pathParameterFields, updateRestClientInfo, getRestClientInfo, isPatch, isAsync,
: base(resource, contextualPath, updateMethodProvider, getMethod, updateRestClientInfo, getRestClientInfo, isPatch, isAsync,
isAsync ? "RemoveTagAsync" : "RemoveTag",
"Removes a tag by key from the resource.")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ public SetTagsMethodProvider(
RequestPathPattern contextualPath,
ResourceOperationMethodProvider updateMethodProvider,
InputServiceMethod getMethod,
List<FieldProvider> pathParameterFields,
RestClientInfo updateRestClientInfo,
RestClientInfo getRestClientInfo,
bool isPatch,
bool isAsync)
: base(resource, contextualPath, updateMethodProvider, getMethod, pathParameterFields, updateRestClientInfo, getRestClientInfo, isPatch, isAsync,
: base(resource, contextualPath, updateMethodProvider, getMethod, updateRestClientInfo, getRestClientInfo, isPatch, isAsync,
isAsync ? "SetTagsAsync" : "SetTags",
"Replace the tags on the resource with the given set.")
{
Expand Down