Skip to content

Commit abbc9b2

Browse files
simplify implementation (#51745)
1 parent de62343 commit abbc9b2

File tree

9 files changed

+41
-66
lines changed

9 files changed

+41
-66
lines changed

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,20 @@ internal class ExistsOperationMethodProvider(
1818
ResourceCollectionClientProvider collection,
1919
RestClientInfo restClientInfo,
2020
InputServiceMethod method,
21-
MethodProvider convenienceMethod,
22-
bool isAsync) : ResourceOperationMethodProvider(collection, collection.ContextualPath, restClientInfo, method, convenienceMethod, isAsync)
21+
bool isAsync)
22+
: ResourceOperationMethodProvider(
23+
collection,
24+
collection.ContextualPath,
25+
restClientInfo,
26+
method,
27+
isAsync,
28+
methodName: isAsync ? "ExistsAsync" : "Exists",
29+
description: $"Checks to see if the resource exists in azure.")
2330
{
24-
protected override MethodSignature CreateSignature()
31+
protected override CSharpType BuildReturnType()
2532
{
26-
var returnType = new CSharpType(typeof(Response<>), typeof(bool))
33+
return new CSharpType(typeof(Response<>), typeof(bool))
2734
.WrapAsync(_isAsync);
28-
29-
return new MethodSignature(
30-
_isAsync ? "ExistsAsync" : "Exists",
31-
$"Checks to see if the resource exists in azure.",
32-
_convenienceMethod.Signature.Modifiers,
33-
returnType,
34-
_convenienceMethod.Signature.ReturnDescription,
35-
GetOperationMethodParameters(),
36-
_convenienceMethod.Signature.Attributes,
37-
_convenienceMethod.Signature.GenericArguments,
38-
_convenienceMethod.Signature.GenericParameterConstraints,
39-
_convenienceMethod.Signature.ExplicitInterface,
40-
_convenienceMethod.Signature.NonDocumentComment);
4135
}
4236

4337
protected override IReadOnlyList<MethodBodyStatement> BuildReturnStatements(ScopedApi<Response> responseVariable, MethodSignature signature)

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ internal class GetIfExistsOperationMethodProvider(
2020
ResourceCollectionClientProvider collection,
2121
RestClientInfo restClientInfo,
2222
InputServiceMethod method,
23-
MethodProvider convenienceMethod,
2423
bool isAsync)
2524
: ResourceOperationMethodProvider(
2625
collection,
2726
collection.ContextualPath,
2827
restClientInfo,
2928
method,
30-
convenienceMethod,
3129
isAsync,
3230
methodName: isAsync ? "GetIfExistsAsync" : "GetIfExists",
3331
description: $"Tries to get details for this resource from the service.")

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal class PageableOperationMethodProvider
2222
private readonly TypeProvider _enclosingType;
2323
private readonly RequestPathPattern _contextualPath;
2424
private readonly RestClientInfo _restClientInfo;
25-
private readonly InputServiceMethod _method;
25+
private readonly InputPagingServiceMethod _method;
2626
private readonly MethodProvider _convenienceMethod;
2727
private readonly bool _isAsync;
2828
private readonly CSharpType _itemType;
@@ -36,21 +36,19 @@ public PageableOperationMethodProvider(
3636
TypeProvider enclosingType,
3737
RequestPathPattern contextualPath,
3838
RestClientInfo restClientInfo,
39-
InputServiceMethod method,
40-
MethodProvider convenienceMethod,
39+
InputPagingServiceMethod method,
4140
bool isAsync,
42-
CSharpType itemType,
4341
ResourceOperationKind methodKind)
4442
{
4543
_enclosingType = enclosingType;
4644
_contextualPath = contextualPath;
4745
_restClientInfo = restClientInfo;
4846
_method = method;
49-
_convenienceMethod = convenienceMethod;
47+
_convenienceMethod = restClientInfo.RestClientProvider.GetConvenienceMethodByOperation(_method.Operation, isAsync);
5048
_isAsync = isAsync;
51-
_itemType = itemType;
49+
_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.
5250
InitializeTypeInfo(
53-
itemType,
51+
_itemType,
5452
ref _actualItemType!,
5553
ref _itemResourceClient
5654
);

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ internal class ResourceOperationMethodProvider
5252
/// <param name="contextualPath">The contextual path of the enclosing type. </param>
5353
/// <param name="restClientInfo">The rest client information containing the client provider and related fields. </param>
5454
/// <param name="method">The input service method that we are building from. </param>
55-
/// <param name="convenienceMethod">The corresponding convenience method provided by the generator framework. </param>
5655
/// <param name="isAsync">Whether this method is an async method. </param>
5756
/// <param name="methodName">Optional override for the method name. If not provided, uses the convenience method name. </param>
5857
/// <param name="description">Optional override for the method description. If not provided, uses the convenience method description.</param>
@@ -62,7 +61,6 @@ public ResourceOperationMethodProvider(
6261
RequestPathPattern contextualPath,
6362
RestClientInfo restClientInfo,
6463
InputServiceMethod method,
65-
MethodProvider convenienceMethod,
6664
bool isAsync,
6765
string? methodName = null,
6866
FormattableString? description = null,
@@ -72,15 +70,15 @@ public ResourceOperationMethodProvider(
7270
_contextualPath = contextualPath;
7371
_restClient = restClientInfo.RestClientProvider;
7472
_serviceMethod = method;
75-
_convenienceMethod = convenienceMethod;
7673
_isAsync = isAsync;
74+
_convenienceMethod = _restClient.GetConvenienceMethodByOperation(_serviceMethod.Operation, isAsync);
7775
InitializeLroFlags(
7876
_serviceMethod,
7977
forceLro: forceLro,
8078
ref _isLongRunningOperation,
8179
ref _isFakeLongRunningOperation);
82-
_methodName = methodName ?? convenienceMethod.Signature.Name;
83-
_description = description ?? convenienceMethod.Signature.Description;
80+
_methodName = methodName ?? _convenienceMethod.Signature.Name;
81+
_description = description ?? _convenienceMethod.Signature.Description;
8482
InitializeTypeInfo(
8583
_serviceMethod,
8684
ref _originalBodyType,

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33

44
using Azure.Generator.Management.Models;
55
using Microsoft.TypeSpec.Generator.Input;
6-
using Microsoft.TypeSpec.Generator.Providers;
76

87
namespace Azure.Generator.Management.Providers.OperationMethodProviders
98
{
109
internal class UpdateOperationMethodProvider(
1110
ResourceClientProvider resource,
1211
RestClientInfo restClientInfo,
1312
InputServiceMethod method,
14-
MethodProvider convenienceMethod,
15-
bool isAsync) : ResourceOperationMethodProvider(resource, resource.ContextualPath, restClientInfo, method, convenienceMethod, isAsync, methodName: isAsync ? "UpdateAsync" : "Update")
13+
bool isAsync) : ResourceOperationMethodProvider(resource, resource.ContextualPath, restClientInfo, method, isAsync, methodName: isAsync ? "UpdateAsync" : "Update")
1614
{
1715
}
1816
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,11 @@ protected override MethodProvider[] BuildMethods()
335335
var convenienceMethod = restClientInfo.RestClientProvider.GetConvenienceMethodByOperation(method.Operation, false);
336336
var asyncConvenienceMethod = restClientInfo.RestClientProvider.GetConvenienceMethodByOperation(method.Operation, true);
337337

338-
if (method is InputPagingServiceMethod)
338+
if (method is InputPagingServiceMethod pagingMethod)
339339
{
340340
// Use PageableOperationMethodProvider for InputPagingServiceMethod
341-
var itemType = convenienceMethod.Signature.ReturnType!.UnWrap();
342-
operationMethods.Add(new PageableOperationMethodProvider(this, ContextualPath, restClientInfo, method, convenienceMethod, false, itemType, methodKind));
343-
operationMethods.Add(new PageableOperationMethodProvider(this, ContextualPath, restClientInfo, method, asyncConvenienceMethod, true, itemType, methodKind));
341+
operationMethods.Add(new PageableOperationMethodProvider(this, ContextualPath, restClientInfo, pagingMethod, false, methodKind));
342+
operationMethods.Add(new PageableOperationMethodProvider(this, ContextualPath, restClientInfo, pagingMethod, true, methodKind));
344343

345344
continue;
346345
}
@@ -350,18 +349,18 @@ protected override MethodProvider[] BuildMethods()
350349

351350
if (isUpdateOperation)
352351
{
353-
updateMethodProvider = new UpdateOperationMethodProvider(this, restClientInfo, method, convenienceMethod, false);
352+
updateMethodProvider = new UpdateOperationMethodProvider(this, restClientInfo, method, false);
354353
operationMethods.Add(updateMethodProvider);
355354

356-
var updateAsyncMethodProvider = new UpdateOperationMethodProvider(this, restClientInfo, method, asyncConvenienceMethod, true);
355+
var updateAsyncMethodProvider = new UpdateOperationMethodProvider(this, restClientInfo, method, true);
357356
operationMethods.Add(updateAsyncMethodProvider);
358357
}
359358
else
360359
{
361360
var methodName = ResourceHelpers.GetOperationMethodName(methodKind, false);
362-
operationMethods.Add(new ResourceOperationMethodProvider(this, ContextualPath, restClientInfo, method, convenienceMethod, false, methodName, forceLro: isFakeLro));
361+
operationMethods.Add(new ResourceOperationMethodProvider(this, ContextualPath, restClientInfo, method, false, methodName, forceLro: isFakeLro));
363362
var asyncMethodName = ResourceHelpers.GetOperationMethodName(methodKind, true);
364-
operationMethods.Add(new ResourceOperationMethodProvider(this, ContextualPath, restClientInfo, method, asyncConvenienceMethod, true, asyncMethodName, forceLro: isFakeLro));
363+
operationMethods.Add(new ResourceOperationMethodProvider(this, ContextualPath, restClientInfo, method, true, asyncMethodName, forceLro: isFakeLro));
365364
}
366365
}
367366

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ private MethodProvider[] BuildGetAllMethods()
230230
}
231231

232232
// implement paging method GetAll
233-
var getAll = BuildGetAllMethod(false);
234-
var getAllAsync = BuildGetAllMethod(true);
233+
var getAll = BuildGetAllMethod(_getAll, false);
234+
var getAllAsync = BuildGetAllMethod(_getAll, true);
235235

236236
return [getAllAsync, getAll];
237237
}
@@ -273,17 +273,20 @@ private List<MethodProvider> BuildCreateOrUpdateMethods()
273273
{
274274
var convenienceMethod = restClientInfo.RestClientProvider.GetConvenienceMethodByOperation(_create!.Operation, isAsync);
275275
var methodName = ResourceHelpers.GetOperationMethodName(ResourceOperationKind.Create, isAsync);
276-
result.Add(new ResourceOperationMethodProvider(this, ContextualPath, restClientInfo, _create, convenienceMethod, isAsync, methodName, forceLro: true));
276+
result.Add(new ResourceOperationMethodProvider(this, ContextualPath, restClientInfo, _create, isAsync, methodName, forceLro: true));
277277
}
278278

279279
return result;
280280
}
281281

282-
private MethodProvider BuildGetAllMethod(bool isAsync)
282+
private MethodProvider BuildGetAllMethod(InputServiceMethod getAll, bool isAsync)
283283
{
284-
var restClientInfo = _resourceMetadata.GetRestClientForServiceMethod(_getAll!, _clientInfos);
285-
var convenienceMethod = restClientInfo.RestClientProvider.GetConvenienceMethodByOperation(_getAll!.Operation, isAsync);
286-
return new PageableOperationMethodProvider(this, this.ContextualPath, restClientInfo, _getAll, convenienceMethod, isAsync, Resource.ResourceData.Type, ResourceOperationKind.List);
284+
var restClientInfo = _resourceMetadata.GetRestClientForServiceMethod(getAll, _clientInfos);
285+
return getAll switch
286+
{
287+
InputPagingServiceMethod pagingGetAll => new PageableOperationMethodProvider(this, ContextualPath, restClientInfo, pagingGetAll, isAsync, ResourceOperationKind.List),
288+
_ => new ResourceOperationMethodProvider(this, ContextualPath, restClientInfo, getAll, isAsync, ResourceHelpers.GetOperationMethodName(ResourceOperationKind.List, isAsync))
289+
};
287290
}
288291

289292
private List<MethodProvider> BuildGetMethods()
@@ -298,7 +301,7 @@ private List<MethodProvider> BuildGetMethods()
298301
foreach (var isAsync in new List<bool> { true, false })
299302
{
300303
var convenienceMethod = restClientInfo.RestClientProvider.GetConvenienceMethodByOperation(_get!.Operation, isAsync);
301-
result.Add(new ResourceOperationMethodProvider(this, ContextualPath, restClientInfo, _get, convenienceMethod, isAsync));
304+
result.Add(new ResourceOperationMethodProvider(this, ContextualPath, restClientInfo, _get, isAsync));
302305
}
303306

304307
return result;
@@ -316,7 +319,7 @@ private List<MethodProvider> BuildExistsMethods()
316319
foreach (var isAsync in new List<bool> { true, false })
317320
{
318321
var convenienceMethod = restClientInfo.RestClientProvider.GetConvenienceMethodByOperation(_get!.Operation, isAsync);
319-
var existsMethodProvider = new ExistsOperationMethodProvider(this, restClientInfo, _get, convenienceMethod, isAsync);
322+
var existsMethodProvider = new ExistsOperationMethodProvider(this, restClientInfo, _get, isAsync);
320323
result.Add(existsMethodProvider);
321324
}
322325

@@ -335,7 +338,7 @@ private List<MethodProvider> BuildGetIfExistsMethods()
335338
foreach (var isAsync in new List<bool> { true, false })
336339
{
337340
var convenienceMethod = restClientInfo.RestClientProvider.GetConvenienceMethodByOperation(_get!.Operation, isAsync);
338-
var getIfExistsMethodProvider = new GetIfExistsOperationMethodProvider(this, restClientInfo, _get, convenienceMethod, isAsync);
341+
var getIfExistsMethodProvider = new GetIfExistsOperationMethodProvider(this, restClientInfo, _get, isAsync);
339342
result.Add(getIfExistsMethodProvider);
340343
}
341344

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,5 @@ public static CSharpType WrapResponse(this CSharpType? type, bool isLongRunning)
3131
: typeof(Response);
3232
}
3333
}
34-
35-
public static CSharpType UnWrap(this CSharpType type)
36-
{
37-
var currentType = type;
38-
39-
// Recursively unwrap any generic type with a single type argument
40-
// This will find the innermost type regardless of nesting level or specific generic types
41-
while (currentType.IsGenericType && currentType.Arguments.Count == 1)
42-
{
43-
currentType = currentType.Arguments[0];
44-
}
45-
46-
return currentType;
47-
}
4834
}
4935
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static string GetRestClientFieldName(string restClientName)
3232
return operationKind switch
3333
{
3434
ResourceOperationKind.Create => isAsync ? "CreateOrUpdateAsync" : "CreateOrUpdate",
35+
ResourceOperationKind.List => isAsync ? "GetAllAsync" : "GetAll",
3536
_ => null
3637
};
3738
}

0 commit comments

Comments
 (0)