From 709e3d6fae2a3ec37cbe85d98403f6d0206710c7 Mon Sep 17 00:00:00 2001 From: Michael Buchoff Date: Mon, 27 May 2024 16:11:10 -0400 Subject: [PATCH] Added static filters --- Sieve.Sample/Sieve.Sample.csproj | 2 +- Sieve/Services/SieveProcessor.cs | 4 +-- SieveUnitTests/General.cs | 26 ++++++++++++++++++- .../Services/SieveCustomFilterMethods.cs | 7 +++++ SieveUnitTests/SieveUnitTests.csproj | 2 +- build/_build.csproj | 2 +- 6 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Sieve.Sample/Sieve.Sample.csproj b/Sieve.Sample/Sieve.Sample.csproj index 8aca6b9..d71ce5b 100644 --- a/Sieve.Sample/Sieve.Sample.csproj +++ b/Sieve.Sample/Sieve.Sample.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net8.0 diff --git a/Sieve/Services/SieveProcessor.cs b/Sieve/Services/SieveProcessor.cs index 5b36ea1..7736130 100644 --- a/Sieve/Services/SieveProcessor.cs +++ b/Sieve/Services/SieveProcessor.cs @@ -436,8 +436,8 @@ namespace Sieve.Services var customMethod = parent?.GetType() .GetMethodExt(name, Options.Value.CaseSensitive - ? BindingFlags.Default - : BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance, + ? BindingFlags.Default | BindingFlags.Static + : BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, typeof(IQueryable)); diff --git a/SieveUnitTests/General.cs b/SieveUnitTests/General.cs index 886ce50..66b1d1f 100644 --- a/SieveUnitTests/General.cs +++ b/SieveUnitTests/General.cs @@ -847,6 +847,30 @@ namespace SieveUnitTests Assert.NotNull(entry); Assert.Equal(1, resultCount); } - + + [Theory] + [InlineData(@"HasInTitleStatic@=Tale", 1)] + [InlineData(@"HasInTitleStatic@=Tail", 0)] + public void CanFilterWithStaticFilters(string filter, int expectedMatches) + { + var posts = new List + { + new() + { + Id = 1, + Title = "A Tale of Two Cities", + } + }.AsQueryable(); + + var model = new SieveModel + { + Filters = filter + }; + + var result = _processor.Apply(model, posts); + var resultCount = result.Count(); + Assert.Equal(expectedMatches, resultCount); + } + } } diff --git a/SieveUnitTests/Services/SieveCustomFilterMethods.cs b/SieveUnitTests/Services/SieveCustomFilterMethods.cs index 0f534e3..8630f2a 100644 --- a/SieveUnitTests/Services/SieveCustomFilterMethods.cs +++ b/SieveUnitTests/Services/SieveCustomFilterMethods.cs @@ -54,6 +54,13 @@ namespace SieveUnitTests.Services return result; } + public static IQueryable HasInTitleStatic(IQueryable source, string op, string[] values) + { + var result = source.Where(p => p.Title.Contains(values[0])); + + return result; + } + public IQueryable IsNew(IQueryable source, string op, string[] values) { var result = source.Where(c => c.DateCreated > DateTimeOffset.UtcNow.AddDays(-2)); diff --git a/SieveUnitTests/SieveUnitTests.csproj b/SieveUnitTests/SieveUnitTests.csproj index 1c26e54..4778282 100644 --- a/SieveUnitTests/SieveUnitTests.csproj +++ b/SieveUnitTests/SieveUnitTests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net8.0 false diff --git a/build/_build.csproj b/build/_build.csproj index c432dd2..f114ddd 100644 --- a/build/_build.csproj +++ b/build/_build.csproj @@ -2,7 +2,7 @@ Exe - net5.0 + net8.0 CS0649;CS0169 ..