Skip to content

Commit e9e5eeb

Browse files
Copilotlive1206
andcommitted
Refactor: Move MethodProvider building logic to resource provider classes
- Added BuildFactoryMethodProvider() to ResourceClientProvider for singleton resources - Added BuildMockableGetMethodProvider() to ResourceCollectionClientProvider for collection resources - Removed BuildGetMethod() static method from MockableResourceProvider - MockableResourceProvider now delegates MethodProvider building to the resource providers - This centralizes the XmlDocs copying logic where the signature is built Co-authored-by: live1206 <[email protected]>
1 parent 8456325 commit e9e5eeb

File tree

3 files changed

+70
-43
lines changed

3 files changed

+70
-43
lines changed

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

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,14 @@ private IEnumerable<MethodProvider> BuildMethodsForResource(ResourceClientProvid
227227
{
228228
if (resource.IsSingleton)
229229
{
230-
var resourceMethodSignature = resource.FactoryMethodSignature;
231230
var bodyStatement = Return(
232231
New.Instance(
233232
resource.Type,
234233
This.As<ArmResource>().Client(),
235234
BuildSingletonResourceIdentifier(This.As<ArmResource>().Id(), resource.ResourceTypeValue, resource.SingletonResourceName!)));
236-
var method = new MethodProvider(
237-
resourceMethodSignature,
238-
bodyStatement,
239-
this);
240-
241-
// Copy the enhanced XML documentation from the singleton resource's Get method if available
242-
var enhancedXmlDocs = resource.GetEnhancedFactoryMethodXmlDocs();
243-
if (enhancedXmlDocs != null)
244-
{
245-
method.XmlDocs?.Update(summary: enhancedXmlDocs);
246-
}
247235

248-
yield return method;
236+
// Use the resource provider's method to build the factory method with enhanced XmlDocs
237+
yield return resource.BuildFactoryMethodProvider(this, bodyStatement);
249238
}
250239
else
251240
{
@@ -267,40 +256,13 @@ private IEnumerable<MethodProvider> BuildMethodsForResource(ResourceClientProvid
267256
if (getAsyncMethod is not null)
268257
{
269258
// we should be sure that this would never be null, but this null check here is just ensuring that we never crash
270-
yield return BuildGetMethod(this, getAsyncMethod, collectionMethodSignature, pathParameters, $"Get{resource.ResourceName}Async", collection);
259+
yield return collection.BuildMockableGetMethodProvider(this, getAsyncMethod, collectionMethodSignature, pathParameters, $"Get{resource.ResourceName}Async");
271260
}
272261

273262
if (getMethod is not null)
274263
{
275264
// we should be sure that this would never be null, but this null check here is just ensuring that we never crash
276-
yield return BuildGetMethod(this, getMethod, collectionMethodSignature, pathParameters, $"Get{resource.ResourceName}", collection);
277-
}
278-
279-
static MethodProvider BuildGetMethod(TypeProvider enclosingType, MethodProvider resourceGetMethod, MethodSignature collectionGetSignature, IReadOnlyList<ParameterProvider> pathParameters, string methodName, ResourceCollectionClientProvider collection)
280-
{
281-
var signature = new MethodSignature(
282-
methodName,
283-
resourceGetMethod.Signature.Description,
284-
resourceGetMethod.Signature.Modifiers,
285-
resourceGetMethod.Signature.ReturnType,
286-
resourceGetMethod.Signature.ReturnDescription,
287-
[.. pathParameters, .. resourceGetMethod.Signature.Parameters],
288-
Attributes: [new AttributeStatement(typeof(ForwardsClientCallsAttribute))]);
289-
290-
var method = new MethodProvider(
291-
signature,
292-
// invoke on a MethodSignature would handle the async extra calls and keyword automatically
293-
Return(This.Invoke(collectionGetSignature).Invoke(resourceGetMethod.Signature)),
294-
enclosingType);
295-
296-
// Copy the enhanced XML documentation from the collection's Get method
297-
var enhancedXmlDocs = collection.GetEnhancedGetMethodXmlDocs();
298-
if (enhancedXmlDocs != null)
299-
{
300-
method.XmlDocs?.Update(summary: enhancedXmlDocs);
301-
}
302-
303-
return method;
265+
yield return collection.BuildMockableGetMethodProvider(this, getMethod, collectionMethodSignature, pathParameters, $"Get{resource.ResourceName}");
304266
}
305267
}
306268
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,32 @@ private MethodSignature BuildFactoryMethodSignature()
181181
}
182182

183183
/// <summary>
184-
/// Gets the enhanced factory method signature with XML documentation from the resource's Get method.
184+
/// Builds a factory method provider for use in mockable resource extensions.
185+
/// For singleton resources, this includes enhanced XML documentation from the Get method.
186+
/// </summary>
187+
/// <param name="enclosingType">The enclosing type (mockable resource) for the method</param>
188+
/// <param name="bodyStatement">The method body statement</param>
189+
/// <returns>A MethodProvider with the factory signature and enhanced XmlDocs if applicable</returns>
190+
internal MethodProvider BuildFactoryMethodProvider(TypeProvider enclosingType, MethodBodyStatement bodyStatement)
191+
{
192+
var signature = FactoryMethodSignature;
193+
var method = new MethodProvider(signature, bodyStatement, enclosingType);
194+
195+
// For singleton resources, copy enhanced XML documentation from the Get method
196+
if (IsSingleton)
197+
{
198+
var getMethod = Methods.FirstOrDefault(m => m.Signature.Name == "Get");
199+
if (getMethod?.XmlDocs?.Summary != null)
200+
{
201+
method.XmlDocs?.Update(summary: getMethod.XmlDocs.Summary);
202+
}
203+
}
204+
205+
return method;
206+
}
207+
208+
/// <summary>
209+
/// Gets the enhanced XML documentation summary from the resource's Get method for singleton resources.
185210
/// This should be called after Methods are built to get the enhanced documentation.
186211
/// </summary>
187212
internal XmlDocSummaryStatement? GetEnhancedFactoryMethodXmlDocs()

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,5 +443,45 @@ public bool TryGetPrivateFieldParameter(ParameterProvider parameter, out FieldPr
443443
var getMethod = Methods.FirstOrDefault(m => m.Signature.Name == "Get");
444444
return getMethod?.XmlDocs?.Summary;
445445
}
446+
447+
/// <summary>
448+
/// Builds a Get method provider for use in mockable resource extensions with enhanced XML documentation.
449+
/// </summary>
450+
/// <param name="enclosingType">The enclosing type (mockable resource) for the method</param>
451+
/// <param name="resourceGetMethod">The source Get method from the collection</param>
452+
/// <param name="collectionGetSignature">The signature of the collection getter method</param>
453+
/// <param name="pathParameters">Path parameters for the method</param>
454+
/// <param name="methodName">The name of the method to build</param>
455+
/// <returns>A MethodProvider with enhanced XmlDocs</returns>
456+
internal MethodProvider BuildMockableGetMethodProvider(
457+
TypeProvider enclosingType,
458+
MethodProvider resourceGetMethod,
459+
MethodSignature collectionGetSignature,
460+
IReadOnlyList<ParameterProvider> pathParameters,
461+
string methodName)
462+
{
463+
var signature = new MethodSignature(
464+
methodName,
465+
resourceGetMethod.Signature.Description,
466+
resourceGetMethod.Signature.Modifiers,
467+
resourceGetMethod.Signature.ReturnType,
468+
resourceGetMethod.Signature.ReturnDescription,
469+
[.. pathParameters, .. resourceGetMethod.Signature.Parameters],
470+
Attributes: [new AttributeStatement(typeof(ForwardsClientCallsAttribute))]);
471+
472+
var method = new MethodProvider(
473+
signature,
474+
Return(This.Invoke(collectionGetSignature).Invoke(resourceGetMethod.Signature)),
475+
enclosingType);
476+
477+
// Copy the enhanced XML documentation from the collection's Get method
478+
var enhancedXmlDocs = GetEnhancedGetMethodXmlDocs();
479+
if (enhancedXmlDocs != null)
480+
{
481+
method.XmlDocs?.Update(summary: enhancedXmlDocs);
482+
}
483+
484+
return method;
485+
}
446486
}
447487
}

0 commit comments

Comments
 (0)