Allow kind to be used as a filter

This commit is contained in:
Zoe Roux 2023-11-28 16:46:43 +01:00
parent 9609fb150a
commit 29d846a944
2 changed files with 27 additions and 0 deletions

View File

@ -209,6 +209,17 @@ public abstract record Filter<T> : Filter
return property.Then(prop =>
{
Type[] types = typeof(T).GetCustomAttribute<OneOfAttribute>()?.Types ?? new[] { typeof(T) };
if (string.Equals(prop, "kind", StringComparison.OrdinalIgnoreCase))
{
return
from eq in op
from val in types
.Select(x => Parse.IgnoreCase(x.Name).Text())
.Aggregate(null as Parser<string>, (acc, x) => acc == null ? x : Parse.Or(acc, x))
select apply("kind", val);
}
PropertyInfo? propInfo = types
.Select(x => x.GetProperty(prop, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance))
.FirstOrDefault();

View File

@ -39,6 +39,8 @@ public static class DapperHelper
{
private static string _Property(string key, Dictionary<string, Type> config)
{
if (key == "kind")
return "kind";
string[] keys = config
.Where(x => key == "id" || x.Value.GetProperty(key) != null)
.Select(x => $"{x.Key}.{x.Value.GetProperty(key)?.GetCustomAttribute<ColumnAttribute>()?.Name ?? key.ToSnakeCase()}")
@ -125,6 +127,20 @@ public static class DapperHelper
{
FormattableString Format(string key, FormattableString op)
{
if (key == "kind")
{
string cases = string.Join('\n', config
.Skip(1)
.Select(x => $"when {x.Key}.id is not null then '{x.Value.Name.ToLowerInvariant()}'")
);
return $"""
case
{cases:raw}
else '{config.First().Value.Name.ToLowerInvariant():raw}'
end {op}
""";
}
IEnumerable<string> properties = config
.Where(x => key == "id" || x.Value.GetProperty(key) != null)
.Select(x => $"{x.Key}.{x.Value.GetProperty(key)?.GetCustomAttribute<ColumnAttribute>()?.Name ?? key.ToSnakeCase()}");