10 Commits

Author SHA1 Message Date
Steven Decoodt
863d75bdc1 Modular mapping configuration (#162)
* Modular configuration for property mappings

* Update Readme, unit tests and sample

Co-authored-by: Steven Decoodt <steven.decoodt@vinci-energies.net>
2022-01-12 22:22:24 +03:00
Nikita Prokhorov
820358e8ff Fixed null-ref for case-(in)sensitive null-search (#165)
* Fixed null-ref for case-insensitive null-search
Added null-escaping sequence (to distinguish between prop==null (as null) and prop==\null (as string))

* Added null-search case-insensitive test

* Code style

* Added escape-sequences description to README.md

Co-authored-by: Nikita Prokhorov <nikita.prokhorov@grse.de>
2022-01-10 19:29:43 +01:00
AViktorovGRSE
7b6f3c7d85 fixed 163 issue (#164)
* Added ability to skip operators and '\' with \ in filtering.
* Added unit tests for filtering '\|' and skipping operators
2021-12-16 19:50:53 +01:00
Nikita Prokhorov
5ef8843f3d Modified ci-cd definition (on tag pushed) (#159)
Co-authored-by: Nikita Prokhorov <nikita.prokhorov@grse.de>
2021-10-12 09:14:12 +02:00
ITDancer13
6025c7fd44 Release new versions of Sieve via CI (#158) 2021-08-31 10:13:01 +02:00
Luciano Delucchi
1a9371a636 Update README (#155)
Small typo
2021-08-29 16:54:07 +02:00
ITDancer13
e83d213181 Release Sieve 2.5.0 (#151)
* Setup release 2.5.0 with automated build and pre-releases

* #80 added support for escaping pipe control characters (#113)

* #80 added support for escaping comma and pipe control characters

* Update SieveModel.cs

Fix build. Accidentally broken by resolving conflicts.

* Migrate UnitTests to xUnit

Co-authored-by: Clayton Andersen <candersen@restaurant365.com>
Co-authored-by: ITDancer13 <kevin@ksommer.eu>
Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com>

* SieveProcessor.Options made protected property (#134)

Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property)
"IncludeScopes" switch is removed from appSettings.{env}.json files

* Revert to _mapper assignment in constructor. (#140)

* reverting fix (#142)

* Revert to _mapper assignment in constructor.

* reverting fix

* pass filter values as parameters (#112)

make GetClosureOverConstant really work

* Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144)

* stop excluding null values when filtering using notEqual (#114)

* stop excluding null values when filtering using notEqual
* add IgnoreNullsOnNotEqual config field, to enable/disable the new feature

Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>

Co-authored-by: Clayton Andersen <tunaman65@gmail.com>
Co-authored-by: Clayton Andersen <candersen@restaurant365.com>
Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com>
Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com>
Co-authored-by: alicak <alicak@users.noreply.github.com>
Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com>
Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
2021-08-29 16:30:19 +02:00
ITDancer13
83a2c1ab18 Add pre-release badge to Readme.md (#132)
* Add pre-release badge

Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com>
2021-05-15 18:05:58 +02:00
Biarity
74a617722a Update and rename feature_request.md to config.yml 2021-05-14 22:13:36 +00:00
Biarity
c444063453 Update issue templates 2021-05-14 22:09:46 +00:00
25 changed files with 753 additions and 97 deletions

38
.github/ISSUE_TEMPLATE/bug_report.md vendored Normal file
View File

@@ -0,0 +1,38 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''
---
**Describe the bug**
A clear and concise description of what the bug is.
**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error
**Expected behavior**
A clear and concise description of what you expected to happen.
**Screenshots**
If applicable, add screenshots to help explain your problem.
**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]
**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]
**Additional context**
Add any other context about the problem here.

5
.github/ISSUE_TEMPLATE/config.yml vendored Normal file
View File

@@ -0,0 +1,5 @@
blank_issues_enabled: false
contact_links:
- name: Feature Request
url: https://github.com/biarity/sieve/discussions/new
about: Share your ideas on how to make Sieve better.

View File

@@ -17,12 +17,10 @@
name: ci
on:
push:
branches:
- master
pull_request:
branches:
- master
- 'releases/*'
jobs:
ubuntu-latest:

35
.github/workflows/ci_publish.yml vendored Normal file
View File

@@ -0,0 +1,35 @@
# ------------------------------------------------------------------------------
# <auto-generated>
#
# This code was generated.
#
# - To turn off auto-generation set:
#
# [GitHubActions (AutoGenerate = false)]
#
# - To trigger manual generation invoke:
#
# nuke --generate-configuration GitHubActions_ci_publish --host GitHubActions
#
# </auto-generated>
# ------------------------------------------------------------------------------
name: ci_publish
on:
push:
branches:
- 'releases/*'
tags:
- 'v*'
jobs:
ubuntu-latest:
name: ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Run './build.cmd CiPublish'
run: ./build.cmd CiPublish
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}

View File

@@ -43,6 +43,9 @@
"type": "boolean",
"description": "Disables displaying the NUKE logo"
},
"NUGET_API_KEY": {
"type": "string"
},
"Plan": {
"type": "boolean",
"description": "Shows the execution plan (HTML)"
@@ -65,9 +68,11 @@
"type": "string",
"enum": [
"Ci",
"CiPublish",
"Clean",
"Compile",
"Package",
"Publish",
"Restore",
"Test"
]
@@ -84,9 +89,11 @@
"type": "string",
"enum": [
"Ci",
"CiPublish",
"Clean",
"Compile",
"Package",
"Publish",
"Restore",
"Test"
]

View File

@@ -0,0 +1,3 @@
branches:
release:
mode: ContinuousDeployment

View File

@@ -2,7 +2,8 @@
⚗️ Sieve is a simple, clean, and extensible framework for .NET Core that **adds sorting, filtering, and pagination functionality out of the box**.
Most common use case would be for serving ASP.NET Core GET queries.
[![NuGet Release](https://img.shields.io/nuget/v/Sieve.svg?style=flat-square)](https://www.nuget.org/packages/Sieve)
[![NuGet Release](https://img.shields.io/nuget/v/Sieve?style=for-the-badge)](https://www.nuget.org/packages/Sieve)
[![NuGet Pre-Release](https://img.shields.io/nuget/vpre/Sieve?style=for-the-badge)](https://www.nuget.org/packages/Sieve)
[Get Sieve on nuget](https://www.nuget.org/packages/Sieve/)
@@ -74,7 +75,7 @@ Where `SieveCustomSortMethodsOfPosts` for example is:
```C#
public class SieveCustomSortMethods : ISieveCustomSortMethods
{
public IQueryable<Post> Popularity(IQueryable<Post> source, bool useThenBy, bool desc) // The method is given an indicator of weather to use ThenBy(), and if the query is descending
public IQueryable<Post> Popularity(IQueryable<Post> source, bool useThenBy, bool desc) // The method is given an indicator of whether to use ThenBy(), and if the query is descending
{
var result = useThenBy ?
((IOrderedQueryable<Post>)source).ThenBy(p => p.LikeCount) : // ThenBy only works on IOrderedQueryable<TEntity>
@@ -127,7 +128,8 @@ Then you can add the configuration:
"CaseSensitive": "boolean: should property names be case-sensitive? Defaults to false",
"DefaultPageSize": "int number: optional number to fallback to when no page argument is given. Set <=0 to disable paging if no pageSize is specified (default).",
"MaxPageSize": "int number: maximum allowed page size. Set <=0 to make infinite (default)",
"ThrowExceptions": "boolean: should Sieve throw exceptions instead of silently failing? Defaults to false"
"ThrowExceptions": "boolean: should Sieve throw exceptions instead of silently failing? Defaults to false",
"IgnoreNullsOnNotEqual": "boolean: ignore null values when filtering using is not equal operator? Default to true"
}
}
```
@@ -157,7 +159,10 @@ More formally:
* `pageSize` is the number of items returned per page
Notes:
* You can use backslashes to escape commas and pipes within value fields
* You can use backslashes to escape special characters and sequences:
* commas: `Title@=some\,title` makes a match with "some,title"
* pipes: `Title@=some\|title` makes a match with "some|title"
* null values: `Title@=\null` will search for items with title equal to "null" (not a missing value, but "null"-string literally)
* You can have spaces anywhere except *within* `{Name}` or `{Operator}` fields
* If you need to look at the data before applying pagination (eg. get total count), use the optional paramters on `Apply` to defer pagination (an [example](https://github.com/Biarity/Sieve/issues/34))
* Here's a [good example on how to work with enumerables](https://github.com/Biarity/Sieve/issues/2)
@@ -258,13 +263,78 @@ public class ApplicationSieveProcessor : SieveProcessor
}
```
Now you should inject the new class instead:
```C#
services.AddScoped<ISieveProcessor, ApplicationSieveProcessor>();
```
Find More on Sieve's Fluent API [here](https://github.com/Biarity/Sieve/issues/4#issuecomment-364629048).
### Modular Fluent API configuration
Adding all fluent mappings directly in the processor can become unwieldy on larger projects.
It can also clash with vertical architectures.
To enable functional grouping of mappings the `ISieveConfiguration` interface was created together with extensions to the default mapper.
```C#
public class SieveConfigurationForPost : ISieveConfiguration
{
protected override SievePropertyMapper Configure(SievePropertyMapper mapper)
{
mapper.Property<Post>(p => p.Title)
.CanFilter()
.HasName("a_different_query_name_here");
mapper.Property<Post>(p => p.CommentCount)
.CanSort();
mapper.Property<Post>(p => p.DateCreated)
.CanSort()
.CanFilter()
.HasName("created_on");
return mapper;
}
}
```
With the processor simplified to:
```C#
public class ApplicationSieveProcessor : SieveProcessor
{
public ApplicationSieveProcessor(
IOptions<SieveOptions> options,
ISieveCustomSortMethods customSortMethods,
ISieveCustomFilterMethods customFilterMethods)
: base(options, customSortMethods, customFilterMethods)
{
}
protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper)
{
return mapper
.ApplyConfiguration<SieveConfigurationForPost>()
.ApplyConfiguration<SieveConfigurationForComment>();
}
}
```
There is also the option to scan and add all configurations for a given assembly
```C#
public class ApplicationSieveProcessor : SieveProcessor
{
public ApplicationSieveProcessor(
IOptions<SieveOptions> options,
ISieveCustomSortMethods customSortMethods,
ISieveCustomFilterMethods customFilterMethods)
: base(options, customSortMethods, customFilterMethods)
{
}
protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper)
{
return mapper.ApplyConfigurationForAssembly(typeof(ApplicationSieveProcessor).Assembly);
}
}
```
## Upgrading to v2.2.0
2.2.0 introduced OR logic for filter values. This means your custom filters will need to accept multiple values rather than just the one.

View File

@@ -0,0 +1,15 @@
using Sieve.Services;
namespace Sieve.Sample.Entities
{
public class SieveConfigurationForPost : ISieveConfiguration
{
public void Configure(SievePropertyMapper mapper)
{
mapper.Property<Post>(p => p.Title)
.CanSort()
.CanFilter()
.HasName("CustomTitleName");
}
}
}

View File

@@ -13,11 +13,18 @@ namespace Sieve.Sample.Services
protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper)
{
// Option 1: Map all properties centrally
mapper.Property<Post>(p => p.Title)
.CanSort()
.CanFilter()
.HasName("CustomTitleName");
// Option 2: Manually apply functionally grouped mapping configurations
//mapper.ApplyConfiguration<SieveConfigurationForPost>();
// Option 3: Scan and apply all configurations
//mapper.ApplyConfigurationsFromAssembly(typeof(ApplicationSieveProcessor).Assembly);
return mapper;
}
}

View File

@@ -1,6 +1,5 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",

View File

@@ -4,10 +4,10 @@
},
"Sieve": {
"CaseSensitive": false,
"DefaultPageSize": 10
"DefaultPageSize": 10,
"IgnoreNullsOnNotEqual": true
},
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
@@ -6,45 +7,49 @@ namespace Sieve.Models
{
public class FilterTerm : IFilterTerm, IEquatable<FilterTerm>
{
public FilterTerm() { }
private const string EscapedPipePattern = @"(?<!($|[^\\]|^)(\\\\)*?\\)\|";
private const string OperatorsRegEx = @"(!@=\*|!_=\*|!=\*|!@=|!_=|==\*|@=\*|_=\*|==|!=|>=|<=|>|<|@=|_=)";
private const string EscapeNegPatternForOper = @"(?<!\\)" + OperatorsRegEx;
private const string EscapePosPatternForOper = @"(?<=\\)" + OperatorsRegEx;
private const string EscapedPipePattern = @"(?<!($|[^\\])(\\\\)*?\\)\|";
private static readonly string[] Operators = new string[] {
"!@=*",
"!_=*",
"!=*",
"!@=",
"!_=",
"==*",
"@=*",
"_=*",
"==",
"!=",
">=",
"<=",
">",
"<",
"@=",
"_="
private static readonly HashSet<string> _escapedSequences = new HashSet<string>
{
@"\|",
@"\\"
};
public string Filter
{
set
{
var filterSplits = value.Split(Operators, StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.Trim()).ToArray();
var filterSplits = Regex.Split(value,EscapeNegPatternForOper).Select(t => t.Trim()).ToArray();
Names = Regex.Split(filterSplits[0], EscapedPipePattern).Select(t => t.Trim()).ToArray();
Values = filterSplits.Length > 1 ? Regex.Split(filterSplits[1], EscapedPipePattern).Select(t => t.Trim()).ToArray() : null;
Operator = Array.Find(Operators, o => value.Contains(o)) ?? "==";
if (filterSplits.Length > 2)
{
foreach (var match in Regex.Matches(filterSplits[2],EscapePosPatternForOper))
{
var matchStr = match.ToString();
filterSplits[2] = filterSplits[2].Replace('\\' + matchStr, matchStr);
}
Values = Regex.Split(filterSplits[2], EscapedPipePattern)
.Select(UnEscape)
.ToArray();
}
Operator = Regex.Match(value,EscapeNegPatternForOper).Value;
OperatorParsed = GetOperatorParsed(Operator);
OperatorIsCaseInsensitive = Operator.EndsWith("*");
OperatorIsNegated = OperatorParsed != FilterOperator.NotEquals && Operator.StartsWith("!");
}
}
private string UnEscape(string escapedTerm)
=> _escapedSequences.Aggregate(escapedTerm,
(current, sequence) => Regex.Replace(current, $@"(\\)({sequence})", "$2"));
public string[] Names { get; private set; }
public FilterOperator OperatorParsed { get; private set; }
@@ -90,6 +95,5 @@ namespace Sieve.Models
&& Values.SequenceEqual(other.Values)
&& Operator == other.Operator;
}
}
}

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

View File

@@ -9,5 +9,7 @@
public int MaxPageSize { get; set; } = 0;
public bool ThrowExceptions { get; set; } = false;
public bool IgnoreNullsOnNotEqual { get; set; } = true;
}
}

View File

@@ -0,0 +1,70 @@
#nullable enable
using System;
using System.Linq;
using System.Reflection;
namespace Sieve.Services
{
/// <summary>
/// Use this interface to create SieveConfiguration (just like EntityTypeConfigurations are defined for EF)
/// </summary>
public interface ISieveConfiguration
{
/// <summary>
/// Configures sieve property mappings.
/// </summary>
/// <param name="mapper"> The mapper used to configure the sieve properties on. </param>
void Configure(SievePropertyMapper mapper);
}
/// <summary>
/// Configuration extensions to the <see cref="SievePropertyMapper" />
/// </summary>
public static class SieveConfigurationExtensions
{
/// <summary>
/// Applies configuration that is defined in an <see cref="ISieveConfiguration" /> instance.
/// </summary>
/// <param name="mapper"> The mapper to apply the configuration on. </param>
/// <typeparam name="T">The configuration to be applied. </typeparam>
/// <returns>
/// The same <see cref="SievePropertyMapper" /> instance so that additional configuration calls can be chained.
/// </returns>
public static SievePropertyMapper ApplyConfiguration<T>(this SievePropertyMapper mapper) where T : ISieveConfiguration, new()
{
var configuration = new T();
configuration.Configure(mapper);
return mapper;
}
/// <summary>
/// Applies configuration from all <see cref="ISieveConfiguration" />
/// instances that are defined in provided assembly.
/// </summary>
/// <param name="mapper"> The mapper to apply the configuration on. </param>
/// <param name="assembly"> The assembly to scan. </param>
/// <returns>
/// The same <see cref="SievePropertyMapper" /> instance so that additional configuration calls can be chained.
/// </returns>
public static SievePropertyMapper ApplyConfigurationsFromAssembly(this SievePropertyMapper mapper, Assembly assembly)
{
foreach (var type in assembly.GetTypes().Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition))
{
// Only accept types that contain a parameterless constructor, are not abstract.
var noArgConstructor = type.GetConstructor(Type.EmptyTypes);
if (noArgConstructor is null)
{
continue;
}
if (type.GetInterfaces().Any(t => t == typeof(ISieveConfiguration)))
{
var configuration = (ISieveConfiguration)noArgConstructor.Invoke(new object?[] { });
configuration.Configure(mapper);
}
}
return mapper;
}
}
}

View File

@@ -68,7 +68,7 @@ namespace Sieve.Services
where TSortTerm : ISortTerm, new()
{
private const string NullFilterValue = "null";
private readonly IOptions<SieveOptions> _options;
private const char EscapeChar = '\\';
private readonly ISieveCustomSortMethods _customSortMethods;
private readonly ISieveCustomFilterMethods _customFilterMethods;
private readonly SievePropertyMapper _mapper = new SievePropertyMapper();
@@ -78,7 +78,7 @@ namespace Sieve.Services
ISieveCustomFilterMethods customFilterMethods)
{
_mapper = MapProperties(_mapper);
_options = options;
Options = options;
_customSortMethods = customSortMethods;
_customFilterMethods = customFilterMethods;
}
@@ -87,7 +87,7 @@ namespace Sieve.Services
ISieveCustomSortMethods customSortMethods)
{
_mapper = MapProperties(_mapper);
_options = options;
Options = options;
_customSortMethods = customSortMethods;
}
@@ -95,16 +95,18 @@ namespace Sieve.Services
ISieveCustomFilterMethods customFilterMethods)
{
_mapper = MapProperties(_mapper);
_options = options;
Options = options;
_customFilterMethods = customFilterMethods;
}
public SieveProcessor(IOptions<SieveOptions> options)
{
_mapper = MapProperties(_mapper);
_options = options;
Options = options;
}
protected IOptions<SieveOptions> Options { get; }
/// <summary>
/// Apply filtering, sorting, and pagination parameters found in `model` to `source`
/// </summary>
@@ -148,7 +150,7 @@ namespace Sieve.Services
}
catch (Exception ex)
{
if (!_options.Value.ThrowExceptions)
if (!Options.Value.ThrowExceptions)
{
return result;
}
@@ -162,7 +164,7 @@ namespace Sieve.Services
}
}
private IQueryable<TEntity> ApplyFiltering<TEntity>(TSieveModel model, IQueryable<TEntity> result,
protected virtual IQueryable<TEntity> ApplyFiltering<TEntity>(TSieveModel model, IQueryable<TEntity> result,
object[] dataForCustomMethods = null)
{
if (model?.GetFiltersParsed() == null)
@@ -198,7 +200,7 @@ namespace Sieve.Services
? Expression.Constant(null, property.PropertyType)
: ConvertStringValueToConstantExpression(filterTermValue, property, converter);
if (filterTerm.OperatorIsCaseInsensitive)
if (filterTerm.OperatorIsCaseInsensitive && !isFilterTermValueNull)
{
propertyValue = Expression.Call(propertyValue,
typeof(string).GetMethods()
@@ -216,11 +218,14 @@ namespace Sieve.Services
expression = Expression.Not(expression);
}
if (expression.NodeType != ExpressionType.NotEqual || Options.Value.IgnoreNullsOnNotEqual)
{
var filterValueNullCheck = GetFilterValueNullCheck(parameter, fullPropertyName, isFilterTermValueNull);
if (filterValueNullCheck != null)
{
expression = Expression.AndAlso(filterValueNullCheck, expression);
}
}
innerExpression = innerExpression == null
? expression
@@ -253,8 +258,7 @@ namespace Sieve.Services
: result.Where(Expression.Lambda<Func<TEntity, bool>>(outerExpression, parameter));
}
private static Expression GetFilterValueNullCheck(Expression parameter, string fullPropertyName,
bool isFilterTermValueNull)
private static Expression GetFilterValueNullCheck(Expression parameter, string fullPropertyName, bool isFilterTermValueNull)
{
var (propertyValue, nullCheck) = GetPropertyValueAndNullCheckExpression(parameter, fullPropertyName);
@@ -308,6 +312,10 @@ namespace Sieve.Services
private static Expression ConvertStringValueToConstantExpression(string value, PropertyInfo property,
TypeConverter converter)
{
// to allow user to distinguish between prop==null (as null) and prop==\null (as "null"-string)
value = value.Equals(EscapeChar + NullFilterValue, StringComparison.InvariantCultureIgnoreCase)
? value.TrimStart(EscapeChar)
: value;
dynamic constantVal = converter.CanConvertFrom(typeof(string))
? converter.ConvertFrom(value)
: Convert.ChangeType(value, property.PropertyType);
@@ -336,15 +344,13 @@ namespace Sieve.Services
}
// Workaround to ensure that the filter value gets passed as a parameter in generated SQL from EF Core
// See https://github.com/aspnet/EntityFrameworkCore/issues/3361
// Expression.Constant passed the target type to allow Nullable comparison
// See http://bradwilson.typepad.com/blog/2008/07/creating-nullab.html
private static Expression GetClosureOverConstant<T>(T constant, Type targetType)
{
return Expression.Constant(constant, targetType);
Expression<Func<T>> hoistedConstant = () => constant;
return Expression.Convert(hoistedConstant.Body, targetType);
}
private IQueryable<TEntity> ApplySorting<TEntity>(TSieveModel model, IQueryable<TEntity> result,
protected virtual IQueryable<TEntity> ApplySorting<TEntity>(TSieveModel model, IQueryable<TEntity> result,
object[] dataForCustomMethods = null)
{
if (model?.GetSortsParsed() == null)
@@ -373,11 +379,11 @@ namespace Sieve.Services
return result;
}
private IQueryable<TEntity> ApplyPagination<TEntity>(TSieveModel model, IQueryable<TEntity> result)
protected virtual IQueryable<TEntity> ApplyPagination<TEntity>(TSieveModel model, IQueryable<TEntity> result)
{
var page = model?.Page ?? 1;
var pageSize = model?.PageSize ?? _options.Value.DefaultPageSize;
var maxPageSize = _options.Value.MaxPageSize > 0 ? _options.Value.MaxPageSize : pageSize;
var pageSize = model?.PageSize ?? Options.Value.DefaultPageSize;
var maxPageSize = Options.Value.MaxPageSize > 0 ? Options.Value.MaxPageSize : pageSize;
if (pageSize <= 0)
{
@@ -399,14 +405,14 @@ namespace Sieve.Services
string name)
{
var property = _mapper.FindProperty<TEntity>(canSortRequired, canFilterRequired, name,
_options.Value.CaseSensitive);
Options.Value.CaseSensitive);
if (property.Item1 != null)
{
return property;
}
var prop = FindPropertyBySieveAttribute<TEntity>(canSortRequired, canFilterRequired, name,
_options.Value.CaseSensitive);
Options.Value.CaseSensitive);
return (prop?.Name, prop);
}
@@ -426,7 +432,7 @@ namespace Sieve.Services
{
var customMethod = parent?.GetType()
.GetMethodExt(name,
_options.Value.CaseSensitive
Options.Value.CaseSensitive
? BindingFlags.Default
: BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance,
typeof(IQueryable<TEntity>));
@@ -437,7 +443,7 @@ namespace Sieve.Services
// Find generic methods `public IQueryable<T> Filter<T>(IQueryable<T> source, ...)`
var genericCustomMethod = parent?.GetType()
.GetMethodExt(name,
_options.Value.CaseSensitive
Options.Value.CaseSensitive
? BindingFlags.Default
: BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance,
typeof(IQueryable<>));
@@ -481,11 +487,11 @@ namespace Sieve.Services
var incompatibleCustomMethods =
parent?
.GetType()
.GetMethods(_options.Value.CaseSensitive
.GetMethods(Options.Value.CaseSensitive
? BindingFlags.Default
: BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)
.Where(method => string.Equals(method.Name, name,
_options.Value.CaseSensitive
Options.Value.CaseSensitive
? StringComparison.InvariantCulture
: StringComparison.InvariantCultureIgnoreCase))
.ToList()

View File

@@ -0,0 +1,37 @@
using Sieve.Services;
namespace SieveUnitTests.Abstractions.Entity
{
public class SieveConfigurationForIPost : ISieveConfiguration
{
public void Configure(SievePropertyMapper mapper)
{
mapper.Property<IPost>(p => p.ThisHasNoAttributeButIsAccessible)
.CanSort()
.CanFilter()
.HasName("shortname");
mapper.Property<IPost>(p => p.TopComment.Text)
.CanFilter();
mapper.Property<IPost>(p => p.TopComment.Id)
.CanSort();
mapper.Property<IPost>(p => p.OnlySortableViaFluentApi)
.CanSort();
mapper.Property<IPost>(p => p.TopComment.Text)
.CanFilter()
.HasName("topc");
mapper.Property<IPost>(p => p.FeaturedComment.Text)
.CanFilter()
.HasName("featc");
mapper
.Property<IPost>(p => p.DateCreated)
.CanSort()
.HasName("CreateDate");
}
}
}

View File

@@ -0,0 +1,37 @@
using Sieve.Services;
namespace SieveUnitTests.Entities
{
public class SieveConfigurationForPost : ISieveConfiguration
{
public void Configure(SievePropertyMapper mapper)
{
mapper.Property<Post>(p => p.ThisHasNoAttributeButIsAccessible)
.CanSort()
.CanFilter()
.HasName("shortname");
mapper.Property<Post>(p => p.TopComment.Text)
.CanFilter();
mapper.Property<Post>(p => p.TopComment.Id)
.CanSort();
mapper.Property<Post>(p => p.OnlySortableViaFluentApi)
.CanSort();
mapper.Property<Post>(p => p.TopComment.Text)
.CanFilter()
.HasName("topc");
mapper.Property<Post>(p => p.FeaturedComment.Text)
.CanFilter()
.HasName("featc");
mapper
.Property<Post>(p => p.DateCreated)
.CanSort()
.HasName("CreateDate");
}
}
}

View File

@@ -15,16 +15,24 @@ namespace SieveUnitTests
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly SieveProcessor _processor;
private readonly SieveProcessor _nullableProcessor;
private readonly IQueryable<Post> _posts;
private readonly IQueryable<Comment> _comments;
public General(ITestOutputHelper testOutputHelper)
{
var nullableAccessor = new SieveOptionsAccessor();
nullableAccessor.Value.IgnoreNullsOnNotEqual = false;
_testOutputHelper = testOutputHelper;
_processor = new ApplicationSieveProcessor(new SieveOptionsAccessor(),
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());
_nullableProcessor = new ApplicationSieveProcessor(nullableAccessor,
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());
_posts = new List<Post>
{
new Post
@@ -65,7 +73,7 @@ namespace SieveUnitTests
CategoryId = 2,
TopComment = new Comment { Id = 1, Text = "D1" },
FeaturedComment = new Comment { Id = 7, Text = "D2" }
},
}
}.AsQueryable();
_comments = new List<Comment>
@@ -180,8 +188,25 @@ namespace SieveUnitTests
};
var result = _processor.Apply(model, _posts);
var nullableResult = _nullableProcessor.Apply(model, _posts);
Assert.True(result.Count() == 2);
Assert.True(nullableResult.Count() == 2);
}
[Fact]
public void CanFilterNullableIntsWithNotEqual()
{
var model = new SieveModel()
{
Filters = "CategoryId!=1"
};
var result = _processor.Apply(model, _posts);
var nullableResult = _nullableProcessor.Apply(model, _posts);
Assert.True(result.Count() == 1);
Assert.True(nullableResult.Count() == 2);
}
[Theory]
@@ -613,5 +638,162 @@ namespace SieveUnitTests
Assert.Equal(1,posts[2].Id);
Assert.Equal(0,posts[3].Id);
}
[Fact]
public void CanFilter_WithEscapeCharacter()
{
var comments = new List<Comment>
{
new Comment
{
Id = 0,
DateCreated = DateTimeOffset.UtcNow,
Text = "Here is, a comment"
},
new Comment
{
Id = 1,
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
Text = "Here is, another comment"
},
}.AsQueryable();
var model = new SieveModel
{
Filters = "Text==Here is\\, another comment"
};
var result = _processor.Apply(model, comments);
Assert.Equal(1, result.Count());
}
[Fact]
public void OrEscapedPipeValueFilteringWorks()
{
var comments = new List<Comment>
{
new Comment
{
Id = 0,
DateCreated = DateTimeOffset.UtcNow,
Text = "Here is | a comment"
},
new Comment
{
Id = 1,
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
Text = "Here is | another comment"
},
new Comment
{
Id = 2,
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
Text = @"Here is \| another comment(1)"
}
}.AsQueryable();
var model = new SieveModel
{
Filters = @"Text==Here is \| a comment|Here is \| another comment|Here is \\\| another comment(1)",
};
var result = _processor.Apply(model, comments);
Assert.Equal(3, result.Count());
}
[Theory]
[InlineData("CategoryId==1,(CategoryId|LikeCount)==50")]
[InlineData("(CategoryId|LikeCount)==50,CategoryId==1")]
public void CanFilterWithEscape(string filter)
{
var model = new SieveModel
{
Filters = filter
};
var result = _processor.Apply(model, _posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();
Assert.NotNull(entry);
Assert.Equal(1, resultCount);
}
[Theory]
[InlineData(@"Title@=\\")]
public void CanFilterWithEscapedBackSlash(string filter)
{
var posts = new List<Post>
{
new Post
{
Id = 1,
Title = "E\\",
LikeCount = 4,
IsDraft = true,
CategoryId = 1,
TopComment = new Comment { Id = 1, Text = "E1" },
FeaturedComment = new Comment { Id = 7, Text = "E2" }
}
}.AsQueryable();
var model = new SieveModel
{
Filters = filter
};
var result = _processor.Apply(model, posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();
Assert.NotNull(entry);
Assert.Equal(1, resultCount);
}
[Theory]
[InlineData(@"Title@=\== ")]
[InlineData(@"Title@=\!= ")]
[InlineData(@"Title@=\> ")]
[InlineData(@"Title@=\< ")]
[InlineData(@"Title@=\<= ")]
[InlineData(@"Title@=\>= ")]
[InlineData(@"Title@=\@= ")]
[InlineData(@"Title@=\_= ")]
[InlineData(@"Title@=!\@= ")]
[InlineData(@"Title@=!\_= ")]
[InlineData(@"Title@=\@=* ")]
[InlineData(@"Title@=\_=* ")]
[InlineData(@"Title@=\==* ")]
[InlineData(@"Title@=\!=* ")]
[InlineData(@"Title@=!\@=* ")]
public void CanFilterWithEscapedOperators(string filter)
{
var posts = new List<Post>
{
new Post
{
Id = 1,
Title = @"Operators: == != > < >= <= @= _= !@= !_= @=* _=* ==* !=* !@=* !_=* ",
LikeCount = 1,
IsDraft = true,
CategoryId = 1,
TopComment = new Comment { Id = 1, Text = "F1" },
FeaturedComment = new Comment { Id = 7, Text = "F2" }
}
}.AsQueryable();
var model = new SieveModel
{
Filters = filter,
};
var result = _processor.Apply(model, posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();
Assert.NotNull(entry);
Assert.Equal(1, resultCount);
}
}
}

View File

@@ -16,16 +16,24 @@ namespace SieveUnitTests
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly SieveProcessor _processor;
private readonly SieveProcessor _nullableProcessor;
private readonly IQueryable<IPost> _posts;
private readonly IQueryable<Comment> _comments;
public GeneralWithInterfaces(ITestOutputHelper testOutputHelper)
{
var nullableAccessor = new SieveOptionsAccessor();
nullableAccessor.Value.IgnoreNullsOnNotEqual = false;
_testOutputHelper = testOutputHelper;
_processor = new ApplicationSieveProcessor(new SieveOptionsAccessor(),
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());
_nullableProcessor = new ApplicationSieveProcessor(nullableAccessor,
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());
_posts = new List<IPost>
{
new Post
@@ -181,8 +189,25 @@ namespace SieveUnitTests
};
var result = _processor.Apply(model, _posts);
var nullableResult = _nullableProcessor.Apply(model, _posts);
Assert.True(result.Count() == 2);
Assert.True(nullableResult.Count() == 2);
}
[Fact]
public void CanFilterNullableIntsWithNotEqual()
{
var model = new SieveModel()
{
Filters = "CategoryId!=1"
};
var result = _processor.Apply(model, _posts);
var nullableResult = _nullableProcessor.Apply(model, _posts);
Assert.True(result.Count() == 1);
Assert.True(nullableResult.Count() == 2);
}
[Fact]

View File

@@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq;
using Sieve.Exceptions;
using Sieve.Models;
using Sieve.Services;
using SieveUnitTests.Entities;
using SieveUnitTests.Services;
using Xunit;
@@ -10,15 +11,10 @@ namespace SieveUnitTests
{
public class Mapper
{
private readonly ApplicationSieveProcessor _processor;
private readonly IQueryable<Post> _posts;
public Mapper()
{
_processor = new ApplicationSieveProcessor(new SieveOptionsAccessor(),
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());
_posts = new List<Post>
{
new Post
@@ -45,23 +41,49 @@ namespace SieveUnitTests
}.AsQueryable();
}
[Fact]
public void MapperWorks()
/// <summary>
/// Processors with the same mappings but configured via a different method.
/// </summary>
/// <returns></returns>
public static IEnumerable<object[]> GetProcessors()
{
yield return new object[] {
new ApplicationSieveProcessor(
new SieveOptionsAccessor(),
new SieveCustomSortMethods(),
new SieveCustomFilterMethods())};
yield return new object[] {
new ModularConfigurationSieveProcessor(
new SieveOptionsAccessor(),
new SieveCustomSortMethods(),
new SieveCustomFilterMethods())};
yield return new object[] {
new ModularConfigurationWithScanSieveProcessor(
new SieveOptionsAccessor(),
new SieveCustomSortMethods(),
new SieveCustomFilterMethods())};
}
[Theory]
[MemberData(nameof(GetProcessors))]
public void MapperWorks(ISieveProcessor processor)
{
var model = new SieveModel
{
Filters = "shortname@=A",
};
var result = _processor.Apply(model, _posts);
var result = processor.Apply(model, _posts);
Assert.Equal("A", result.First().ThisHasNoAttributeButIsAccessible);
Assert.True(result.Count() == 1);
}
[Fact]
public void MapperSortOnlyWorks()
[Theory]
[MemberData(nameof(GetProcessors))]
public void MapperSortOnlyWorks(ISieveProcessor processor)
{
var model = new SieveModel
{
@@ -69,9 +91,9 @@ namespace SieveUnitTests
Sorts = "OnlySortableViaFluentApi"
};
var result = _processor.Apply(model, _posts, applyFiltering: false, applyPagination: false);
var result = processor.Apply(model, _posts, applyFiltering: false, applyPagination: false);
Assert.Throws<SieveMethodNotFoundException>(() => _processor.Apply(model, _posts));
Assert.Throws<SieveMethodNotFoundException>(() => processor.Apply(model, _posts));
Assert.Equal(3, result.First().Id);

View File

@@ -0,0 +1,26 @@
using Microsoft.Extensions.Options;
using Sieve.Models;
using Sieve.Services;
using SieveUnitTests.Abstractions.Entity;
using SieveUnitTests.Entities;
namespace SieveUnitTests.Services
{
public class ModularConfigurationSieveProcessor : SieveProcessor
{
public ModularConfigurationSieveProcessor(
IOptions<SieveOptions> options,
ISieveCustomSortMethods customSortMethods,
ISieveCustomFilterMethods customFilterMethods)
: base(options, customSortMethods, customFilterMethods)
{
}
protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper)
{
return mapper
.ApplyConfiguration<SieveConfigurationForPost>()
.ApplyConfiguration<SieveConfigurationForIPost>();
}
}
}

View File

@@ -0,0 +1,20 @@
using Microsoft.Extensions.Options;
using Sieve.Models;
using Sieve.Services;
namespace SieveUnitTests.Services
{
public class ModularConfigurationWithScanSieveProcessor : SieveProcessor
{
public ModularConfigurationWithScanSieveProcessor(
IOptions<SieveOptions> options,
ISieveCustomSortMethods customSortMethods,
ISieveCustomFilterMethods customFilterMethods)
: base(options, customSortMethods, customFilterMethods)
{
}
protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper) =>
mapper.ApplyConfigurationsFromAssembly(typeof(ModularConfigurationWithScanSieveProcessor).Assembly);
}
}

View File

@@ -38,7 +38,7 @@ namespace SieveUnitTests
{
Id = 2,
DateCreated = DateTimeOffset.UtcNow,
Text = "Regular comment without n*ll.",
Text = "Regular comment without n*ll",
Author = "Mouse",
},
new Comment
@@ -47,24 +47,28 @@ namespace SieveUnitTests
DateCreated = DateTimeOffset.UtcNow,
Text = null,
Author = "null",
},
}
}.AsQueryable();
}
[Fact]
public void Filter_Equals_Null()
[Theory]
[InlineData("Text==null")]
[InlineData("Text==*null")]
public void Filter_Equals_Null(string filter)
{
var model = new SieveModel {Filters = "Text==null"};
var model = new SieveModel {Filters = filter};
var result = _processor.Apply(model, _comments);
Assert.Equal(100, result.Single().Id);
}
[Fact]
public void Filter_NotEquals_Null()
[Theory]
[InlineData("Text!=null")]
[InlineData("Text!=*null")]
public void Filter_NotEquals_Null(string filter)
{
var model = new SieveModel {Filters = "Text!=null"};
var model = new SieveModel {Filters = filter};
var result = _processor.Apply(model, _comments);
@@ -101,6 +105,22 @@ namespace SieveUnitTests
Assert.Equal(expectedIds, result.Select(p => p.Id));
}
[Theory]
[InlineData(@"Author==\null", 100)]
[InlineData(@"Author==*\null", 100)]
[InlineData(@"Author==*\NuLl", 100)]
[InlineData(@"Author!=*\null", 0, 1, 2)]
[InlineData(@"Author!=*\NulL", 0, 1, 2)]
[InlineData(@"Author!=\null", 0, 1, 2)]
public void SingleFilter_Equals_NullStringEscaped(string filter, params int[] expectedIds)
{
var model = new SieveModel {Filters = filter};
var result = _processor.Apply(model, _comments);
Assert.Equal(expectedIds, result.Select(p => p.Id));
}
[Theory]
[InlineData("Text_=null")]
[InlineData("Text_=*null")]

View File

@@ -1,4 +1,5 @@
using System.Linq;
using GlobExpressions;
using Nuke.Common;
using Nuke.Common.CI;
using Nuke.Common.CI.GitHubActions;
@@ -8,17 +9,24 @@ using Nuke.Common.IO;
using Nuke.Common.ProjectModel;
using Nuke.Common.Tools.DotNet;
using Nuke.Common.Tools.GitVersion;
using Nuke.Common.Utilities.Collections;
using static Nuke.Common.IO.FileSystemTasks;
using static Nuke.Common.Tools.DotNet.DotNetTasks;
[CheckBuildProjectConfigurations]
[ShutdownDotNetAfterServerBuild]
[GitHubActions("ci", GitHubActionsImage.UbuntuLatest,
OnPushBranches = new[] {"master"},
OnPullRequestBranches = new[] {"master"},
OnPullRequestBranches = new[] {"master", "releases/*"},
AutoGenerate = true,
InvokedTargets = new[] {nameof(Ci)},
CacheKeyFiles = new string[0])]
[GitHubActions("ci_publish", GitHubActionsImage.UbuntuLatest,
OnPushBranches = new[] { "releases/*" },
OnPushTags = new[] { "v*" },
AutoGenerate = true,
InvokedTargets = new[] {nameof(CiPublish)},
CacheKeyFiles = new string[0],
ImportSecrets = new[] {"NUGET_API_KEY"})]
class Build : NukeBuild
{
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
@@ -30,6 +38,9 @@ class Build : NukeBuild
[Solution] readonly Solution Solution;
// ReSharper disable once InconsistentNaming
[Parameter] string NUGET_API_KEY;
Project SieveProject => Solution.AllProjects.First(p => p.Name == "Sieve");
AbsolutePath OutputDirectory => RootDirectory / "output";
@@ -73,6 +84,7 @@ class Build : NukeBuild
Target Package => _ => _
.DependsOn(Test)
.Executes(() =>
{
DotNetPack(s => s
.SetProject(SieveProject)
@@ -83,13 +95,29 @@ class Build : NukeBuild
.EnableNoBuild());
});
Target Ci => _ => _
.DependsOn(Package);
Target Publish => _ => _
.DependsOn(Package)
.Requires(() => IsServerBuild)
.Requires(() => NUGET_API_KEY)
.Requires(() => Configuration.Equals(Configuration.Release))
.Executes(() =>
{
Glob.Files(OutputDirectory, "*.nupkg")
.NotEmpty()
.ForEach(x =>
{
DotNetNuGetPush(s => s
.SetTargetPath(OutputDirectory / x)
.SetSource("https://api.nuget.org/v3/index.json")
.SetApiKey(NUGET_API_KEY));
});
});
Target Ci => _ => _
.DependsOn(Test);
Target CiPublish => _ => _
.DependsOn(Publish);
/// Support plugins are available for:
/// - JetBrains ReSharper https://nuke.build/resharper
/// - JetBrains Rider https://nuke.build/rider
/// - Microsoft VisualStudio https://nuke.build/visualstudio
/// - Microsoft VSCode https://nuke.build/vscode
public static int Main() => Execute<Build>(x => x.Package);
}