diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ModelSerializationExtensionsDefinition.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ModelSerializationExtensionsDefinition.cs index bd588effd2..2e070745d3 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ModelSerializationExtensionsDefinition.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ModelSerializationExtensionsDefinition.cs @@ -98,7 +98,7 @@ protected override MethodProvider[] BuildMethods() this); var charValueParameter = new ParameterProvider("value", FormattableStringHelpers.Empty, typeof(char)); - var value = charValueParameter.AsExpression.As(); + var value = charValueParameter.As(); var writeStringChar = new MethodProvider( new MethodSignature( Name: WriteStringValueMethodName, diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/MrwSerializationTypeDefinition.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/MrwSerializationTypeDefinition.cs index c4f19fa92d..e40247089d 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/MrwSerializationTypeDefinition.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/MrwSerializationTypeDefinition.cs @@ -176,7 +176,7 @@ private MethodProvider BuildExplicitFromClientResult() var result = new ParameterProvider("result", $"The {ClientModelPlugin.Instance.TypeFactory.ClientResponseApi.ClientResponseType:C} to deserialize the {Type:C} from.", ClientModelPlugin.Instance.TypeFactory.ClientResponseApi.ClientResponseType); var modifiers = MethodSignatureModifiers.Public | MethodSignatureModifiers.Static | MethodSignatureModifiers.Explicit | MethodSignatureModifiers.Operator; // using PipelineResponse response = result.GetRawResponse(); - var responseDeclaration = UsingDeclare("response", ClientModelPlugin.Instance.TypeFactory.HttpResponseApi.HttpResponseType, result.AsExpression.ToApi().GetRawResponse(), out var response); + var responseDeclaration = UsingDeclare("response", ClientModelPlugin.Instance.TypeFactory.HttpResponseApi.HttpResponseType, result.ToApi().GetRawResponse(), out var response); // using JsonDocument document = JsonDocument.Parse(response.Content); var document = UsingDeclare( "document", @@ -206,7 +206,7 @@ private MethodProvider BuildImplicitToBinaryContent() new MethodSignature(ClientModelPlugin.Instance.TypeFactory.RequestContentApi.RequestContentType.FrameworkType.Name, null, modifiers, null, null, [model]), new MethodBodyStatement[] { - !_isStruct ? new IfStatement(model.AsExpression.Equal(Null)) { Return(Null) } : MethodBodyStatement.Empty, + !_isStruct ? new IfStatement(model.Equal(Null)) { Return(Null) } : MethodBodyStatement.Empty, ClientModelPlugin.Instance.TypeFactory.RequestContentApi.ToExpression().Create(model) }, this); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Primitives/ConstructorInitializer.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Primitives/ConstructorInitializer.cs index a20857aac6..35c3f77b3b 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Primitives/ConstructorInitializer.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Primitives/ConstructorInitializer.cs @@ -5,11 +5,12 @@ using System.Linq; using Microsoft.Generator.CSharp.Expressions; using Microsoft.Generator.CSharp.Providers; +using Microsoft.Generator.CSharp.Snippets; namespace Microsoft.Generator.CSharp.Primitives { public sealed record ConstructorInitializer(bool IsBase, IReadOnlyList Arguments) { - public ConstructorInitializer(bool isBase, IEnumerable arguments) : this(isBase, [.. arguments.Select(p => p.AsExpression)]) { } + public ConstructorInitializer(bool isBase, IEnumerable arguments) : this(isBase, [.. arguments.Select(p => p.AsExpression())]) { } } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ArgumentDefinition.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ArgumentDefinition.cs index 781663cf25..e272acd9d0 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ArgumentDefinition.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ArgumentDefinition.cs @@ -229,7 +229,7 @@ private MethodProvider BuildAssertNotNullOrEmptyCollection() private static ScopedApi IsCollectionEmpty(ParameterProvider valueParam, VariableExpression collection) { - return valueParam.AsExpression.Is(new DeclarationExpression(collection)).And(new MemberExpression(collection, "Count").Equal(Literal(0))); + return valueParam.Is(new DeclarationExpression(collection)).And(new MemberExpression(collection, "Count").Equal(Literal(0))); } private MethodBodyStatement ThrowArgumentException(ValueExpression expression) @@ -266,7 +266,7 @@ private MethodProvider BuildAssertNotNull() private IfStatement AssertNotNullSnippet(ParameterProvider value) { - return new IfStatement(value.AsExpression.Is(Null)) + return new IfStatement(value.Is(Null)) { Throw(New.ArgumentNullException(_nameParam, false)) }; diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ChangeTrackingDictionaryDefinition.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ChangeTrackingDictionaryDefinition.cs index 1a36f3b2ac..08c953907a 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ChangeTrackingDictionaryDefinition.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ChangeTrackingDictionaryDefinition.cs @@ -107,7 +107,7 @@ private ConstructorProvider ConstructorWithDictionary() var signature = new ConstructorSignature(Type, null, MethodSignatureModifiers.Public, [dictionary]); return new ConstructorProvider(signature, new MethodBodyStatement[] { - new IfStatement(dictionary.AsExpression.Equal(Null)) + new IfStatement(dictionary.Equal(Null)) { Return() }, diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ExtensibleEnumProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ExtensibleEnumProvider.cs index 28d6386a1c..1781a7c57f 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ExtensibleEnumProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ExtensibleEnumProvider.cs @@ -170,7 +170,7 @@ protected override MethodProvider[] BuildMethods() // public override bool Equals(object obj) => obj is EnumType other && Equals(other); methods.Add(new( equalsSignature, - objParameter.AsExpression + objParameter .Is(new DeclarationExpression(Type, "other", out var other)) .And(This.Invoke(nameof(Equals), [other])), this)); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ModelProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ModelProvider.cs index 5de95ac291..b1664b4e55 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ModelProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ModelProvider.cs @@ -594,7 +594,7 @@ private ValueExpression GetExpressionForCtor(ParameterProvider parameter, HashSe return null; } - var discriminatorExpression = property.AsParameter.AsExpression; + var discriminatorExpression = property.AsParameter; var type = property.Type; if (!type.IsFrameworkType && type.IsEnum) diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/OptionalDefinition.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/OptionalDefinition.cs index a860ac01eb..e47f8380c2 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/OptionalDefinition.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/OptionalDefinition.cs @@ -112,7 +112,7 @@ private MethodProvider BuildIsReadOnlyDictionaryDefined() return new MethodProvider(signature, new MethodBodyStatement[] { - Return(Not(collectionParam.AsExpression.Is(changeTrackingDeclarationExpression) + Return(Not(collectionParam.Is(changeTrackingDeclarationExpression) .And(new MemberExpression(changeTrackingReference, "IsUndefined")))) }, this); @@ -127,7 +127,7 @@ private MethodProvider BuildIsDictionaryDefined() return new MethodProvider(signature, new MethodBodyStatement[] { - Return(Not(collectionParam.AsExpression.Is(changeTrackingDeclarationExpression) + Return(Not(collectionParam.Is(changeTrackingDeclarationExpression) .And(new MemberExpression(changeTrackingReference, "IsUndefined")))) }, this); @@ -142,7 +142,7 @@ private MethodProvider BuildIsListDefined() return new MethodProvider(signature, new MethodBodyStatement[] { - Return(Not(collectionParam.AsExpression.Is(changeTrackingDeclarationExpression) + Return(Not(collectionParam.Is(changeTrackingDeclarationExpression) .And(new MemberExpression(changeTrackingReference, "IsUndefined")))) }, this); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ParameterProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ParameterProvider.cs index 78b4048818..cfda9d2d5b 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ParameterProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ParameterProvider.cs @@ -175,7 +175,7 @@ private static VariableExpression GetVariableExpression(ParameterProvider parame } private VariableExpression? _asVariable; - public VariableExpression AsExpression => _asVariable ??= this; + private VariableExpression AsExpression => _asVariable ??= this; public TypeProvider? SpreadSource { get; set; } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Snippets/Snippet.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Snippets/Snippet.cs index ed1280b2ae..d1a5355c4a 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Snippets/Snippet.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Snippets/Snippet.cs @@ -13,14 +13,17 @@ namespace Microsoft.Generator.CSharp.Snippets { public static partial class Snippet { - public static ScopedApi As(this ParameterProvider parameter, CSharpType type) => parameter.AsExpression.As(type); - public static ScopedApi As(this ParameterProvider parameter) => parameter.AsExpression.As(); - public static ScopedApi As(this PropertyProvider property) => ((MemberExpression)property).As(); - public static ScopedApi As(this FieldProvider field) => ((MemberExpression)field).As(); + public static ScopedApi Equal(this ParameterProvider parameter, ValueExpression other) => new BinaryOperatorExpression("==", parameter, other).As(); + public static ScopedApi Is(this ParameterProvider parameter, ValueExpression other) => new BinaryOperatorExpression("is", parameter, other).As(); + + public static ScopedApi As(this ParameterProvider parameter, CSharpType type) => ((ValueExpression)parameter).As(type); + public static ScopedApi As(this ParameterProvider parameter) => ((ValueExpression)parameter).As(); + public static ScopedApi As(this PropertyProvider property) => ((ValueExpression)property).As(); + public static ScopedApi As(this FieldProvider field) => ((ValueExpression)field).As(); public static ValueExpression NullConditional(this ParameterProvider parameter) => new NullConditionalExpression(parameter); - public static ValueExpression NullCoalesce(this ParameterProvider parameter, ValueExpression value) => parameter.AsExpression.NullCoalesce(value); + public static ValueExpression NullCoalesce(this ParameterProvider parameter, ValueExpression value) => new BinaryOperatorExpression("??", parameter, value); public static ValueExpression PositionalReference(this ParameterProvider parameter, ValueExpression value) => new PositionalParameterReferenceExpression(parameter.Name, value); @@ -143,5 +146,7 @@ public static ValueExpression Invoke(this PropertyProvider property, public static ScopedApi NotEqual(this ParameterProvider parameter, ValueExpression other) => new BinaryOperatorExpression("!=", parameter, other).As(); + + public static VariableExpression AsExpression(this ParameterProvider variableExpression) => (VariableExpression)variableExpression; } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Writers/CodeWriter.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Writers/CodeWriter.cs index ee51ef2f93..d1281aab16 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Writers/CodeWriter.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Writers/CodeWriter.cs @@ -10,6 +10,7 @@ using Microsoft.Generator.CSharp.Expressions; using Microsoft.Generator.CSharp.Primitives; using Microsoft.Generator.CSharp.Providers; +using Microsoft.Generator.CSharp.Snippets; using Microsoft.Generator.CSharp.Statements; using static Microsoft.Generator.CSharp.Snippets.Snippet; @@ -281,7 +282,7 @@ public void WriteProperty(PropertyProvider property, bool isPublicContext = fals if (property is IndexPropertyProvider indexer) { indexerScope = AmbientScope(); - Append($"{indexer.Name}[{indexer.IndexerParameter.Type} {indexer.IndexerParameter.AsExpression.Declaration}]"); + Append($"{indexer.Name}[{indexer.IndexerParameter.Type} {indexer.IndexerParameter.AsExpression().Declaration}]"); } else { @@ -406,7 +407,7 @@ public void WriteParameter(ParameterProvider parameter) AppendRawIf("out ", parameter.IsOut); AppendRawIf("ref ", parameter.IsRef); - Append($"{parameter.Type} {parameter.AsExpression.Declaration}"); + Append($"{parameter.Type} {parameter.AsExpression().Declaration}"); if (parameter.DefaultValue != null) { AppendRaw(" = "); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ParameterProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ParameterProviderTests.cs index c7f57479a4..dab2cf9a9f 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ParameterProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ParameterProviderTests.cs @@ -102,7 +102,7 @@ public void ToPublicInputParameterCopiesProperties() var parameter = CodeModelPlugin.Instance.TypeFactory.CreateParameter(inputType); var publicParameter = parameter.ToPublicInputParameter(); - Assert.AreEqual(parameter.AsExpression, publicParameter.AsExpression); + Assert.AreEqual(parameter, publicParameter); Assert.AreEqual(parameter.Attributes, publicParameter.Attributes); Assert.AreEqual(parameter.DefaultValue, publicParameter.DefaultValue); Assert.AreEqual(parameter.Description, publicParameter.Description); @@ -127,7 +127,7 @@ public void WithRefCopiesProperties() var parameter = CodeModelPlugin.Instance.TypeFactory.CreateParameter(inputType); var refParemeter = parameter.WithRef(); - Assert.AreEqual(parameter.AsExpression, refParemeter.AsExpression); + Assert.AreEqual(parameter, refParemeter); Assert.AreEqual(parameter.Attributes, refParemeter.Attributes); Assert.AreEqual(parameter.DefaultValue, refParemeter.DefaultValue); Assert.AreEqual(parameter.Description, refParemeter.Description); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/CodeWriterDeclarationTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/CodeWriterDeclarationTests.cs index 511dd2035d..cbe2191a2c 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/CodeWriterDeclarationTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/CodeWriterDeclarationTests.cs @@ -163,7 +163,7 @@ public void ScopeDeclaredTwiceForMethodSignatureParam() using var codeWriter = new CodeWriter(); codeWriter.WriteMethod(method); - var declScopes = GetDeclarationScopes(param.AsExpression.Declaration); + var declScopes = GetDeclarationScopes(param.AsExpression().Declaration); Assert.AreEqual(2, declScopes.Count); Assert.AreEqual(Helpers.GetExpectedFromFile(), codeWriter.ToString(false)); }