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

FIX: Negation filtering #45

Merged
merged 4 commits into from
May 26, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static IQueryable<T> ApplyFilter<T>(this IQueryable<T> source,
//generate the expression equivalent to that querystring with the mapping corresponding to the DB
var predicateKey = PredicateBuilder.New<T>(false); //Declare a PredicateBuilder for the current key values
predicateKey = values.Where(value => ValidateDataType(value, GetBodyExpression(filterMapLower[key]).Type))
.Select(value => GetOperationExpression(key, filterMapLower[key], value)) //then generate the expresion for each value
.Select(value => GetOperationExpression(key, filterMapLower[key], value)) //then generate the expression for each value
.Aggregate(predicateKey, (current, expr) => current.Or(expr)); //and chain them all with an OR operator
predicate = allConditions ? predicate.And(predicateKey) : predicate.Or(predicateKey); //then add the resulting expression to the more general predicate
}
Expand Down Expand Up @@ -153,19 +153,22 @@ private static Expression<Func<T, bool>> GetOperationExpression<T>(string fieldK
return Expression.Lambda<Func<T, bool>>(expression, fieldMap.Parameters);
}

private static bool ValidateDataType(string? data, Type type) =>
data switch
{
null or { Length: 0 } => true,
not null when type == typeof(int) || type == typeof(int?) => int.TryParse(data, out _),
not null when type == typeof(long) || type == typeof(long?) => long.TryParse(data, out _),
not null when type == typeof(short) || type == typeof(short?) => short.TryParse(data, out _),
not null when type == typeof(float) || type == typeof(float?) => float.TryParse(data, out _),
not null when type == typeof(decimal) || type == typeof(decimal?) => decimal.TryParse(data, out _),
not null when type == typeof(bool) || type == typeof(bool?) => bool.TryParse(data, out _),
not null when type == typeof(Guid) || type == typeof(Guid?) => Guid.TryParse(data, out _),
not null when type == typeof(DateTime) || type == typeof(DateTime?) => DateTime.TryParse(data, out _),
not null when type == typeof(Enum) => Enum.TryParse(type, data, true, out _),
_ => type == typeof(string)
};
private static bool ValidateDataType(string? data, Type type)
{
data = data is ['!', ..] ? data[1..] : data;
return data switch
{
null or { Length: 0 } => true,
not null when type == typeof(int) || type == typeof(int?) => int.TryParse(data, out _),
not null when type == typeof(long) || type == typeof(long?) => long.TryParse(data, out _),
not null when type == typeof(short) || type == typeof(short?) => short.TryParse(data, out _),
not null when type == typeof(float) || type == typeof(float?) => float.TryParse(data, out _),
not null when type == typeof(decimal) || type == typeof(decimal?) => decimal.TryParse(data, out _),
not null when type == typeof(bool) || type == typeof(bool?) => bool.TryParse(data, out _),
not null when type == typeof(Guid) || type == typeof(Guid?) => Guid.TryParse(data, out _),
not null when type == typeof(DateTime) || type == typeof(DateTime?) => DateTime.TryParse(data, out _),
not null when type == typeof(Enum) => Enum.TryParse(type, data, true, out _),
_ => type == typeof(string)
};
}
}
2 changes: 1 addition & 1 deletion src/Monaco.Template.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>Monaco.Template</id>
<version>2.0.3</version>
<version>2.0.4</version>
<title>Monaco Template</title>
<description>
Templates for .NET projects
Expand Down