mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-06-05 07:59:09 +00:00
Fix for projectable instance members returning complex types
This commit is contained in:
@@ -126,23 +126,35 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
{
|
||||
if (symbolInfo.Symbol is IMethodSymbol methodSymbol && methodSymbol.IsExtensionMethod)
|
||||
{
|
||||
|
||||
}
|
||||
else if (symbolInfo.Symbol.Kind is SymbolKind.Property or SymbolKind.Method or SymbolKind.Field && SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol))
|
||||
{
|
||||
bool rewrite = true;
|
||||
|
||||
if (node.Parent is MemberAccessExpressionSyntax parentMemberAccessNode)
|
||||
{
|
||||
var targetSymbolInfo = _semanticModel.GetSymbolInfo(parentMemberAccessNode.Expression);
|
||||
if (targetSymbolInfo.Symbol is { Kind: SymbolKind.Parameter })
|
||||
{
|
||||
return base.VisitIdentifierName(node);
|
||||
rewrite = false;
|
||||
}
|
||||
}
|
||||
else if (node.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
|
||||
{
|
||||
rewrite = false;
|
||||
}
|
||||
else if (node.Parent.IsKind(SyntaxKind.InvocationExpression))
|
||||
{
|
||||
rewrite = true;
|
||||
}
|
||||
|
||||
return SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
|
||||
SyntaxFactory.IdentifierName("@this"),
|
||||
node
|
||||
);
|
||||
if (rewrite)
|
||||
{
|
||||
return SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
|
||||
SyntaxFactory.IdentifierName("@this"),
|
||||
node
|
||||
);
|
||||
}
|
||||
}
|
||||
else if (symbolInfo.Symbol.Kind is SymbolKind.NamedType && node.Parent?.Kind() is not SyntaxKind.QualifiedName)
|
||||
{
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using EntityFrameworkCore.Projectables;
|
||||
using Foo;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.Generated
|
||||
#nullable disable
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C, int>> Expression()
|
||||
{
|
||||
return (global::Foo.C @this) =>
|
||||
@this.Bar;
|
||||
}
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
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_Entity_Something
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.EntityExtensions.Entity, global::Foo.EntityExtensions.Entity>> Expression()
|
||||
{
|
||||
return (global::Foo.EntityExtensions.Entity entity) =>
|
||||
new Entity(entity.Id) {
|
||||
FullName = entity.FullName
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
+59
@@ -76,6 +76,30 @@ namespace Foo {
|
||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task MinimalProjectableComputedProperty()
|
||||
{
|
||||
var compilation = CreateCompilation(@"
|
||||
using System;
|
||||
using EntityFrameworkCore.Projectables;
|
||||
namespace Foo {
|
||||
class C {
|
||||
public int Bar { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public int Foo => Bar;
|
||||
}
|
||||
}
|
||||
");
|
||||
|
||||
var result = RunGenerator(compilation);
|
||||
|
||||
Assert.Empty(result.Diagnostics);
|
||||
Assert.Single(result.GeneratedTrees);
|
||||
|
||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task SimpleProjectableComputedProperty()
|
||||
{
|
||||
@@ -900,6 +924,41 @@ namespace Foo {
|
||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task MixPrimaryConstructorAndProperties()
|
||||
{
|
||||
var compilation = CreateCompilation(@"
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using EntityFrameworkCore.Projectables;
|
||||
|
||||
namespace Foo {
|
||||
public static class EntityExtensions
|
||||
{
|
||||
public record Entity(int Id)
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string? FullName { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public static Entity Something(Entity entity)
|
||||
=> new Entity(entity.Id) {
|
||||
FullName = entity.FullName
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
");
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user