Skip to content

Commit

Permalink
Fix merge conflicts for: Let you choose the Culture used for TypeConv…
Browse files Browse the repository at this point in the history
…ersion in the processor Biarity#79
  • Loading branch information
Yannick De Paepe committed Apr 27, 2021
1 parent 18eedf2 commit 940e057
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Sieve/Models/SieveOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class SieveOptions
public int MaxPageSize { get; set; } = 0;

public bool ThrowExceptions { get; set; } = false;

public string CultureNameOfTypeConversion { get; set; } = "en";
}
}
}
9 changes: 6 additions & 3 deletions Sieve/Services/SieveProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -171,6 +172,8 @@ private IQueryable<TEntity> ApplyFiltering<TEntity>(
return result;
}

var cultureInfoToUseForTypeConversion = new CultureInfo(_options.Value.CultureNameOfTypeConversion ?? "en");

Expression outerExpression = null;
var parameter = Expression.Parameter(typeof(TEntity), "e");
foreach (var filterTerm in model.GetFiltersParsed())
Expand Down Expand Up @@ -202,7 +205,7 @@ private IQueryable<TEntity> ApplyFiltering<TEntity>(
var isFilterTermValueNull = filterTermValue.ToLower() == nullFilterValue;
var filterValue = isFilterTermValueNull
? Expression.Constant(null, property.PropertyType)
: ConvertStringValueToConstantExpression(filterTermValue, property, converter);
: ConvertStringValueToConstantExpression(filterTermValue, property, converter, cultureInfoToUseForTypeConversion);

if (filterTerm.OperatorIsCaseInsensitive)
{
Expand Down Expand Up @@ -275,10 +278,10 @@ private static Expression GenerateFilterNullCheckExpression(Expression propertyV
: Expression.AndAlso(nullCheckExpression, Expression.NotEqual(propertyValue, Expression.Default(propertyValue.Type)));
}

private Expression ConvertStringValueToConstantExpression(string value, PropertyInfo property, TypeConverter converter)
private Expression ConvertStringValueToConstantExpression(string value, PropertyInfo property, TypeConverter converter, CultureInfo cultureInfo)
{
dynamic constantVal = converter.CanConvertFrom(typeof(string))
? converter.ConvertFrom(value)
? converter.ConvertFrom(null, cultureInfo, value)
: Convert.ChangeType(value, property.PropertyType);

return GetClosureOverConstant(constantVal, property.PropertyType);
Expand Down

0 comments on commit 940e057

Please sign in to comment.