Skip to content

Commit 11493f0

Browse files
Update the list methods of resources in extensions to always have a name of Get{ResourceNames} (#52008)
* code update * regen * Update eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Utilities/ResourceHelpers.cs Co-authored-by: Copilot <[email protected]> * fix after merge --------- Co-authored-by: Copilot <[email protected]>
1 parent 5d608b9 commit 11493f0

File tree

8 files changed

+67
-27
lines changed

8 files changed

+67
-27
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ _mockableClients is not null ||
104104
// build mockable resources
105105
var resourcesAndMethodsPerScope = BuildResourcesAndNonResourceMethods(
106106
resourceDict,
107-
resourceMethodCategories.Values.SelectMany(c => c.MethodsInExtension),
107+
resourceMethodCategories,
108108
ManagementClientGenerator.Instance.InputLibrary.NonResourceMethods);
109109
var mockableArmClientResource = new MockableArmClientProvider(_resourceClients);
110110
var mockableResources = new List<MockableResourceProvider>(resourcesAndMethodsPerScope.Count)
@@ -126,7 +126,7 @@ _mockableClients is not null ||
126126

127127
static Dictionary<ResourceScope, ResourcesAndNonResourceMethodsInScope> BuildResourcesAndNonResourceMethods(
128128
IReadOnlyDictionary<ResourceMetadata, ResourceClientProvider> resourceDict,
129-
IEnumerable<ResourceMethod> resourceMethods,
129+
IReadOnlyDictionary<ResourceMetadata, ResourceMethodCategory> resourceMethods,
130130
IEnumerable<NonResourceMethod> nonResourceMethods)
131131
{
132132
// walk through all resources to figure out their scopes
@@ -144,9 +144,22 @@ static Dictionary<ResourceScope, ResourcesAndNonResourceMethodsInScope> BuildRes
144144
resourcesAndMethodsPerScope[metadata.ResourceScope].ResourceClients.Add(resourceClient);
145145
}
146146
}
147-
foreach (var resourceMethod in resourceMethods)
147+
foreach (var (metadata, category) in resourceMethods)
148148
{
149-
resourcesAndMethodsPerScope[resourceMethod.OperationScope].ResourceMethods.Add(resourceMethod);
149+
// find the resource
150+
var resource = resourceDict[metadata];
151+
// the resource methods
152+
foreach (var resourceMethod in category.MethodsInExtension)
153+
{
154+
var resourcesAndMethodsInThisScope = resourcesAndMethodsPerScope[resourceMethod.OperationScope];
155+
if (!resourcesAndMethodsInThisScope.ResourceMethods.TryGetValue(resource, out var methods))
156+
{
157+
methods = new List<ResourceMethod>();
158+
resourcesAndMethodsInThisScope.ResourceMethods[resource] = methods;
159+
}
160+
// add this method into the list
161+
((List<ResourceMethod>)methods).Add(resourceMethod);
162+
}
150163
}
151164
foreach (var nonResourceMethod in nonResourceMethods)
152165
{
@@ -251,7 +264,7 @@ internal bool TryGetResourceClientProvider(CSharpType resourceDataType, [MaybeNu
251264

252265
private record ResourcesAndNonResourceMethodsInScope(
253266
List<ResourceClientProvider> ResourceClients,
254-
List<ResourceMethod> ResourceMethods,
267+
Dictionary<ResourceClientProvider, IReadOnlyList<ResourceMethod>> ResourceMethods,
255268
List<NonResourceMethod> NonResourceMethods);
256269
}
257270
}

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Providers/MockableArmClientProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ namespace Azure.Generator.Management.Providers
1616
internal sealed class MockableArmClientProvider : MockableResourceProvider
1717
{
1818
// TODO -- we also need to put operations here when we want to support scope resources/operations https://github.com/Azure/azure-sdk-for-net/issues/51821
19-
public MockableArmClientProvider(IReadOnlyList<ResourceClientProvider> resources) : base(typeof(ArmClient), RequestPathPattern.Tenant, resources, [], [])
19+
public MockableArmClientProvider(IReadOnlyList<ResourceClientProvider> resources)
20+
: base(typeof(ArmClient), RequestPathPattern.Tenant, resources, new Dictionary<ResourceClientProvider, IReadOnlyList<ResourceMethod>>(), [])
2021
{
2122
}
2223

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Providers/MockableResourceProvider.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Azure.Generator.Management.Providers
2525
internal class MockableResourceProvider : TypeProvider
2626
{
2727
private protected readonly IReadOnlyList<ResourceClientProvider> _resources;
28-
private protected readonly IReadOnlyList<ResourceMethod> _resourceMethods;
28+
private protected readonly IReadOnlyDictionary<ResourceClientProvider, IReadOnlyList<ResourceMethod>> _resourceMethods;
2929
private protected readonly IReadOnlyList<NonResourceMethod> _nonResourceMethods;
3030
private readonly Dictionary<InputClient, RestClientInfo> _clientInfos;
3131

@@ -38,28 +38,27 @@ internal class MockableResourceProvider : TypeProvider
3838
/// <param name="resources">the resources in this scope.</param>
3939
/// <param name="resourceMethods">the resource methods that belong to this scope.</param>
4040
/// <param name="nonResourceMethods">the non-resource methods that belong to this scope.</param>
41-
public MockableResourceProvider(ResourceScope resourceScope, IReadOnlyList<ResourceClientProvider> resources, IReadOnlyList<ResourceMethod> resourceMethods, IReadOnlyList<NonResourceMethod> nonResourceMethods)
41+
public MockableResourceProvider(ResourceScope resourceScope, IReadOnlyList<ResourceClientProvider> resources, IReadOnlyDictionary<ResourceClientProvider, IReadOnlyList<ResourceMethod>> resourceMethods, IReadOnlyList<NonResourceMethod> nonResourceMethods)
4242
: this(ResourceHelpers.GetArmCoreTypeFromScope(resourceScope), RequestPathPattern.GetFromScope(resourceScope), resources, resourceMethods, nonResourceMethods)
4343
{
4444
}
4545

46-
private protected MockableResourceProvider(CSharpType armCoreType, RequestPathPattern contextualPath, IReadOnlyList<ResourceClientProvider> resources, IReadOnlyList<ResourceMethod> resourceMethods, IReadOnlyList<NonResourceMethod> nonResourceMethods)
46+
private protected MockableResourceProvider(CSharpType armCoreType, RequestPathPattern contextualPath, IReadOnlyList<ResourceClientProvider> resources, IReadOnlyDictionary<ResourceClientProvider, IReadOnlyList<ResourceMethod>> resourceMethods, IReadOnlyList<NonResourceMethod> nonResourceMethods)
4747
{
4848
_resources = resources;
4949
_resourceMethods = resourceMethods;
5050
_nonResourceMethods = nonResourceMethods;
5151
ArmCoreType = armCoreType;
5252
_contextualPath = contextualPath;
53-
_clientInfos = BuildRestClientInfos(resourceMethods, nonResourceMethods, this);
53+
_clientInfos = BuildRestClientInfos(resourceMethods.Values.SelectMany(m => m).Select(m => m.InputClient).Concat(nonResourceMethods.Select(m => m.InputClient)), this);
5454
}
5555

5656
private static Dictionary<InputClient, RestClientInfo> BuildRestClientInfos(
57-
IReadOnlyList<ResourceMethod> resourceMethods,
58-
IReadOnlyList<NonResourceMethod> nonResourceMethods,
57+
IEnumerable<InputClient> inputClients,
5958
TypeProvider enclosingType)
6059
{
6160
var clientInfos = new Dictionary<InputClient, RestClientInfo>();
62-
foreach (var inputClient in resourceMethods.Select(m => m.InputClient).Concat(nonResourceMethods.Select(m => m.InputClient)))
61+
foreach (var inputClient in inputClients)
6362
{
6463
if (clientInfos.ContainsKey(inputClient))
6564
{
@@ -184,10 +183,13 @@ protected override MethodProvider[] BuildMethods()
184183
methods.AddRange(BuildMethodsForResource(resource));
185184
}
186185

187-
foreach (var method in _resourceMethods)
186+
foreach (var (resource, resourceMethods) in _resourceMethods)
188187
{
189-
methods.Add(BuildServiceMethod(method.InputMethod, method.InputClient, true));
190-
methods.Add(BuildServiceMethod(method.InputMethod, method.InputClient, false));
188+
foreach (var resourceMethod in resourceMethods)
189+
{
190+
methods.Add(BuildResourceServiceMethod(resource, resourceMethod, true));
191+
methods.Add(BuildResourceServiceMethod(resource, resourceMethod, false));
192+
}
191193
}
192194

193195
foreach (var method in _nonResourceMethods)
@@ -276,13 +278,20 @@ static MethodProvider BuildGetMethod(TypeProvider enclosingType, MethodProvider
276278
}
277279
}
278280

279-
private MethodProvider BuildServiceMethod(InputServiceMethod method, InputClient inputClient, bool isAsync)
281+
private MethodProvider BuildResourceServiceMethod(ResourceClientProvider resource, ResourceMethod resourceMethod, bool isAsync)
282+
{
283+
var methodName = ResourceHelpers.GetExtensionOperationMethodName(resourceMethod.Kind, resource.ResourceName, isAsync);
284+
285+
return BuildServiceMethod(resourceMethod.InputMethod, resourceMethod.InputClient, isAsync, methodName);
286+
}
287+
288+
private MethodProvider BuildServiceMethod(InputServiceMethod method, InputClient inputClient, bool isAsync, string? methodName = null)
280289
{
281290
var clientInfo = _clientInfos[inputClient];
282291
return method switch
283292
{
284-
InputPagingServiceMethod pagingMethod => new PageableOperationMethodProvider(this, _contextualPath, clientInfo, pagingMethod, isAsync),
285-
_ => new ResourceOperationMethodProvider(this, _contextualPath, clientInfo, method, isAsync)
293+
InputPagingServiceMethod pagingMethod => new PageableOperationMethodProvider(this, _contextualPath, clientInfo, pagingMethod, isAsync, methodName: methodName),
294+
_ => new ResourceOperationMethodProvider(this, _contextualPath, clientInfo, method, isAsync, methodName: methodName)
286295
};
287296
}
288297

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Utilities/ResourceHelpers.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Azure.Generator.Management.Models;
55
using Azure.ResourceManager.ManagementGroups;
66
using Azure.ResourceManager.Resources;
7+
using Humanizer;
78
using Microsoft.TypeSpec.Generator.Input.Extensions;
89
using Microsoft.TypeSpec.Generator.Primitives;
910
using Microsoft.TypeSpec.Generator.Providers;
@@ -61,6 +62,22 @@ public static string GetRestClientPropertyName(string clientName)
6162
};
6263
}
6364

65+
/// <summary>
66+
/// Gets the appropriate method name for an extension operation based on its kind and corresponding resource name.
67+
/// </summary>
68+
/// <param name="operationKind">The kind of resource operation to perform (e.g., List, Create).</param>
69+
/// <param name="resourceName">The name of the resource for which the operation is being performed.</param>
70+
/// <param name="isAsync">Whether the method should be asynchronous.</param>
71+
/// <returns>The method name to use for the extension operation, or null if no override is needed.</returns>
72+
public static string? GetExtensionOperationMethodName(ResourceOperationKind operationKind, string resourceName, bool isAsync)
73+
{
74+
return operationKind switch
75+
{
76+
ResourceOperationKind.List => isAsync ? $"Get{resourceName.Pluralize()}Async" : $"Get{resourceName.Pluralize()}",
77+
_ => null
78+
};
79+
}
80+
6481
public static string GetDiagnosticScope(TypeProvider enclosingType, string methodName, bool isAsync)
6582
{
6683
var rawMethodName = isAsync && methodName.EndsWith("Async") ? methodName[..^5] : methodName; // trim "Async" if the method is async method

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

Lines changed: 4 additions & 4 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/Extensions/MockableMgmtTypeSpecSubscriptionResource.cs

Lines changed: 2 additions & 2 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/ZoosGetBySubscriptionAsyncCollectionResultOfT.cs

Lines changed: 1 addition & 1 deletion
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/ZoosGetBySubscriptionCollectionResultOfT.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)