support for rewriting null conditional access expressions

This commit is contained in:
Koen Bekkenutte
2021-10-18 22:36:06 +08:00
parent 654dcba6ff
commit 86223a11c9
29 changed files with 682 additions and 11 deletions
@@ -11,8 +11,8 @@ namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMethods
[Projectable]
public static int Foo(this Entity entity) => entity.Id + 1;
[Projectable]
public static int Foo2(this Entity entity) => entity.Foo() + 1;
[Projectable]
public static int Foo2(this Entity entity) => entity.Foo() + 1;
[Projectable]
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; }
}
}
@@ -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];
}
}
@@ -0,0 +1,2 @@
SELECT CAST(LEN([e].[Name]) AS int)
FROM [Entity] AS [e]
@@ -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]
@@ -0,0 +1,48 @@
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());
}
[Fact]
public Task RelationalExpression()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.GetFirstRelatedIgnoreNulls());
return Verifier.Verify(query.ToQueryString());
}
}
}
@@ -0,0 +1,2 @@
SELECT CAST(LEN([e].[Name]) AS int)
FROM [Entity] AS [e]
@@ -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]
@@ -0,0 +1,2 @@
SELECT CAST(LEN([e].[Name]) AS int)
FROM [Entity] AS [e]
@@ -0,0 +1,48 @@
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());
}
[Fact]
public Task RelationalExpression()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.GetFirstRelatedRewriteNulls());
return Verifier.Verify(query.ToQueryString());
}
}
}
@@ -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;
}
}
@@ -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];
}
}
@@ -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;
}
}
@@ -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();
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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];
}
}
@@ -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];
}
}
@@ -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;
}
}
@@ -487,6 +487,250 @@ namespace Foo {
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
Compilation CreateCompilation(string source, bool expectedToCompile = true)
@@ -494,8 +738,6 @@ namespace Foo {
var references = Basic.Reference.Assemblies.NetStandard20.All.ToList();
references.Add(MetadataReference.CreateFromFile(typeof(ProjectableAttribute).Assembly.Location));
var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
var compilation = CSharpCompilation.Create("compilation",
new[] { CSharpSyntaxTree.ParseText(source) },
references,