Skip to content

Commit ee0a599

Browse files
authored
[http-client-csharp-mgmt] code refactoring (Azure#50205)
* refine method BuildOperationMethodTryStatement. * address remaining unresolved review comments from ealier PR. * remove useless comments. * fix typo. * new tspCodeModel.json. * regen tspCodeModel.json. * fix rebase issue. * address review comments. * fix. * address code review comments. * create new class ResourceOperationMethodProvider. * add two extension methods to CSharpType. * address new review comments from copilot. * further refine. * more refine. * Apply WrapAsync everywhere possible. * Replace SkipMethodParameter with ImplicitParameterNames property * introduce GetAllResourceOperationMethodProvider. * address new review comments. * rename GetAllResourceOperationMethodProvider to GetAllOperationMethodProvider * small refine. * address review comments. * add one TODO item inline in the code.
1 parent 1f3e8f5 commit ee0a599

File tree

11 files changed

+750
-302
lines changed

11 files changed

+750
-302
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Azure.Generator.Management.Utilities;
5+
using Microsoft.TypeSpec.Generator.Expressions;
6+
using Microsoft.TypeSpec.Generator.Input;
7+
using Microsoft.TypeSpec.Generator.Primitives;
8+
using Microsoft.TypeSpec.Generator.Providers;
9+
using Microsoft.TypeSpec.Generator.Statements;
10+
using System.Collections.Generic;
11+
using static Microsoft.TypeSpec.Generator.Snippets.Snippet;
12+
13+
namespace Azure.Generator.Management.Providers.OperationMethodProviders
14+
{
15+
internal class ExistsOperationMethodProvider(
16+
ResourceClientProvider resourceClientProvider,
17+
InputServiceMethod method,
18+
MethodProvider convenienceMethod,
19+
bool isAsync) : ResourceOperationMethodProvider(resourceClientProvider, method, convenienceMethod, isAsync)
20+
{
21+
protected override MethodSignature CreateSignature()
22+
{
23+
var returnType = new CSharpType(typeof(Response<>), typeof(bool))
24+
.WrapAsync(_isAsync);
25+
26+
return new MethodSignature(
27+
_isAsync ? "ExistsAsync" : "Exists",
28+
$"Checks to see if the resource exists in azure.",
29+
_convenienceMethod.Signature.Modifiers,
30+
returnType,
31+
_convenienceMethod.Signature.ReturnDescription,
32+
GetOperationMethodParameters(),
33+
_convenienceMethod.Signature.Attributes,
34+
_convenienceMethod.Signature.GenericArguments,
35+
_convenienceMethod.Signature.GenericParameterConstraints,
36+
_convenienceMethod.Signature.ExplicitInterface,
37+
_convenienceMethod.Signature.NonDocumentComment);
38+
}
39+
40+
protected override IReadOnlyList<MethodBodyStatement> BuildReturnStatements(ValueExpression responseVariable, MethodSignature signature)
41+
{
42+
// For Exists methods, we check if Value is not null and return a boolean
43+
var returnValueExpression = responseVariable.Property("Value").NotEqual(Null);
44+
45+
return [
46+
Return(
47+
Static(typeof(Response)).Invoke(
48+
nameof(Response.FromValue),
49+
returnValueExpression,
50+
responseVariable.Invoke("GetRawResponse")
51+
)
52+
)
53+
];
54+
}
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.TypeSpec.Generator.Expressions;
5+
using Microsoft.TypeSpec.Generator.Input;
6+
using Microsoft.TypeSpec.Generator.Primitives;
7+
using Microsoft.TypeSpec.Generator.Providers;
8+
using Microsoft.TypeSpec.Generator.Statements;
9+
using System;
10+
using static Microsoft.TypeSpec.Generator.Snippets.Snippet;
11+
12+
namespace Azure.Generator.Management.Providers.OperationMethodProviders
13+
{
14+
internal class GetAllOperationMethodProvider(
15+
ResourceCollectionClientProvider resourceCollectionClientProvider,
16+
InputServiceMethod method,
17+
MethodProvider convenienceMethod,
18+
bool isAsync) : ResourceOperationMethodProvider(resourceCollectionClientProvider, method, convenienceMethod, isAsync)
19+
{
20+
private readonly ResourceCollectionClientProvider _resourceCollectionClientProvider = resourceCollectionClientProvider;
21+
22+
protected override MethodSignature CreateSignature()
23+
{
24+
var resourceType = _resourceCollectionClientProvider.ResourceClientCSharpType;
25+
var returnType = _isAsync
26+
? new CSharpType(typeof(AsyncPageable<>), resourceType)
27+
: new CSharpType(typeof(Pageable<>), resourceType);
28+
29+
return new MethodSignature(
30+
_isAsync ? "GetAllAsync" : "GetAll",
31+
_convenienceMethod.Signature.Description,
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);
41+
}
42+
43+
protected override MethodBodyStatement[] BuildBodyStatements()
44+
{
45+
// TODO: implement paging method properly
46+
return [((KeywordExpression)ThrowExpression(New.Instance(typeof(NotImplementedException)))).Terminate()];
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Azure.Generator.Management.Utilities;
5+
using Microsoft.TypeSpec.Generator.Expressions;
6+
using Microsoft.TypeSpec.Generator.Input;
7+
using Microsoft.TypeSpec.Generator.Primitives;
8+
using Microsoft.TypeSpec.Generator.Providers;
9+
using Microsoft.TypeSpec.Generator.Statements;
10+
using System.Collections.Generic;
11+
using static Microsoft.TypeSpec.Generator.Snippets.Snippet;
12+
13+
namespace Azure.Generator.Management.Providers.OperationMethodProviders
14+
{
15+
internal class GetIfExistsOperationMethodProvider(
16+
ResourceClientProvider resourceClientProvider,
17+
InputServiceMethod method,
18+
MethodProvider convenienceMethod,
19+
bool isAsync) : ResourceOperationMethodProvider(resourceClientProvider, method, convenienceMethod, isAsync)
20+
{
21+
protected override MethodSignature CreateSignature()
22+
{
23+
var returnType = new CSharpType(typeof(NullableResponse<>), _resourceClientProvider.ResourceClientCSharpType)
24+
.WrapAsync(_isAsync);
25+
26+
return new MethodSignature(
27+
_isAsync ? "GetIfExistsAsync" : "GetIfExists",
28+
$"Tries to get details for this resource from the service.",
29+
_convenienceMethod.Signature.Modifiers,
30+
returnType,
31+
_convenienceMethod.Signature.ReturnDescription,
32+
GetOperationMethodParameters(),
33+
_convenienceMethod.Signature.Attributes,
34+
_convenienceMethod.Signature.GenericArguments,
35+
_convenienceMethod.Signature.GenericParameterConstraints,
36+
_convenienceMethod.Signature.ExplicitInterface,
37+
_convenienceMethod.Signature.NonDocumentComment);
38+
}
39+
40+
protected override IReadOnlyList<MethodBodyStatement> BuildReturnStatements(ValueExpression responseVariable, MethodSignature signature)
41+
{
42+
var resourceClientCSharpType = _resourceClientProvider.ResourceClientCSharpType;
43+
44+
List<MethodBodyStatement> statements =
45+
[
46+
new IfStatement(responseVariable.Property("Value").Equal(Null))
47+
{
48+
Return(
49+
New.Instance(
50+
new CSharpType(typeof(NoValueResponse<>), resourceClientCSharpType),
51+
responseVariable.Invoke("GetRawResponse")
52+
)
53+
)
54+
}
55+
];
56+
57+
var returnValueExpression = New.Instance(resourceClientCSharpType, This.Property("Client"), responseVariable.Property("Value"));
58+
statements.Add(
59+
Return(
60+
Static(typeof(Response)).Invoke(
61+
nameof(Response.FromValue),
62+
returnValueExpression,
63+
responseVariable.Invoke("GetRawResponse")
64+
)
65+
)
66+
);
67+
68+
return statements;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)