mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2025-12-06 05:56:10 +00:00
Merge pull request #42 from koenbeuk/params-arguments
Support for the params keyword
This commit is contained in:
@@ -28,6 +28,13 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
visitedNode = visitedParameterSyntax.WithModifiers(node.Modifiers.RemoveAt(thisKeywordIndex));
|
||||
}
|
||||
|
||||
// Strip the parans keyword of any parameter
|
||||
var paramsKeywordIndex = ((ParameterSyntax)visitedNode).Modifiers.IndexOf(SyntaxKind.ParamsKeyword);
|
||||
if (paramsKeywordIndex != -1)
|
||||
{
|
||||
visitedNode = ((ParameterSyntax)visitedNode).WithModifiers(node.Modifiers.RemoveAt(paramsKeywordIndex));
|
||||
}
|
||||
|
||||
// Remove default values from parameters as this is not accepted in an expression tree
|
||||
if (visitedParameterSyntax.Default is not null)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
SELECT [t].[Id]
|
||||
FROM [TestEntity] AS [t]
|
||||
WHERE [t].[Id] IN (1, 2, 3)
|
||||
@@ -0,0 +1,3 @@
|
||||
SELECT [t].[Id]
|
||||
FROM [TestEntity] AS [t]
|
||||
WHERE [t].[Id] IN (1, 2, 3)
|
||||
@@ -0,0 +1,3 @@
|
||||
SELECT [t].[Id]
|
||||
FROM [TestEntity] AS [t]
|
||||
WHERE [t].[Id] IN (1, 2, 3)
|
||||
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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
|
||||
{
|
||||
[UsesVerify]
|
||||
public class ComplexArgumentsTests
|
||||
{
|
||||
public class TestEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public bool IsValid1(List<int> validIds) => validIds.Contains(Id);
|
||||
|
||||
[Projectable]
|
||||
public bool IsValid2(int[] validIds) => validIds.Contains(Id);
|
||||
|
||||
[Projectable]
|
||||
public bool IsValid3(params int[] validIds) => validIds.Contains(Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ListOfPrimitivesArguments()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<TestEntity>();
|
||||
|
||||
var validList = new List<int>() { 1, 2, 3 };
|
||||
|
||||
var query = dbContext.Set<TestEntity>()
|
||||
.Where(x => x.IsValid1(validList));
|
||||
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ArrayOfPrimitivesArguments()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<TestEntity>();
|
||||
|
||||
var validArray = new[] { 1, 2, 3 };
|
||||
|
||||
var query = dbContext.Set<TestEntity>()
|
||||
.Where(x => x.IsValid2(validArray));
|
||||
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public Task ParamsOfPrimitivesArguments()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<TestEntity>();
|
||||
|
||||
var query = dbContext.Set<TestEntity>()
|
||||
.Where(x => x.IsValid3(1, 2, 3));
|
||||
|
||||
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_First
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo ,int[], int>> Expression()
|
||||
{
|
||||
return (global::Foo @this, int[] numbers) =>
|
||||
numbers[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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_First
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo ,int[], int>> Expression()
|
||||
{
|
||||
return (global::Foo @this,int[] all) =>
|
||||
all[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1447,6 +1447,26 @@ class Foo {
|
||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ParamsModifiedGetsRemoved()
|
||||
{
|
||||
var compilation = CreateCompilation(@"
|
||||
using EntityFrameworkCore.Projectables;
|
||||
|
||||
class Foo {
|
||||
[Projectable]
|
||||
public int First(params int[] all) => all[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)
|
||||
|
||||
Reference in New Issue
Block a user