Skip to content

Commit

Permalink
migrate expressions & statements from autorest (#3402)
Browse files Browse the repository at this point in the history
This PR migrates the updates to the generator's expressions and
statements from autorest. It also updates the existing SCM expressions
to that of autorest's which including having to update to the
System.ClientModel dependency to `1.1.0-beta.2` to pull in the new
types.
  • Loading branch information
jorgerangel-msft authored May 21, 2024
1 parent 3c1c0d6 commit 3b4bd0b
Show file tree
Hide file tree
Showing 32 changed files with 116 additions and 317 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.ClientModel;
using Microsoft.Generator.CSharp.Expressions;

namespace Microsoft.Generator.CSharp.ClientModel.Expressions
{
internal sealed record ClientResultExpression(ValueExpression Untyped) : TypedValueExpression<ClientResult>(Untyped)
{
public ValueExpression Value => Property(nameof(ClientResult<object>.Value));
public BinaryDataExpression Content => throw new InvalidOperationException("Result does not have a Content property");
public StreamExpression ContentStream => throw new InvalidOperationException("Result does not have a ContentStream property");

public static ClientResultExpression FromResponse(PipelineResponseExpression response)
=> new(InvokeStatic(nameof(ClientResult.FromResponse), response));

public static ClientResultExpression FromValue(ValueExpression value, PipelineResponseExpression response)
=> new(InvokeStatic(nameof(ClientResult.FromValue), value, response));

public static ClientResultExpression FromValue(CSharpType explicitValueType, ValueExpression value, PipelineResponseExpression response)
=> new(new InvokeStaticMethodExpression(typeof(ClientResult), nameof(ClientResult.FromValue), new[] { value, response }, new[] { explicitValueType }));

public ClientResultExpression FromValue(ValueExpression value)
=> new(new InvokeStaticMethodExpression(typeof(ClientResult), nameof(ClientResult.FromValue), new[] { value, this }));

public ClientResultExpression FromValue(CSharpType explicitValueType, ValueExpression value)
=> new(new InvokeStaticMethodExpression(typeof(ClientResult), nameof(ClientResult.FromValue), new[] { value, this }, new[] { explicitValueType }));

public PipelineResponseExpression GetRawResponse() => new(Invoke(nameof(ClientResult<object>.GetRawResponse)));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.ClientModel.Primitives;
Expand All @@ -9,5 +9,9 @@ namespace Microsoft.Generator.CSharp.ClientModel.Expressions
internal sealed record PipelineMessageExpression(ValueExpression Untyped) : TypedValueExpression<PipelineMessage>(Untyped)
{
public PipelineRequestExpression Request => new(Property(nameof(PipelineMessage.Request)));

public PipelineResponseExpression Response => new(Property(nameof(PipelineMessage.Response)));

public BoolExpression BufferResponse => new(Property(nameof(PipelineMessage.BufferResponse)));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
Expand All @@ -11,8 +11,8 @@ namespace Microsoft.Generator.CSharp.ClientModel.Expressions
internal sealed record PipelineRequestExpression(ValueExpression Untyped) : TypedValueExpression<PipelineRequest>(Untyped)
{
public TypedValueExpression Uri => new FrameworkTypeExpression(typeof(Uri), Property(nameof(PipelineRequest.Uri)));
public RequestBodyExpression Content => new(Property(nameof(PipelineRequest.Content)));
public MethodBodyStatement SetMethod(string method) => new InvokeInstanceMethodStatement(Untyped, nameof(PipelineRequest.SetMethod), Literal(method));
public MethodBodyStatement SetHeaderValue(string name, StringExpression value) => new InvokeInstanceMethodStatement(Untyped, nameof(PipelineRequest.SetHeaderValue), Literal(name), value);
public MethodBodyStatement SetMethod(string method) => Assign(Untyped.Property("Method"), Literal(method));
public MethodBodyStatement SetHeaderValue(string name, StringExpression value)
=> new InvokeInstanceMethodStatement(Untyped.Property(nameof(PipelineRequest.Headers)), nameof(PipelineRequestHeaders.Set), Literal(name), value);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.ClientModel;
using System.ClientModel.Primitives;
using Microsoft.Generator.CSharp.Expressions;


Expand All @@ -10,6 +10,8 @@ namespace Microsoft.Generator.CSharp.ClientModel.Expressions
internal sealed record RequestOptionsExpression(ValueExpression Untyped) : TypedValueExpression<RequestOptions>(Untyped)
{
public static RequestOptionsExpression FromCancellationToken()
=> new(new InvokeStaticMethodExpression(null, nameof(FromCancellationToken), new ValueExpression[] { KnownParameters.CancellationTokenParameter }));
=> new(new InvokeStaticMethodExpression(null, "FromCancellationToken", new ValueExpression[] { KnownParameters.CancellationTokenParameter }));

public ValueExpression ErrorOptions => Property(nameof(RequestOptions.ErrorOptions));
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.ClientModel.Internal;
using System.ClientModel.Primitives;
using Microsoft.Generator.CSharp.Expressions;

Expand All @@ -12,21 +10,6 @@ internal partial class SystemExtensibleSnippets
{
internal class SystemModelSnippets : ModelSnippets
{
public override CSharpMethod BuildConversionToRequestBodyMethod(MethodSignatureModifiers modifiers)
{
return new CSharpMethod
(
new MethodSignature(ClientModelPlugin.Instance.Configuration.ApiTypes.ToRequestContentName, null, $"Convert into a {nameof(Utf8JsonRequestBody)}.", modifiers, typeof(RequestBody), null, Array.Empty<Parameter>()),
new[]
{
Snippets.Extensible.RestOperations.DeclareContentWithUtf8JsonWriter(out var requestContent, out var writer),
writer.WriteObjectValue(Snippets.This),
Snippets.Return(requestContent)
},
"default"
);
}

public override CSharpMethod BuildFromOperationResponseMethod(TypeProvider typeProvider, MethodSignatureModifiers modifiers)
{
var result = new Parameter("response", $"The result to deserialize the model from.", typeof(PipelineResponse), null, ValidationType.None, null);
Expand All @@ -41,8 +24,6 @@ public override CSharpMethod BuildFromOperationResponseMethod(TypeProvider typeP
"default"
);
}

public override TypedValueExpression InvokeToRequestBodyMethod(TypedValueExpression model) => new RequestBodyExpression(model.Invoke("ToRequestBody"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@ internal partial class SystemExtensibleSnippets
private class SystemRestOperationsSnippets : RestOperationsSnippets
{
public override StreamExpression GetContentStream(TypedValueExpression result)
=> new ResultExpression(result).GetRawResponse().ContentStream;
=> new ClientResultExpression(result).GetRawResponse().ContentStream;

public override TypedValueExpression GetTypedResponseFromValue(TypedValueExpression value, TypedValueExpression result)
{
return ResultExpression.FromValue(value, GetRawResponse(result));
return ClientResultExpression.FromValue(value, GetRawResponse(result));
}

public override TypedValueExpression GetTypedResponseFromModel(TypeProvider typeProvider, TypedValueExpression result)
{
var response = GetRawResponse(result);
var model = new InvokeStaticMethodExpression(typeProvider.Type, ClientModelPlugin.Instance.Configuration.ApiTypes.FromResponseName, new[] { response });
return ResultExpression.FromValue(model, response);
return ClientResultExpression.FromValue(model, response);
}

public override TypedValueExpression GetTypedResponseFromEnum(EnumType enumType, TypedValueExpression result)
{
var response = GetRawResponse(result);
return ResultExpression.FromValue(EnumExpression.ToEnum(enumType, response.Content.ToObjectFromJson(typeof(string))), response);
return ClientResultExpression.FromValue(EnumExpression.ToEnum(enumType, response.Content.ToObjectFromJson(typeof(string))), response);
}

public override TypedValueExpression GetTypedResponseFromBinaryData(Type responseType, TypedValueExpression result, string? contentType = null)
{
var rawResponse = GetRawResponse(result);
if (responseType == typeof(string) && contentType != null && FormattableStringHelpers.ToMediaType(contentType) == BodyMediaType.Text)
{
return ResultExpression.FromValue(rawResponse.Content.InvokeToString(), rawResponse);
return ClientResultExpression.FromValue(rawResponse.Content.InvokeToString(), rawResponse);
}
return responseType == typeof(BinaryData)
? ResultExpression.FromValue(rawResponse.Content, rawResponse)
: ResultExpression.FromValue(rawResponse.Content.ToObjectFromJson(responseType), rawResponse);
? ClientResultExpression.FromValue(rawResponse.Content, rawResponse)
: ClientResultExpression.FromValue(rawResponse.Content.ToObjectFromJson(responseType), rawResponse);
}

public override MethodBodyStatement DeclareHttpMessage(MethodSignatureBase createRequestMethodSignature, out TypedValueExpression message)
Expand All @@ -57,30 +57,16 @@ public override MethodBodyStatement DeclareHttpMessage(MethodSignatureBase creat

public override MethodBodyStatement DeclareContentWithUtf8JsonWriter(out TypedValueExpression content, out Utf8JsonWriterExpression writer)
{
var contentVar = new VariableReference(typeof(Utf8JsonRequestBody), "content");
var contentVar = new VariableReference(typeof(BinaryContent), "content");
content = contentVar;
writer = new Utf8JsonRequestBodyExpression(content).JsonWriter;
return Snippets.Var(contentVar, Snippets.New.Instance(typeof(Utf8JsonRequestBody)));
writer = new Utf8JsonWriterExpression(content.Property("JsonWriter"));
return Snippets.Var(contentVar, Snippets.New.Instance(typeof(BinaryContent)));
}

public override MethodBodyStatement InvokeServiceOperationCallAndReturnHeadAsBool(TypedValueExpression pipeline, TypedValueExpression message, TypedValueExpression clientDiagnostics, bool async)
{
var resultVar = new VariableReference(typeof(NullableResult<bool>), "result");
var result = new ResultExpression(resultVar);
return new MethodBodyStatement[]
{
Snippets.Var(resultVar, new MessagePipelineExpression(pipeline).ProcessHeadAsBoolMessage(message, new RequestOptionsExpression(KnownParameters.RequestContext), async)),
Snippets.Return(ResultExpression.FromValue(result.Value, result.GetRawResponse()))
};
}

public override TypedValueExpression InvokeServiceOperationCall(TypedValueExpression pipeline, TypedValueExpression message, bool async)
=> ResultExpression.FromResponse(new MessagePipelineExpression(pipeline).ProcessMessage(message, new RequestOptionsExpression(KnownParameters.RequestContext), null, async));

private static PipelineResponseExpression GetRawResponse(TypedValueExpression result)
=> result.Type.Equals(typeof(PipelineResponse))
? new PipelineResponseExpression(result)
: new ResultExpression(result).GetRawResponse();
: new ClientResultExpression(result).GetRawResponse();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.Generator.CSharp.Expressions;

namespace Microsoft.Generator.CSharp.ClientModel.Expressions
{
internal partial class SystemExtensibleSnippets : ExtensibleSnippets
{
public override JsonElementSnippets JsonElement { get; } = new SystemJsonElementSnippets();
public override RestOperationsSnippets RestOperations { get; } = new SystemRestOperationsSnippets();
public override ModelSnippets Model { get; } = new SystemModelSnippets();
}
Expand Down

This file was deleted.

Loading

0 comments on commit 3b4bd0b

Please sign in to comment.