Skip to content

Commit bd5b115

Browse files
Refactor ResourceClientProvider and ResourceCollectionClientProvider (Azure#51169)
--------- Co-authored-by: Copilot <[email protected]>
1 parent b30b72d commit bd5b115

File tree

49 files changed

+758
-452
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+758
-452
lines changed

eng/packages/http-client-csharp-mgmt/eng/scripts/Generate.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
param(
33
$filter,
44
[bool]$Stubbed = $true,
5-
[bool]$LaunchOnly = $false
5+
[bool]$LaunchOnly = $false,
6+
[bool]$Debug = $false
67
)
78

89
Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force;
@@ -20,7 +21,7 @@ if (-not $LaunchOnly) {
2021

2122
$mgmtTypespecTestProject = Join-Path $testProjectsLocalDir "Mgmt-TypeSpec"
2223

23-
Invoke (Get-Mgmt-TspCommand "$mgmtTypespecTestProject/main.tsp" $mgmtTypespecTestProject)
24+
Invoke (Get-Mgmt-TspCommand "$mgmtTypespecTestProject/main.tsp" $mgmtTypespecTestProject -debug:$Debug)
2425

2526
# exit if the generation failed
2627
if ($LASTEXITCODE -ne 0) {

eng/packages/http-client-csharp-mgmt/eng/scripts/Generation.psm1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ function Get-Mgmt-TspCommand {
2525
[string]$specFile,
2626
[string]$generationDir,
2727
[bool]$generateStub = $false,
28-
[string]$apiVersion = $null
28+
[string]$apiVersion = $null,
29+
[bool]$debug = $false
2930
)
3031
$command = "npx tsp compile $specFile"
3132
$command += " --trace @azure-typespec/http-client-csharp-mgmt"
@@ -46,6 +47,10 @@ function Get-Mgmt-TspCommand {
4647

4748
$command += " --option @azure-typespec/http-client-csharp-mgmt.new-project=true"
4849

50+
if ($debug) {
51+
$command += " --option @azure-typespec/http-client-csharp-mgmt.debug=true"
52+
}
53+
4954
return $command
5055
}
5156

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using Azure.Core;
5+
using Azure.Generator.Management.Snippets;
6+
using Azure.Generator.Management.Utilities;
7+
using Microsoft.TypeSpec.Generator.Primitives;
8+
using Microsoft.TypeSpec.Generator.Providers;
9+
using Microsoft.TypeSpec.Generator.Snippets;
410
using System;
511
using System.Collections;
612
using System.Collections.Generic;
13+
using System.Diagnostics;
714
using System.Diagnostics.CodeAnalysis;
815
using System.Linq;
916

1017
namespace Azure.Generator.Management.Models
1118
{
19+
/// <summary>
20+
/// This class provides the pattern of an operation request path.
21+
/// </summary>
1222
internal class RequestPathPattern : IEquatable<RequestPathPattern>, IReadOnlyList<RequestPathSegment>
1323
{
1424
private const string ProviderPath = "/subscriptions/{subscriptionId}/providers/{resourceProviderNamespace}";
@@ -162,5 +172,26 @@ public static implicit operator string(RequestPathPattern requestPath)
162172
{
163173
return requestPath._path;
164174
}
175+
176+
private IReadOnlyDictionary<string, ContextualParameter>? _contextualParameters;
177+
178+
/// <summary>
179+
/// Get the corresponding contextual parameter in this request path for a provided parameter.
180+
/// </summary>
181+
/// <param name="parameter"></param>
182+
/// <param name="contextualParameter"></param>
183+
/// <returns></returns>
184+
public bool TryGetContextualParameter(ParameterProvider parameter, [MaybeNullWhen(false)] out ContextualParameter contextualParameter)
185+
{
186+
contextualParameter = null;
187+
if (parameter.Location != ParameterLocation.Path)
188+
{
189+
return false;
190+
}
191+
192+
_contextualParameters ??= ContextualParameterBuilder.BuildContextualParameters(this).ToDictionary(p => p.VariableName);
193+
194+
return _contextualParameters.TryGetValue(parameter.WireInfo.SerializedName, out contextualParameter);
195+
}
165196
}
166197
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private MethodProvider BuildGetResourceIdMethodForResource(ResourceClientProvide
4343

4444
var body = new MethodBodyStatement[]
4545
{
46-
Static(resource.Type).Invoke(ResourceClientProvider.ValidateResourceIdMethodName, idParameter).Terminate(),
46+
Static(resource.Type).Invoke("ValidateResourceId", idParameter).Terminate(),
4747
Return(New.Instance(resource.Type,
4848
[
4949
This.As<ArmResource>().Client(),

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private IEnumerable<MethodProvider> BuildMethodsForResource(ResourceClientProvid
7676
if (resource.IsSingleton)
7777
{
7878
var resourceMethodSignature = new MethodSignature(
79-
$"Get{resource.SpecName}",
79+
$"Get{resource.ResourceName}",
8080
$"Gets an object representing a {resource.Type:C} along with the instance operations that can be performed on it in the {ArmCoreType:C}.",
8181
MethodSignatureModifiers.Public | MethodSignatureModifiers.Virtual,
8282
resource.Type,
@@ -97,7 +97,7 @@ private IEnumerable<MethodProvider> BuildMethodsForResource(ResourceClientProvid
9797
{
9898
var collection = resource.ResourceCollection!;
9999
// the first method is returning the collection
100-
var pluralOfResourceName = resource.SpecName.Pluralize();
100+
var pluralOfResourceName = resource.ResourceName.Pluralize();
101101
var collectionMethodSignature = new MethodSignature(
102102
$"Get{pluralOfResourceName}",
103103
$"Gets a collection of {pluralOfResourceName} in the {ArmCoreType:C}",
@@ -119,13 +119,13 @@ private IEnumerable<MethodProvider> BuildMethodsForResource(ResourceClientProvid
119119
if (getMethod is not null)
120120
{
121121
// we should be sure that this would never be null, but this null check here is just ensuring that we never crash
122-
yield return BuildGetMethod(this, getMethod, collectionMethodSignature, $"Get{collection.SpecName}");
122+
yield return BuildGetMethod(this, getMethod, collectionMethodSignature, $"Get{resource.ResourceName}");
123123
}
124124

125125
if (getAsyncMethod is not null)
126126
{
127127
// we should be sure that this would never be null, but this null check here is just ensuring that we never crash
128-
yield return BuildGetMethod(this, getAsyncMethod, collectionMethodSignature, $"Get{collection.SpecName}Async");
128+
yield return BuildGetMethod(this, getAsyncMethod, collectionMethodSignature, $"Get{resource.ResourceName}Async");
129129
}
130130

131131
static MethodProvider BuildGetMethod(TypeProvider enclosingType, MethodProvider resourceGetMethod, MethodSignature collectionGetSignature, string methodName)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using Azure.Generator.Management.Utilities;
5+
using Microsoft.TypeSpec.Generator.ClientModel.Providers;
56
using Microsoft.TypeSpec.Generator.Expressions;
67
using Microsoft.TypeSpec.Generator.Input;
78
using Microsoft.TypeSpec.Generator.Primitives;
@@ -13,10 +14,13 @@
1314
namespace Azure.Generator.Management.Providers.OperationMethodProviders
1415
{
1516
internal class ExistsOperationMethodProvider(
16-
ResourceClientProvider resourceClientProvider,
17+
ResourceCollectionClientProvider collection,
18+
ClientProvider restClient,
1719
InputServiceMethod method,
1820
MethodProvider convenienceMethod,
19-
bool isAsync) : ResourceOperationMethodProvider(resourceClientProvider, method, convenienceMethod, isAsync)
21+
FieldProvider clientDiagnosticsField,
22+
FieldProvider restClientField,
23+
bool isAsync) : ResourceOperationMethodProvider(collection, collection.ContextualPath, restClient, method, convenienceMethod, clientDiagnosticsField, restClientField, isAsync)
2024
{
2125
protected override MethodSignature CreateSignature()
2226
{

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using Azure.Generator.Management.Models;
5+
using Microsoft.TypeSpec.Generator.ClientModel.Providers;
46
using Microsoft.TypeSpec.Generator.Expressions;
57
using Microsoft.TypeSpec.Generator.Input;
68
using Microsoft.TypeSpec.Generator.Primitives;
@@ -12,16 +14,17 @@
1214
namespace Azure.Generator.Management.Providers.OperationMethodProviders
1315
{
1416
internal class GetAllOperationMethodProvider(
15-
ResourceCollectionClientProvider resourceCollectionClientProvider,
17+
ResourceCollectionClientProvider collection,
18+
ClientProvider restClient,
1619
InputServiceMethod method,
1720
MethodProvider convenienceMethod,
18-
bool isAsync) : ResourceOperationMethodProvider(resourceCollectionClientProvider, method, convenienceMethod, isAsync)
21+
FieldProvider clientDiagnosticsField,
22+
FieldProvider restClientField,
23+
bool isAsync) : ResourceOperationMethodProvider(collection, collection.ContextualPath, restClient, method, convenienceMethod, clientDiagnosticsField, restClientField, isAsync)
1924
{
20-
private readonly ResourceCollectionClientProvider _resourceCollectionClientProvider = resourceCollectionClientProvider;
21-
2225
protected override MethodSignature CreateSignature()
2326
{
24-
var resourceType = _resourceCollectionClientProvider.ResourceClientCSharpType;
27+
var resourceType = _resource!.Type;
2528
var returnType = _isAsync
2629
? new CSharpType(typeof(AsyncPageable<>), resourceType)
2730
: new CSharpType(typeof(Pageable<>), resourceType);

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using Azure.Generator.Management.Utilities;
5+
using Microsoft.TypeSpec.Generator.ClientModel.Providers;
56
using Microsoft.TypeSpec.Generator.Expressions;
67
using Microsoft.TypeSpec.Generator.Input;
78
using Microsoft.TypeSpec.Generator.Primitives;
@@ -13,14 +14,17 @@
1314
namespace Azure.Generator.Management.Providers.OperationMethodProviders
1415
{
1516
internal class GetIfExistsOperationMethodProvider(
16-
ResourceClientProvider resourceClientProvider,
17+
ResourceCollectionClientProvider collection,
18+
ClientProvider restClient,
1719
InputServiceMethod method,
1820
MethodProvider convenienceMethod,
19-
bool isAsync) : ResourceOperationMethodProvider(resourceClientProvider, method, convenienceMethod, isAsync)
21+
FieldProvider clientDiagnosticsField,
22+
FieldProvider restClientField,
23+
bool isAsync) : ResourceOperationMethodProvider(collection, collection.ContextualPath, restClient, method, convenienceMethod, clientDiagnosticsField, restClientField, isAsync)
2024
{
2125
protected override MethodSignature CreateSignature()
2226
{
23-
var returnType = new CSharpType(typeof(NullableResponse<>), _resourceClientProvider.ResourceClientCSharpType)
27+
var returnType = new CSharpType(typeof(NullableResponse<>), _resource.Type)
2428
.WrapAsync(_isAsync);
2529

2630
return new MethodSignature(
@@ -39,22 +43,20 @@ protected override MethodSignature CreateSignature()
3943

4044
protected override IReadOnlyList<MethodBodyStatement> BuildReturnStatements(ValueExpression responseVariable, MethodSignature signature)
4145
{
42-
var resourceClientCSharpType = _resourceClientProvider.ResourceClientCSharpType;
43-
4446
List<MethodBodyStatement> statements =
4547
[
4648
new IfStatement(responseVariable.Property("Value").Equal(Null))
4749
{
4850
Return(
4951
New.Instance(
50-
new CSharpType(typeof(NoValueResponse<>), resourceClientCSharpType),
52+
new CSharpType(typeof(NoValueResponse<>), _resource.Type),
5153
responseVariable.Invoke("GetRawResponse")
5254
)
5355
)
5456
}
5557
];
5658

57-
var returnValueExpression = New.Instance(resourceClientCSharpType, This.Property("Client"), responseVariable.Property("Value"));
59+
var returnValueExpression = New.Instance(_resource.Type, This.Property("Client"), responseVariable.Property("Value"));
5860
statements.Add(
5961
Return(
6062
Static(typeof(Response)).Invoke(

0 commit comments

Comments
 (0)