Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumerable filters project #464

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions YesSql.sln
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YesSql.Filters.Abstractions
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YesSql.Filters.Query", "src\YesSql.Filters.Query\YesSql.Filters.Query.csproj", "{86C3967F-B817-4119-B354-B6EB1AC1F237}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YesSql.Filters.Enumerable", "src\YesSql.Filters.Enumerable\YesSql.Filters.Enumerable.csproj", "{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -302,6 +304,22 @@ Global
{86C3967F-B817-4119-B354-B6EB1AC1F237}.Release|x64.Build.0 = Release|Any CPU
{86C3967F-B817-4119-B354-B6EB1AC1F237}.Release|x86.ActiveCfg = Release|Any CPU
{86C3967F-B817-4119-B354-B6EB1AC1F237}.Release|x86.Build.0 = Release|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Debug|x64.ActiveCfg = Debug|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Debug|x64.Build.0 = Debug|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Debug|x86.ActiveCfg = Debug|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Debug|x86.Build.0 = Debug|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Release|Any CPU.Build.0 = Release|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Release|x64.ActiveCfg = Release|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Release|x64.Build.0 = Release|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Release|x86.ActiveCfg = Release|Any CPU
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -322,6 +340,7 @@ Global
{6BAC7887-02FB-4B6F-BB5C-D6DEF9646CC6} = {0C294EC4-E6EF-4839-AD34-335C1A5112F9}
{348B307C-66F0-4DBE-BA96-5D9DFAB5588E} = {0C294EC4-E6EF-4839-AD34-335C1A5112F9}
{86C3967F-B817-4119-B354-B6EB1AC1F237} = {0C294EC4-E6EF-4839-AD34-335C1A5112F9}
{8F8F2268-9EE8-4BEC-B60E-44DFC122F6BF} = {0C294EC4-E6EF-4839-AD34-335C1A5112F9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A6AB2945-2138-42B9-8F82-FCCF4DFC8F55}
Expand Down
20 changes: 20 additions & 0 deletions src/YesSql.Filters.Enumerable/EnumerableBooleanEngineBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using YesSql;
using YesSql.Filters.Abstractions.Builders;
using YesSql.Filters.Enumerable.Services;

namespace YesSql.Filters.Enumerable
{
public class EnumerableBooleanEngineBuilder<T> : BooleanEngineBuilder<T, EnumerableTermOption<T>> where T : class
{
public EnumerableBooleanEngineBuilder(
string name,
Func<string, IEnumerable<T>, EnumerableExecutionContext<T>, ValueTask<IEnumerable<T>>> matchQuery,
Func<string, IEnumerable<T>, EnumerableExecutionContext<T>, ValueTask<IEnumerable<T>>> notMatchQuery)
{
_termOption = new EnumerableTermOption<T>(name, matchQuery, notMatchQuery);
}
}
}
33 changes: 33 additions & 0 deletions src/YesSql.Filters.Enumerable/EnumerableEngineBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using YesSql.Filters.Abstractions.Builders;
using YesSql.Filters.Enumerable.Services;

namespace YesSql.Filters.Enumerable
{
/// <summary>
/// Builds a <see cref="EnumerableEngineBuilder{T}"/> for an <see cref="IEnumerable{T}"/>.
/// </summary>
public class EnumerableEngineBuilder<T> where T : class
{
private readonly Dictionary<string, TermEngineBuilder<T, EnumerableTermOption<T>>> _termBuilders = new Dictionary<string, TermEngineBuilder<T, EnumerableTermOption<T>>>();

public EnumerableEngineBuilder<T> SetTermParser(TermEngineBuilder<T, EnumerableTermOption<T>> builder)
{
_termBuilders[builder.Name] = builder;

return this;
}

public IEnumerableParser<T> Build()
{
var builders = _termBuilders.Values.Select(x => x.Build());

var parsers = builders.Select(x => x.Parser).ToArray();
var termOptions = builders.Select(x => x.TermOption).ToDictionary(k => k.Name, v => v, StringComparer.OrdinalIgnoreCase);

return new EnumerableParser<T>(parsers, termOptions);
}
}
}
33 changes: 33 additions & 0 deletions src/YesSql.Filters.Enumerable/EnumerableEngineBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using YesSql.Filters.Abstractions.Builders;
using YesSql.Filters.Enumerable.Services;

namespace YesSql.Filters.Enumerable
{
public static class EnumerableEngineBuilderExtensions
{
/// <summary>
/// Adds a term where the name must be specified to an <see cref="EnumerableEngineBuilder{T}"/>
/// </summary>
public static EnumerableEngineBuilder<T> WithNamedTerm<T>(this EnumerableEngineBuilder<T> builder, string name, Action<NamedTermEngineBuilder<T, EnumerableTermOption<T>>> action) where T : class
{
var parserBuilder = new NamedTermEngineBuilder<T, EnumerableTermOption<T>>(name);
action(parserBuilder);

builder.SetTermParser(parserBuilder);
return builder;
}

/// <summary>
/// Adds a term where the name is optional to an <see cref="EnumerableEngineBuilder{T}"/>
/// </summary>
public static EnumerableEngineBuilder<T> WithDefaultTerm<T>(this EnumerableEngineBuilder<T> builder, string name, Action<DefaultTermEngineBuilder<T, EnumerableTermOption<T>>> action) where T : class
{
var parserBuilder = new DefaultTermEngineBuilder<T, EnumerableTermOption<T>>(name);
action(parserBuilder);

builder.SetTermParser(parserBuilder);
return builder;
}
}
}
69 changes: 69 additions & 0 deletions src/YesSql.Filters.Enumerable/EnumerableFilterResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using YesSql.Filters.Abstractions.Nodes;
using YesSql.Filters.Abstractions.Services;
using YesSql.Filters.Enumerable.Services;

namespace YesSql.Filters.Enumerable
{
public class EnumerableFilterResult<T> : FilterResult<T, EnumerableTermOption<T>> where T : class
{
public EnumerableFilterResult(IReadOnlyDictionary<string, EnumerableTermOption<T>> termOptions) : base(termOptions)
{ }

public EnumerableFilterResult(List<TermNode> terms, IReadOnlyDictionary<string, EnumerableTermOption<T>> termOptions) : base(terms, termOptions)
{ }

public void MapFrom<TModel>(TModel model)
{
foreach (var option in TermOptions)
{
if (option.Value.MapFrom is Action<EnumerableFilterResult<T>, string, TermOption, TModel> mappingMethod)
{
mappingMethod(this, option.Key, option.Value, model);
}
}
}

/// <summary>
/// Applies term filters to an <see cref="IEnumerable{T}"/>
/// </summary>
public async ValueTask<IEnumerable<T>> ExecuteAsync(EnumerableExecutionContext<T> context)
{
var visitor = new EnumerableFilterVisitor<T>();

foreach (var term in _terms.Values)
{
// TODO optimize value task.
await VisitTerm(TermOptions, context, visitor, term);
}

// Execute always run terms. These are not added to the terms list.
foreach (var termOption in TermOptions)
{
if (!termOption.Value.AlwaysRun)
{
continue;
}

if (!_terms.ContainsKey(termOption.Key))
{
var alwaysRunNode = new NamedTermNode(termOption.Key, new UnaryNode(String.Empty, OperateNodeQuotes.None));
await VisitTerm(TermOptions, context, visitor, alwaysRunNode);
}
}

return context.Item;
}

private async static Task VisitTerm(IReadOnlyDictionary<string, EnumerableTermOption<T>> termOptions, EnumerableExecutionContext<T> context, EnumerableFilterVisitor<T> visitor, TermNode term)
{
context.CurrentTermOption = termOptions[term.TermName];

var termQuery = visitor.Visit(term, context);
context.Item = await termQuery.Invoke(context.Item);
context.CurrentTermOption = null;
}
}
}
12 changes: 12 additions & 0 deletions src/YesSql.Filters.Enumerable/EnumerableFilterResultExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using YesSql.Filters.Enumerable.Services;

namespace YesSql.Filters.Enumerable
{
public static class EnumerableFilterResultExtensions
{
public static ValueTask<IEnumerable<T>> ExecuteAsync<T>(this EnumerableFilterResult<T> result, IEnumerable<T> query) where T : class
=> result.ExecuteAsync(new EnumerableExecutionContext<T>(query));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Threading.Tasks;
using YesSql.Filters.Abstractions.Builders;
using YesSql.Filters.Enumerable.Services;
using System.Collections.Generic;

namespace YesSql.Filters.Enumerable
{
public static class QueryTermFilterBuilderExtensions
{
/// <summary>
/// Adds a single condition to a <see cref="TermEngineBuilder{T, TTermOption}"/>.
/// <param name="builder"></param>.
/// <param name="matchQuery">The predicate to apply when the term is parsed.</param>
/// </summary>
public static EnumerableUnaryEngineBuilder<T> OneCondition<T>(this TermEngineBuilder<T, EnumerableTermOption<T>> builder, Func<string, IEnumerable<T>, IEnumerable<T>> matchQuery) where T : class
{
ValueTask<IEnumerable<T>> valueQuery(string q, IEnumerable<T> val, EnumerableExecutionContext<T> ctx) => new(matchQuery(q, val));

return builder.OneCondition(valueQuery);
}

/// <summary>
/// Adds a single condition to a <see cref="TermEngineBuilder{T, TTermOption}"/>.
/// <param name="builder"></param>
/// <param name="matchQuery">An async predicate to apply when the term is parsed.</param>
/// </summary>
public static EnumerableUnaryEngineBuilder<T> OneCondition<T>(this TermEngineBuilder<T, EnumerableTermOption<T>> builder, Func<string, IEnumerable<T>, EnumerableExecutionContext<T>, ValueTask<IEnumerable<T>>> matchQuery) where T : class
{
var operatorBuilder = new EnumerableUnaryEngineBuilder<T>(builder.Name, matchQuery);
builder.SetOperator(operatorBuilder);

return operatorBuilder;
}

/// <summary>
/// Adds a condition which supports many operations to a <see cref="TermEngineBuilder{T, TTermOption}"/>
/// <param name="builder"></param>
/// <param name="matchQuery">The predicate to apply when the term is parsed with an AND or OR operator.</param>
/// <param name="notMatchQuery">The predicate to apply when the term is parsed with a NOT operator.</param>
/// </summary>
public static EnumerableBooleanEngineBuilder<T> ManyCondition<T>(
this TermEngineBuilder<T, EnumerableTermOption<T>> builder,
Func<string, IEnumerable<T>, IEnumerable<T>> matchQuery,
Func<string, IEnumerable<T>, IEnumerable<T>> notMatchQuery) where T : class
{
ValueTask<IEnumerable<T>> valueMatch(string q, IEnumerable<T> val, EnumerableExecutionContext<T> ctx) => new(matchQuery(q, val));
ValueTask<IEnumerable<T>> valueNotMatch(string q, IEnumerable<T> val, EnumerableExecutionContext<T> ctx) => new(notMatchQuery(q, val));

return builder.ManyCondition(valueMatch, valueNotMatch);
}

/// <summary>
/// Adds a condition which supports many operations to a <see cref="TermEngineBuilder{T, TTermOption}"/>
/// <param name="builder"></param>.
/// <param name="matchQuery">The predicate to apply when the term is parsed with an AND or OR operator.</param>
/// <param name="notMatchQuery">The predicate to apply when the term is parsed with a NOT operator.</param>
/// </summary>
public static EnumerableBooleanEngineBuilder<T> ManyCondition<T>(
this TermEngineBuilder<T, EnumerableTermOption<T>> builder,
Func<string, IEnumerable<T>, EnumerableExecutionContext<T>, ValueTask<IEnumerable<T>>> matchQuery,
Func<string, IEnumerable<T>, EnumerableExecutionContext<T>, ValueTask<IEnumerable<T>>> notMatchQuery) where T : class
{
var operatorBuilder = new EnumerableBooleanEngineBuilder<T>(builder.Name, matchQuery, notMatchQuery);
builder.SetOperator(operatorBuilder);

return operatorBuilder;
}
}
}
70 changes: 70 additions & 0 deletions src/YesSql.Filters.Enumerable/EnumerableUnaryEngineBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Threading.Tasks;
using YesSql.Filters.Abstractions.Builders;
using YesSql.Filters.Abstractions.Nodes;
using YesSql.Filters.Abstractions.Services;
using YesSql.Filters.Enumerable.Services;
using YesSql;
using System.Collections.Generic;

namespace YesSql.Filters.Enumerable
{

public class EnumerableUnaryEngineBuilder<T> : UnaryEngineBuilder<T, EnumerableTermOption<T>> where T : class
{
public EnumerableUnaryEngineBuilder(string name, Func<string, IEnumerable<T>, EnumerableExecutionContext<T>, ValueTask<IEnumerable<T>>> query) : base(new EnumerableTermOption<T>(name, query))
{
}

/// <summary>
/// Adds a mapping function which can be applied to a model.
/// <typeparam name="TModel">The type of model.</typeparam>
/// </summary>
public EnumerableUnaryEngineBuilder<T> MapTo<TModel>(Action<string, TModel> map)
{
_termOption.MapTo = map;

return this;
}

/// <summary>
/// Adds a mapping function where terms can be mapped from a model.
/// <typeparam name="TModel">The type of model.</typeparam>
/// <param name="map">Mapping to apply</param>
/// </summary>
public EnumerableUnaryEngineBuilder<T> MapFrom<TModel>(Func<TModel, (bool, string)> map)
{
static TermNode factory(string name, string value) => new NamedTermNode(name, new UnaryNode(value, OperateNodeQuotes.None));

return MapFrom(map, factory);
}

/// <summary>
/// Adds a mapping function where terms can be mapped from a model.
/// <typeparam name="TModel">The type of model.</typeparam>
/// <param name="map">Mapping to apply</param>
/// <param name="factory">Factory to create a <see cref="TermNode" /> when adding a mapping</param>
/// </summary>
public EnumerableUnaryEngineBuilder<T> MapFrom<TModel>(Func<TModel, (bool, string)> map, Func<string, string, TermNode> factory)
{
Action<EnumerableFilterResult<T>, string, TermOption, TModel> mapFrom = (EnumerableFilterResult<T> terms, string name, TermOption termOption, TModel model) =>
{
(var shouldMap, var value) = map(model);
if (shouldMap)
{
var node = termOption.MapFromFactory(name, value);
terms.TryAddOrReplace(node);
}
else
{
terms.TryRemove(name);
}
};

_termOption.MapFrom = mapFrom;
_termOption.MapFromFactory = factory;

return this;
}
}
}
12 changes: 12 additions & 0 deletions src/YesSql.Filters.Enumerable/IEnumerableParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using YesSql.Filters.Abstractions.Services;

namespace YesSql.Filters.Enumerable
{
/// <summary>
/// Represents a filter parser for an <see cref="IEnumerable{T}"/>
/// </summary>
public interface IEnumerableParser<T> : IFilterParser<EnumerableFilterResult<T>> where T : class
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using YesSql.Filters.Abstractions.Services;

namespace YesSql.Filters.Enumerable.Services
{
public class EnumerableExecutionContext<T> : FilterExecutionContext<IEnumerable<T>> where T : class
{
public EnumerableExecutionContext(IEnumerable<T> query) : base(query)
{
}

public EnumerableTermOption<T> CurrentTermOption { get; set; }
}
}
Loading