Skip to content

[http-client-csharp-mgmt] code refactoring #50205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.TypeSpec.Generator.Expressions;
using Microsoft.TypeSpec.Generator.Input;
using Microsoft.TypeSpec.Generator.Primitives;
using Microsoft.TypeSpec.Generator.Providers;
using Microsoft.TypeSpec.Generator.Statements;
using System.Collections.Generic;
using static Microsoft.TypeSpec.Generator.Snippets.Snippet;

namespace Azure.Generator.Management.Providers
{
internal class ExistsOperationMethodProvider(
ResourceClientProvider resourceClientProvider,
InputServiceMethod method,
MethodProvider convenienceMethod,
MethodSignature signature,
bool isAsync) : ResourceOperationMethodProvider(resourceClientProvider, method, convenienceMethod, signature, isAsync)
{
protected override IReadOnlyList<MethodBodyStatement> BuildReturnStatements(ValueExpression responseVariable, MethodSignature signature)
{
// For Exists methods, we check if Value is not null and return a boolean
var returnValueExpression = responseVariable.Property("Value").NotEqual(Null);

return [
Return(
Static(typeof(Response)).Invoke(
nameof(Response.FromValue),
returnValueExpression,
responseVariable.Invoke("GetRawResponse")
)
)
];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.TypeSpec.Generator.Expressions;
using Microsoft.TypeSpec.Generator.Input;
using Microsoft.TypeSpec.Generator.Primitives;
using Microsoft.TypeSpec.Generator.Providers;
using Microsoft.TypeSpec.Generator.Statements;
using System.Collections.Generic;
using static Microsoft.TypeSpec.Generator.Snippets.Snippet;

namespace Azure.Generator.Management.Providers
{
internal class GetIfExistsOperationMethodProvider(
ResourceClientProvider resourceClientProvider,
InputServiceMethod method,
MethodProvider convenienceMethod,
MethodSignature signature,
bool isAsync) : ResourceOperationMethodProvider(resourceClientProvider, method, convenienceMethod, signature, isAsync)
{
private readonly ResourceClientProvider _resourceClient = resourceClientProvider;

protected override IReadOnlyList<MethodBodyStatement> BuildReturnStatements(ValueExpression responseVariable, MethodSignature signature)
{
var resourceClientCSharpType = _resourceClient.ResourceClientCSharpType;

List<MethodBodyStatement> statements =
[
new IfStatement(responseVariable.Property("Value").Equal(Null))
{
Return(
New.Instance(
new CSharpType(typeof(NoValueResponse<>), resourceClientCSharpType),
responseVariable.Invoke("GetRawResponse")
)
)
}
];

var returnValueExpression = New.Instance(resourceClientCSharpType, This.Property("Client"), responseVariable.Property("Value"));
statements.Add(
Return(
Static(typeof(Response)).Invoke(
nameof(Response.FromValue),
returnValueExpression,
responseVariable.Invoke("GetRawResponse")
)
)
);

return statements;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Azure.Core;
using Azure.Generator.Management.Primitives;
using Azure.Generator.Primitives;
using Azure.ResourceManager;
using Microsoft.TypeSpec.Generator.ClientModel.Providers;
using Microsoft.TypeSpec.Generator.Primitives;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.TypeSpec.Generator.Input;
using Microsoft.TypeSpec.Generator.Primitives;
using Microsoft.TypeSpec.Generator.Providers;
using Microsoft.TypeSpec.Generator.Statements;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -61,7 +60,7 @@ _getAll is null

protected override PropertyProvider[] BuildProperties() => [];

protected override FieldProvider[] BuildFields() => [_clientDiagonosticsField, _restClientField];
protected override FieldProvider[] BuildFields() => [_clientDiagnosticsField, _restClientField];

protected override ConstructorProvider[] BuildConstructors()
=> [ConstructorProviderHelper.BuildMockingConstructor(this), BuildResourceIdentifierConstructor()];
Expand All @@ -70,7 +69,7 @@ protected override ConstructorProvider[] BuildConstructors()

protected override ValueExpression ResourceTypeExpression => Static(_resource.Type).Property("ResourceType");

protected override CSharpType ResourceClientCSharpType => _resource.Type;
protected internal override CSharpType ResourceClientCSharpType => _resource.Type;

protected override MethodProvider[] BuildMethods() => [BuildValidateResourceIdMethod(), .. BuildCreateOrUpdateMethods(), .. BuildGetMethods(), .. BuildGetAllMethods(), .. BuildExistsMethods(), .. BuildGetIfExistsMethods(), .. BuildEnumeratorMethods()];

Expand Down Expand Up @@ -189,7 +188,9 @@ private List<MethodProvider> BuildExistsMethods()
convenienceMethod.Signature.GenericParameterConstraints,
convenienceMethod.Signature.ExplicitInterface,
convenienceMethod.Signature.NonDocumentComment);
result.Add(BuildOperationMethodCore(_get, convenienceMethod, signature, isAsync, IsReturnTypeGeneric(_get)));

var existsMethodProvider = new ExistsOperationMethodProvider(this, _get, convenienceMethod, signature, isAsync);
result.Add(existsMethodProvider.Build());
}

return result;
Expand Down Expand Up @@ -218,7 +219,9 @@ private List<MethodProvider> BuildGetIfExistsMethods()
convenienceMethod.Signature.GenericParameterConstraints,
convenienceMethod.Signature.ExplicitInterface,
convenienceMethod.Signature.NonDocumentComment);
result.Add(BuildOperationMethodCore(_get, convenienceMethod, signature, isAsync, IsReturnTypeGeneric(_get)));

var getIfExistsMethodProvider = new GetIfExistsOperationMethodProvider(this, _get, convenienceMethod, signature, isAsync);
result.Add(getIfExistsMethodProvider.Build());
}

return result;
Expand All @@ -232,40 +235,5 @@ protected override bool SkipMethodParameter(ParameterProvider parameter)
}
return ContextualParameters.Take(ContextualParameters.Count - 1).Any(p => p == parameter.Name);
}

protected override MethodBodyStatement BuildReturnStatements(ValueExpression responseVariable, MethodSignature signature)
{
if (signature.Name == "GetIfExists" || signature.Name == "GetIfExistsAsync")
{
return BuildReturnStatementsForGetIfExists(responseVariable, signature);
}
if (signature.Name == "Exists" || signature.Name == "ExistsAsync")
{
return BuildReturnStatementsForExists(responseVariable);
}

return base.BuildReturnStatements(responseVariable, signature);
}

private MethodBodyStatement BuildReturnStatementsForGetIfExists(ValueExpression responseVariable, MethodSignature signature)
{
List<MethodBodyStatement> statements =
[
new IfStatement(responseVariable.Property("Value").Equal(Null))
{
Return(New.Instance(new CSharpType(typeof(NoValueResponse<>), _resource.Type), responseVariable.Invoke("GetRawResponse")))
}
];
var returnValueExpression = New.Instance(ResourceClientCSharpType, This.Property("Client"), responseVariable.Property("Value"));
statements.Add(Return(Static(typeof(Response)).Invoke(nameof(Response.FromValue), returnValueExpression, responseVariable.Invoke("GetRawResponse"))));

return statements;
}

private MethodBodyStatement BuildReturnStatementsForExists(ValueExpression responseVariable)
{
var returnValueExpression = responseVariable.Property("Value").NotEqual(Null);
return Return(Static(typeof(Response)).Invoke(nameof(Response.FromValue), returnValueExpression, responseVariable.Invoke("GetRawResponse")));
}
}
}
Loading