-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip to refactor methodprovidercollection (#3686)
contributes to #3213
- Loading branch information
Showing
13 changed files
with
532 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...rator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ScmMethodProviderCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.Generator.CSharp.Expressions; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Generator.CSharp.Input; | ||
using Microsoft.Generator.CSharp.Providers; | ||
using Microsoft.Generator.CSharp.Statements; | ||
using static Microsoft.Generator.CSharp.Snippets.Snippet; | ||
using Microsoft.Generator.CSharp.Snippets; | ||
|
||
namespace Microsoft.Generator.CSharp.ClientModel.Providers | ||
{ | ||
internal class ScmMethodProviderCollection : MethodProviderCollection | ||
{ | ||
public ScmMethodProviderCollection(InputOperation operation, TypeProvider enclosingType) | ||
: base(operation, enclosingType) | ||
{ | ||
} | ||
|
||
protected override IReadOnlyList<MethodProvider> BuildMethods() | ||
{ | ||
return | ||
[ | ||
// TO-DO: Add Protocol and Convenience methods https://github.com/Azure/autorest.csharp/issues/4585, https://github.com/Azure/autorest.csharp/issues/4586 | ||
BuildCreateMessageMethod(_operation, _enclosingType), | ||
BuildProtocolMethod(_operation, _enclosingType, false), | ||
BuildProtocolMethod(_operation, _enclosingType, true) | ||
]; | ||
} | ||
private static MethodProvider BuildProtocolMethod(InputOperation operation, TypeProvider enclosingType, bool isAsync) | ||
{ | ||
List<ParameterProvider> methodParameters = new(); | ||
foreach (InputParameter inputParam in operation.Parameters) | ||
{ | ||
if (inputParam.Kind != InputOperationParameterKind.Method) | ||
continue; | ||
methodParameters.Add(ClientModelPlugin.Instance.TypeFactory.CreateCSharpParam(inputParam)); | ||
} | ||
|
||
var methodModifier = MethodSignatureModifiers.Public | MethodSignatureModifiers.Virtual; | ||
if (isAsync) | ||
{ | ||
methodModifier |= MethodSignatureModifiers.Async; | ||
} | ||
var opName = operation.Name.ToCleanName(); | ||
var methodSignature = new MethodSignature( | ||
isAsync ? opName + "Async" : opName, | ||
FormattableStringHelpers.FromString(operation.Description), | ||
methodModifier, | ||
GetResponseType(operation.Responses, isAsync), | ||
null, | ||
Parameters: [.. methodParameters, KnownParameters.CancellationTokenParameter]); | ||
MethodBodyStatement[] methodBody = | ||
[ | ||
//UsingDeclare("message", typeof(RequestMess) | ||
//using PipelineMessage message = CreateSayHiRequest(headParameter, queryParameter, optionalQuery, options); | ||
isAsync ? new InvokeStaticPropertyExpression(typeof(Task), nameof(Task.CompletedTask), true).Terminate() : EmptyStatement | ||
]; | ||
|
||
return new MethodProvider(methodSignature, methodBody, enclosingType); | ||
} | ||
|
||
private static CSharpType? GetResponseType(IReadOnlyList<OperationResponse> responses, bool isAsync) | ||
{ | ||
var response = responses.FirstOrDefault(r => !r.IsErrorResponse); | ||
if (response is null || response.BodyType is null) | ||
return null; | ||
var returnType = ClientModelPlugin.Instance.TypeFactory.CreateCSharpType(response.BodyType); | ||
if (isAsync) | ||
{ | ||
returnType = returnType.WrapInTask(); | ||
} | ||
return returnType; | ||
} | ||
|
||
private static MethodProvider BuildCreateMessageMethod(InputOperation operation, TypeProvider enclosingType) | ||
{ | ||
// TO-DO: properly build method https://github.com/Azure/autorest.csharp/issues/4583 | ||
List<ParameterProvider> methodParameters = new(); | ||
foreach (var inputParam in operation.Parameters) | ||
{ | ||
methodParameters.Add(ClientModelPlugin.Instance.TypeFactory.CreateCSharpParam(inputParam)); | ||
} | ||
|
||
var methodModifier = MethodSignatureModifiers.Internal; | ||
var methodSignatureName = $"Create{operation.Name.ToCleanName()}Request"; | ||
var methodSignature = new MethodSignature(methodSignatureName, FormattableStringHelpers.FromString(operation.Description), methodModifier, null, null, Parameters: methodParameters); | ||
var methodBody = Throw(New.NotImplementedException(Literal("Method not implemented."))); | ||
|
||
return new MethodProvider(methodSignature, methodBody, enclosingType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...nt-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Mocks/MockTypeProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Microsoft.Generator.CSharp.Providers; | ||
|
||
namespace Microsoft.Generator.CSharp.ClientModel.Tests | ||
{ | ||
internal class MockTypeProvider : TypeProvider | ||
{ | ||
public override string RelativeFilePath => throw new NotImplementedException(); | ||
public override string Name => throw new NotImplementedException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...rp/generator/Microsoft.Generator.CSharp/src/Expressions/InvokeStaticPropertyExpression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.Generator.CSharp.Statements; | ||
|
||
namespace Microsoft.Generator.CSharp.Expressions | ||
{ | ||
public sealed record InvokeStaticPropertyExpression(CSharpType? ContainingType, string PropertyName, bool CallAsAsync = false) : ValueExpression | ||
{ | ||
internal override void Write(CodeWriter writer) | ||
{ | ||
writer.AppendRawIf("await ", CallAsAsync); | ||
if (ContainingType != null) | ||
{ | ||
writer.Append($"{ContainingType}."); | ||
} | ||
|
||
writer.AppendRaw(PropertyName); | ||
writer.AppendRawIf(".ConfigureAwait(false)", CallAsAsync); | ||
} | ||
|
||
private MethodBodyStatement? _terminated; | ||
public MethodBodyStatement Terminate() => _terminated ??= new ExpressionStatement(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.