mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2025-12-06 05:56:10 +00:00
Merge pull request #41 from koenbeuk/support-default-values
Support default parameter values
This commit is contained in:
@@ -21,10 +21,17 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
|
||||
if (visitedNode is ParameterSyntax visitedParameterSyntax)
|
||||
{
|
||||
// Strip the this keyword of any parameter
|
||||
var thisKeywordIndex = visitedParameterSyntax.Modifiers.IndexOf(SyntaxKind.ThisKeyword);
|
||||
if (thisKeywordIndex != -1)
|
||||
{
|
||||
return visitedParameterSyntax.WithModifiers(node.Modifiers.RemoveAt(thisKeywordIndex));
|
||||
visitedNode = visitedParameterSyntax.WithModifiers(node.Modifiers.RemoveAt(thisKeywordIndex));
|
||||
}
|
||||
|
||||
// Remove default values from parameters as this is not accepted in an expression tree
|
||||
if (visitedParameterSyntax.Default is not null)
|
||||
{
|
||||
visitedNode = ((ParameterSyntax)visitedNode).WithDefault(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
SELECT [e].[Id] + 2
|
||||
FROM [Entity] AS [e]
|
||||
@@ -0,0 +1,32 @@
|
||||
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.FunctionalTests
|
||||
{
|
||||
[UsesVerify]
|
||||
public class DefaultValueTests
|
||||
{
|
||||
public record Entity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public int NextId(int skip = 1) => Id + skip;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ExplicitDefaultValueIsSupported()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.NextId(2));
|
||||
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// <auto-generated/>
|
||||
using EntityFrameworkCore.Projectables;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.Generated
|
||||
#nullable disable
|
||||
{
|
||||
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
|
||||
public static class _Foo_Calculate
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo ,int, int>> Expression()
|
||||
{
|
||||
return (global::Foo @this,int i ) =>
|
||||
i;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1427,6 +1427,26 @@ class Derived : Base
|
||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task DefaultValuesGetRemoved()
|
||||
{
|
||||
var compilation = CreateCompilation(@"
|
||||
using EntityFrameworkCore.Projectables;
|
||||
|
||||
class Foo {
|
||||
[Projectable]
|
||||
public int Calculate(int i = 0) => i;
|
||||
}
|
||||
");
|
||||
|
||||
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