mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-05-27 20:35:00 +00:00
Merge pull request #6 from koenbeuk/nullable-rewriting
support for rewriting null conditional access expressions
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
namespace EntityFrameworkCore.Projectables
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Configures how null-conditional operators are handeled
|
||||||
|
/// </summary>
|
||||||
|
public enum NullConditionalRewriteSupport
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Don't rewrite null conditional operators (Default behavior).
|
||||||
|
/// Usage of null conditional operators is thereby not allowed
|
||||||
|
/// </summary>
|
||||||
|
None,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ignore null-conditional operators in the generated expression tree
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <c>(A?.B)</c> is rewritten as expression: <c>(A.B)</c>
|
||||||
|
/// </remarks>
|
||||||
|
Ignore,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Translates null-conditional operators into explicit null checks
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <c>(A?.B)</c> is rewritten as expression: <c>(A != null ? A.B : null)</c>
|
||||||
|
/// </remarks>
|
||||||
|
Rewrite
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,5 +9,6 @@ namespace EntityFrameworkCore.Projectables
|
|||||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
|
||||||
public sealed class ProjectableAttribute : Attribute
|
public sealed class ProjectableAttribute : Attribute
|
||||||
{
|
{
|
||||||
|
public NullConditionalRewriteSupport NullConditionalRewriteSupport { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
@@ -7,6 +7,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="..\EntityFrameworkCore.Projectables.Abstractions\NullConditionalRewriteSupport.cs" Link="NullConditionalRewriteSupport.cs" />
|
||||||
<Compile Include="..\EntityFrameworkCore.Projectables\Services\ProjectionExpressionClassNameGenerator.cs" Link="ProjectionExpressionClassNameGenerator.cs" />
|
<Compile Include="..\EntityFrameworkCore.Projectables\Services\ProjectionExpressionClassNameGenerator.cs" Link="ProjectionExpressionClassNameGenerator.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -14,11 +14,76 @@ namespace EntityFrameworkCore.Projectables.Generator
|
|||||||
{
|
{
|
||||||
readonly INamedTypeSymbol _targetTypeSymbol;
|
readonly INamedTypeSymbol _targetTypeSymbol;
|
||||||
readonly SemanticModel _semanticModel;
|
readonly SemanticModel _semanticModel;
|
||||||
|
readonly NullConditionalRewriteSupport _nullConditionalRewriteSupport;
|
||||||
|
readonly Stack<ExpressionSyntax> _conditionalAccessExpressionsStack = new();
|
||||||
|
|
||||||
public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, SemanticModel semanticModel)
|
public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, SemanticModel semanticModel, NullConditionalRewriteSupport nullConditionalRewriteSupport)
|
||||||
{
|
{
|
||||||
_targetTypeSymbol = targetTypeSymbol;
|
_targetTypeSymbol = targetTypeSymbol;
|
||||||
_semanticModel = semanticModel;
|
_semanticModel = semanticModel;
|
||||||
|
_nullConditionalRewriteSupport = nullConditionalRewriteSupport;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override SyntaxNode? VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
|
||||||
|
{
|
||||||
|
var targetExpression = (ExpressionSyntax)Visit(node.Expression);
|
||||||
|
|
||||||
|
_conditionalAccessExpressionsStack.Push(targetExpression);
|
||||||
|
|
||||||
|
return _nullConditionalRewriteSupport switch {
|
||||||
|
NullConditionalRewriteSupport.Ignore => Visit(node.WhenNotNull),
|
||||||
|
NullConditionalRewriteSupport.Rewrite =>
|
||||||
|
SyntaxFactory.ConditionalExpression(
|
||||||
|
SyntaxFactory.BinaryExpression(
|
||||||
|
SyntaxKind.NotEqualsExpression,
|
||||||
|
targetExpression
|
||||||
|
.WithTrailingTrivia(SyntaxFactory.Whitespace(" ")),
|
||||||
|
SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)
|
||||||
|
.WithLeadingTrivia(SyntaxFactory.Whitespace(" "))
|
||||||
|
)
|
||||||
|
.WithTrailingTrivia(SyntaxFactory.Whitespace(" ")),
|
||||||
|
SyntaxFactory.ParenthesizedExpression(
|
||||||
|
(ExpressionSyntax)Visit(node.WhenNotNull)
|
||||||
|
)
|
||||||
|
.WithLeadingTrivia(SyntaxFactory.Whitespace(" "))
|
||||||
|
.WithTrailingTrivia(SyntaxFactory.Whitespace(" ")),
|
||||||
|
SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)
|
||||||
|
.WithLeadingTrivia(SyntaxFactory.Whitespace(" "))
|
||||||
|
),
|
||||||
|
_ => base.VisitConditionalAccessExpression(node)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override SyntaxNode? VisitMemberBindingExpression(MemberBindingExpressionSyntax node)
|
||||||
|
{
|
||||||
|
if (_conditionalAccessExpressionsStack.Count == 0)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Expected at least one conditional expression on the stack");
|
||||||
|
}
|
||||||
|
|
||||||
|
var targetExpression = _conditionalAccessExpressionsStack.Pop();
|
||||||
|
|
||||||
|
return _nullConditionalRewriteSupport switch {
|
||||||
|
NullConditionalRewriteSupport.Ignore => SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, targetExpression, node.Name),
|
||||||
|
NullConditionalRewriteSupport.Rewrite => SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, targetExpression, node.Name),
|
||||||
|
_ => node
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override SyntaxNode? VisitElementBindingExpression(ElementBindingExpressionSyntax node)
|
||||||
|
{
|
||||||
|
if (_conditionalAccessExpressionsStack.Count == 0)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Expected at least one conditional expression on the stack");
|
||||||
|
}
|
||||||
|
|
||||||
|
var targetExpression = _conditionalAccessExpressionsStack.Pop();
|
||||||
|
|
||||||
|
return _nullConditionalRewriteSupport switch {
|
||||||
|
NullConditionalRewriteSupport.Ignore => SyntaxFactory.ElementAccessExpression(targetExpression, node.ArgumentList),
|
||||||
|
NullConditionalRewriteSupport.Rewrite => SyntaxFactory.ElementAccessExpression(targetExpression, node.ArgumentList),
|
||||||
|
_ => Visit(node)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public override SyntaxNode? VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
|
public override SyntaxNode? VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
using Microsoft.CodeAnalysis.CSharp;
|
using Microsoft.CodeAnalysis.CSharp;
|
||||||
@@ -31,7 +32,6 @@ namespace EntityFrameworkCore.Projectables.Generator
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var projectableAttributeTypeSymbol = context.Compilation.GetTypeByMetadataName("EntityFrameworkCore.Projectables.ProjectableAttribute");
|
var projectableAttributeTypeSymbol = context.Compilation.GetTypeByMetadataName("EntityFrameworkCore.Projectables.ProjectableAttribute");
|
||||||
|
|
||||||
var projectableAttributeClass = memberSymbol.GetAttributes()
|
var projectableAttributeClass = memberSymbol.GetAttributes()
|
||||||
@@ -43,7 +43,15 @@ namespace EntityFrameworkCore.Projectables.Generator
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var expressionSyntaxRewriter = new ExpressionSyntaxRewriter(memberSymbol.ContainingType, semanticModel);
|
var nullConditionalRewriteSupport = projectableAttributeClass.NamedArguments
|
||||||
|
.Where(x => x.Key == "NullConditionalRewriteSupport")
|
||||||
|
.Where(x => x.Value.Kind == TypedConstantKind.Enum)
|
||||||
|
.Select(x => x.Value.Value)
|
||||||
|
.Where(x => Enum.IsDefined(typeof(NullConditionalRewriteSupport), x))
|
||||||
|
.Cast<NullConditionalRewriteSupport>()
|
||||||
|
.FirstOrDefault();
|
||||||
|
|
||||||
|
var expressionSyntaxRewriter = new ExpressionSyntaxRewriter(memberSymbol.ContainingType, semanticModel, nullConditionalRewriteSupport);
|
||||||
var parameterSyntaxRewriter = new ParameterSyntaxRewriter(semanticModel);
|
var parameterSyntaxRewriter = new ParameterSyntaxRewriter(semanticModel);
|
||||||
var returnTypeSyntaxRewriter = new ReturnTypeSyntaxRewriter(semanticModel);
|
var returnTypeSyntaxRewriter = new ReturnTypeSyntaxRewriter(semanticModel);
|
||||||
|
|
||||||
@@ -128,8 +136,6 @@ namespace EntityFrameworkCore.Projectables.Generator
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
descriptor.UsingDirectives =
|
descriptor.UsingDirectives =
|
||||||
memberDeclarationSyntax.SyntaxTree
|
memberDeclarationSyntax.SyntaxTree
|
||||||
.GetRoot()
|
.GetRoot()
|
||||||
@@ -137,7 +143,6 @@ namespace EntityFrameworkCore.Projectables.Generator
|
|||||||
.OfType<UsingDirectiveSyntax>()
|
.OfType<UsingDirectiveSyntax>()
|
||||||
.Select(x => x.ToString());
|
.Select(x => x.ToString());
|
||||||
|
|
||||||
|
|
||||||
return descriptor;
|
return descriptor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -11,8 +11,8 @@ namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMethods
|
|||||||
[Projectable]
|
[Projectable]
|
||||||
public static int Foo(this Entity entity) => entity.Id + 1;
|
public static int Foo(this Entity entity) => entity.Id + 1;
|
||||||
|
|
||||||
[Projectable]
|
[Projectable]
|
||||||
public static int Foo2(this Entity entity) => entity.Foo() + 1;
|
public static int Foo2(this Entity entity) => entity.Foo() + 1;
|
||||||
|
|
||||||
[Projectable]
|
[Projectable]
|
||||||
public static Entity? LeadingEntity(this Entity entity, DbContext dbContext)
|
public static Entity? LeadingEntity(this Entity entity, DbContext dbContext)
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.FunctionalTests.NullConditionals
|
||||||
|
{
|
||||||
|
public record Entity
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string? Name { get; set; }
|
||||||
|
public List<Entity>? RelatedEntities { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
+31
@@ -0,0 +1,31 @@
|
|||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.FunctionalTests.NullConditionals
|
||||||
|
{
|
||||||
|
public static class EntityExtensions
|
||||||
|
{
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
|
||||||
|
public static string GetNameIgnoreNulls(this Entity entity)
|
||||||
|
=> entity?.Name;
|
||||||
|
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
|
||||||
|
public static int? GetNameLengthIgnoreNulls(this Entity entity)
|
||||||
|
=> entity.GetNameIgnoreNulls()?.Length;
|
||||||
|
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
|
||||||
|
public static Entity GetFirstRelatedIgnoreNulls(this Entity entity)
|
||||||
|
=> entity?.RelatedEntities?[0];
|
||||||
|
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
|
||||||
|
public static string GetNameRewriteNulls(this Entity entity)
|
||||||
|
=> entity?.Name;
|
||||||
|
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
|
||||||
|
public static int? GetNameLengthRewriteNulls(this Entity entity)
|
||||||
|
=> entity.GetNameIgnoreNulls()?.Length;
|
||||||
|
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
|
||||||
|
public static Entity GetFirstRelatedRewriteNulls(this Entity entity)
|
||||||
|
=> entity?.RelatedEntities?[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
SELECT CAST(LEN([e].[Name]) AS int)
|
||||||
|
FROM [Entity] AS [e]
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
SELECT [e].[Id], [e0].[Id], [e0].[EntityId], [e0].[Name]
|
||||||
|
FROM [Entity] AS [e]
|
||||||
|
LEFT JOIN [Entity] AS [e0] ON [e].[Id] = [e0].[EntityId]
|
||||||
|
ORDER BY [e].[Id]
|
||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
SELECT [e].[Name]
|
||||||
|
FROM [Entity] AS [e]
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using VerifyXunit;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.FunctionalTests.NullConditionals
|
||||||
|
{
|
||||||
|
[UsesVerify]
|
||||||
|
public class IngoreNullConditionalRewriteTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public Task SimpleMemberExpression()
|
||||||
|
{
|
||||||
|
using var dbContext = new SampleDbContext<Entity>();
|
||||||
|
|
||||||
|
var query = dbContext.Set<Entity>()
|
||||||
|
.Select(x => x.GetNameIgnoreNulls());
|
||||||
|
|
||||||
|
return Verifier.Verify(query.ToQueryString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task ComplexMemberExpression()
|
||||||
|
{
|
||||||
|
using var dbContext = new SampleDbContext<Entity>();
|
||||||
|
|
||||||
|
var query = dbContext.Set<Entity>()
|
||||||
|
.Select(x => x.GetNameLengthIgnoreNulls());
|
||||||
|
|
||||||
|
return Verifier.Verify(query.ToQueryString());
|
||||||
|
}
|
||||||
|
|
||||||
|
#if EFPROJECTABLES2
|
||||||
|
[Fact]
|
||||||
|
public Task RelationalExpression()
|
||||||
|
{
|
||||||
|
using var dbContext = new SampleDbContext<Entity>();
|
||||||
|
|
||||||
|
var query = dbContext.Set<Entity>()
|
||||||
|
.Select(x => x.GetFirstRelatedIgnoreNulls());
|
||||||
|
|
||||||
|
return Verifier.Verify(query.ToQueryString());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
SELECT CAST(LEN([e].[Name]) AS int)
|
||||||
|
FROM [Entity] AS [e]
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
SELECT CAST(1 AS bit), [e].[Id], [e0].[Id], [e0].[EntityId], [e0].[Name], [e1].[Id], [e1].[EntityId], [e1].[Name]
|
||||||
|
FROM [Entity] AS [e]
|
||||||
|
LEFT JOIN [Entity] AS [e0] ON [e].[Id] = [e0].[EntityId]
|
||||||
|
LEFT JOIN [Entity] AS [e1] ON [e].[Id] = [e1].[EntityId]
|
||||||
|
ORDER BY [e].[Id], [e0].[Id]
|
||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
SELECT CAST(LEN([e].[Name]) AS int)
|
||||||
|
FROM [Entity] AS [e]
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using VerifyXunit;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.FunctionalTests.NullConditionals
|
||||||
|
{
|
||||||
|
[UsesVerify]
|
||||||
|
public class RewriteNullConditionalRewriteTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public Task SimpleMemberExpression()
|
||||||
|
{
|
||||||
|
using var dbContext = new SampleDbContext<Entity>();
|
||||||
|
|
||||||
|
var query = dbContext.Set<Entity>()
|
||||||
|
.Select(x => x.GetNameLengthRewriteNulls());
|
||||||
|
|
||||||
|
return Verifier.Verify(query.ToQueryString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task ComplexMemberExpression()
|
||||||
|
{
|
||||||
|
using var dbContext = new SampleDbContext<Entity>();
|
||||||
|
|
||||||
|
var query = dbContext.Set<Entity>()
|
||||||
|
.Select(x => x.GetNameLengthRewriteNulls());
|
||||||
|
|
||||||
|
return Verifier.Verify(query.ToQueryString());
|
||||||
|
}
|
||||||
|
|
||||||
|
#if EFPROJECTABLES2
|
||||||
|
[Fact]
|
||||||
|
public Task RelationalExpression()
|
||||||
|
{
|
||||||
|
using var dbContext = new SampleDbContext<Entity>();
|
||||||
|
|
||||||
|
var query = dbContext.Set<Entity>()
|
||||||
|
.Select(x => x.GetFirstRelatedRewriteNulls());
|
||||||
|
|
||||||
|
return Verifier.Verify(query.ToQueryString());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
using Foo;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.Generated
|
||||||
|
#nullable disable
|
||||||
|
{
|
||||||
|
public static class Foo_EntityExtensions_GetFirstRelatedIgnoreNulls
|
||||||
|
{
|
||||||
|
public static System.Linq.Expressions.Expression<System.Func<Entity, global::Foo.EntityExtensions.Entity>> Expression =>
|
||||||
|
(Entity entity) => entity != null ? (entity.RelatedEntities != null ? (entity.RelatedEntities[0]) : null) : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
using Foo;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.Generated
|
||||||
|
#nullable disable
|
||||||
|
{
|
||||||
|
public static class Foo_EntityExtensions_GetFirstRelatedIgnoreNulls
|
||||||
|
{
|
||||||
|
public static System.Linq.Expressions.Expression<System.Func<Entity, global::Foo.EntityExtensions.Entity>> Expression =>
|
||||||
|
(Entity entity) => entity.RelatedEntities[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
using Foo;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.Generated
|
||||||
|
#nullable disable
|
||||||
|
{
|
||||||
|
public static class Foo_EntityExtensions_GetFirstRelatedIgnoreNulls
|
||||||
|
{
|
||||||
|
public static System.Linq.Expressions.Expression<System.Func<Entity, global::Foo.EntityExtensions.Entity>> Expression =>
|
||||||
|
(Entity entity) => entity != null ? (entity.RelatedEntities != null ? (entity.RelatedEntities[0]) : null) : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
using Foo;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.Generated
|
||||||
|
#nullable disable
|
||||||
|
{
|
||||||
|
public static class Foo_C_GetFirst
|
||||||
|
{
|
||||||
|
public static System.Linq.Expressions.Expression<System.Func<string, string>> Expression =>
|
||||||
|
(string input) => input[0].ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
using Foo;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.Generated
|
||||||
|
#nullable disable
|
||||||
|
{
|
||||||
|
public static class Foo_C_GetFirst
|
||||||
|
{
|
||||||
|
public static System.Linq.Expressions.Expression<System.Func<string, string>> Expression =>
|
||||||
|
(string input) => input != null ? (input[0].ToString()) : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
using Foo;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.Generated
|
||||||
|
#nullable disable
|
||||||
|
{
|
||||||
|
public static class Foo_C_GetLength
|
||||||
|
{
|
||||||
|
public static System.Linq.Expressions.Expression<System.Func<string, int?>> Expression =>
|
||||||
|
(string input) => input.Length;
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
using Foo;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.Generated
|
||||||
|
#nullable disable
|
||||||
|
{
|
||||||
|
public static class Foo_C_GetLength
|
||||||
|
{
|
||||||
|
public static System.Linq.Expressions.Expression<System.Func<string, int?>> Expression =>
|
||||||
|
(string input) => input != null ? (input.Length) : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
using Foo;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.Generated
|
||||||
|
#nullable disable
|
||||||
|
{
|
||||||
|
public static class Foo_EntityExtensions_GetFirstName
|
||||||
|
{
|
||||||
|
public static System.Linq.Expressions.Expression<System.Func<Entity, string>> Expression =>
|
||||||
|
(Entity entity) => entity.FullName != null ? (entity.FullName.Substring(entity.FullName != null ? (entity.FullName.IndexOf(' ') ) : null?? 0)) : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
using Foo;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.Generated
|
||||||
|
#nullable disable
|
||||||
|
{
|
||||||
|
public static class Foo_C_GetFirst
|
||||||
|
{
|
||||||
|
public static System.Linq.Expressions.Expression<System.Func<string, char?>> Expression =>
|
||||||
|
(string input) => input != null ? (input[0]) : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
using Foo;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.Generated
|
||||||
|
#nullable disable
|
||||||
|
{
|
||||||
|
public static class Foo_C_GetFirst
|
||||||
|
{
|
||||||
|
public static System.Linq.Expressions.Expression<System.Func<string, char?>> Expression =>
|
||||||
|
(string input) => input[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
using Foo;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.Generated
|
||||||
|
#nullable disable
|
||||||
|
{
|
||||||
|
public static class Foo_C_GetFirst
|
||||||
|
{
|
||||||
|
public static System.Linq.Expressions.Expression<System.Func<string, char?>> Expression =>
|
||||||
|
(string input) => input[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
using Foo;
|
||||||
|
|
||||||
|
namespace EntityFrameworkCore.Projectables.Generated
|
||||||
|
#nullable disable
|
||||||
|
{
|
||||||
|
public static class Foo_C_GetFirst
|
||||||
|
{
|
||||||
|
public static System.Linq.Expressions.Expression<System.Func<string, char?>> Expression =>
|
||||||
|
(string input) => input != null ? (input[0]) : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
+244
-2
@@ -487,6 +487,250 @@ namespace Foo {
|
|||||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task NullableMemberBinding_WithIgnoreSupport_IsBeingRewritten()
|
||||||
|
{
|
||||||
|
var compilation = CreateCompilation(@"
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
|
||||||
|
namespace Foo {
|
||||||
|
static class C {
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
|
||||||
|
public static int? GetLength(this string input) => input?.Length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
|
||||||
|
var result = RunGenerator(compilation);
|
||||||
|
|
||||||
|
Assert.Empty(result.Diagnostics);
|
||||||
|
Assert.Single(result.GeneratedTrees);
|
||||||
|
|
||||||
|
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task NullableMemberBinding_WithRewriteSupport_IsBeingRewritten()
|
||||||
|
{
|
||||||
|
var compilation = CreateCompilation(@"
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
|
||||||
|
namespace Foo {
|
||||||
|
static class C {
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
|
||||||
|
public static int? GetLength(this string input) => input?.Length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
|
||||||
|
var result = RunGenerator(compilation);
|
||||||
|
|
||||||
|
Assert.Empty(result.Diagnostics);
|
||||||
|
Assert.Single(result.GeneratedTrees);
|
||||||
|
|
||||||
|
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task NullableSimpleElementBinding_WithIgnoreSupport_IsBeingRewritten()
|
||||||
|
{
|
||||||
|
var compilation = CreateCompilation(@"
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
|
||||||
|
namespace Foo {
|
||||||
|
static class C {
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
|
||||||
|
public static char? GetFirst(this string input) => input?[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
|
||||||
|
var result = RunGenerator(compilation);
|
||||||
|
|
||||||
|
Assert.Empty(result.Diagnostics);
|
||||||
|
Assert.Single(result.GeneratedTrees);
|
||||||
|
|
||||||
|
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task NullableSimpleElementBinding_WithRewriteSupport_IsBeingRewritten()
|
||||||
|
{
|
||||||
|
var compilation = CreateCompilation(@"
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
|
||||||
|
namespace Foo {
|
||||||
|
static class C {
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
|
||||||
|
public static char? GetFirst(this string input) => input?[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
|
||||||
|
var result = RunGenerator(compilation);
|
||||||
|
|
||||||
|
Assert.Empty(result.Diagnostics);
|
||||||
|
Assert.Single(result.GeneratedTrees);
|
||||||
|
|
||||||
|
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task NullableElementBinding_WithIgnoreSupport_IsBeingRewritten()
|
||||||
|
{
|
||||||
|
var compilation = CreateCompilation(@"
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
|
||||||
|
namespace Foo {
|
||||||
|
static class C {
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
|
||||||
|
public static string? GetFirst(this string input) => input?[0].ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
|
||||||
|
var result = RunGenerator(compilation);
|
||||||
|
|
||||||
|
Assert.Empty(result.Diagnostics);
|
||||||
|
Assert.Single(result.GeneratedTrees);
|
||||||
|
|
||||||
|
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task NullableElementBinding_WithRewriteSupport_IsBeingRewritten()
|
||||||
|
{
|
||||||
|
var compilation = CreateCompilation(@"
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
|
||||||
|
namespace Foo {
|
||||||
|
static class C {
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
|
||||||
|
public static string? GetFirst(this string input) => input?[0].ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
|
||||||
|
var result = RunGenerator(compilation);
|
||||||
|
|
||||||
|
Assert.Empty(result.Diagnostics);
|
||||||
|
Assert.Single(result.GeneratedTrees);
|
||||||
|
|
||||||
|
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task NullableElementAndMemberBinding_WithIgnoreSupport_IsBeingRewritten()
|
||||||
|
{
|
||||||
|
var compilation = CreateCompilation(@"
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
|
||||||
|
namespace Foo {
|
||||||
|
public static class EntityExtensions
|
||||||
|
{
|
||||||
|
public record Entity
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public List<Entity>? RelatedEntities { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
|
||||||
|
public static Entity GetFirstRelatedIgnoreNulls(this Entity entity)
|
||||||
|
=> entity?.RelatedEntities?[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
|
||||||
|
var result = RunGenerator(compilation);
|
||||||
|
|
||||||
|
Assert.Empty(result.Diagnostics);
|
||||||
|
Assert.Single(result.GeneratedTrees);
|
||||||
|
|
||||||
|
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task NullableElementAndMemberBinding_WithRewriteSupport_IsBeingRewritten()
|
||||||
|
{
|
||||||
|
var compilation = CreateCompilation(@"
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
|
||||||
|
namespace Foo {
|
||||||
|
public static class EntityExtensions
|
||||||
|
{
|
||||||
|
public record Entity
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public List<Entity>? RelatedEntities { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
|
||||||
|
public static Entity GetFirstRelatedIgnoreNulls(this Entity entity)
|
||||||
|
=> entity?.RelatedEntities?[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
|
||||||
|
var result = RunGenerator(compilation);
|
||||||
|
|
||||||
|
Assert.Empty(result.Diagnostics);
|
||||||
|
Assert.Single(result.GeneratedTrees);
|
||||||
|
|
||||||
|
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task NullableParameters_WithRewriteSupport_IsBeingRewritten()
|
||||||
|
{
|
||||||
|
var compilation = CreateCompilation(@"
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
|
||||||
|
namespace Foo {
|
||||||
|
public static class EntityExtensions
|
||||||
|
{
|
||||||
|
public record Entity
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string? FullName { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
|
||||||
|
public static string GetFirstName(this Entity entity)
|
||||||
|
=> entity.FullName?.Substring(entity.FullName?.IndexOf(' ') ?? 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
|
||||||
|
var result = RunGenerator(compilation);
|
||||||
|
|
||||||
|
Assert.Empty(result.Diagnostics);
|
||||||
|
Assert.Single(result.GeneratedTrees);
|
||||||
|
|
||||||
|
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
#region Helpers
|
#region Helpers
|
||||||
|
|
||||||
Compilation CreateCompilation(string source, bool expectedToCompile = true)
|
Compilation CreateCompilation(string source, bool expectedToCompile = true)
|
||||||
@@ -494,8 +738,6 @@ namespace Foo {
|
|||||||
var references = Basic.Reference.Assemblies.NetStandard20.All.ToList();
|
var references = Basic.Reference.Assemblies.NetStandard20.All.ToList();
|
||||||
references.Add(MetadataReference.CreateFromFile(typeof(ProjectableAttribute).Assembly.Location));
|
references.Add(MetadataReference.CreateFromFile(typeof(ProjectableAttribute).Assembly.Location));
|
||||||
|
|
||||||
var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
|
|
||||||
|
|
||||||
var compilation = CSharpCompilation.Create("compilation",
|
var compilation = CSharpCompilation.Create("compilation",
|
||||||
new[] { CSharpSyntaxTree.ParseText(source) },
|
new[] { CSharpSyntaxTree.ParseText(source) },
|
||||||
references,
|
references,
|
||||||
|
|||||||
Reference in New Issue
Block a user