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

Add support to change the current CulutureInfo #187

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 3 additions & 1 deletion Sieve/Models/SieveOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Sieve.Models
namespace Sieve.Models
{
public class SieveOptions
{
Expand All @@ -13,5 +13,7 @@ public class SieveOptions
public bool IgnoreNullsOnNotEqual { get; set; } = true;

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

public string CultureNameOfTypeConversion { get; set; } = "en";
}
}
10 changes: 6 additions & 4 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 @@ -172,6 +173,8 @@ protected virtual IQueryable<TEntity> ApplyFiltering<TEntity>(TSieveModel model,
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 All @@ -198,7 +201,7 @@ protected virtual IQueryable<TEntity> ApplyFiltering<TEntity>(TSieveModel model,

var filterValue = isFilterTermValueNull
? Expression.Constant(null, property.PropertyType)
: ConvertStringValueToConstantExpression(filterTermValue, property, converter);
: ConvertStringValueToConstantExpression(filterTermValue, property, converter, cultureInfoToUseForTypeConversion);

if (filterTerm.OperatorIsCaseInsensitive && !isFilterTermValueNull)
{
Expand Down Expand Up @@ -309,15 +312,14 @@ private static Expression GenerateFilterNullCheckExpression(Expression propertyV
Expression.NotEqual(propertyValue, Expression.Default(propertyValue.Type)));
}

private static Expression ConvertStringValueToConstantExpression(string value, PropertyInfo property,
TypeConverter converter)
private static Expression ConvertStringValueToConstantExpression(string value, PropertyInfo property, TypeConverter converter, CultureInfo cultureInfo)
{
// 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)
? converter.ConvertFrom(null, cultureInfo, value)
: Convert.ChangeType(value, property.PropertyType);

return GetClosureOverConstant(constantVal, property.PropertyType);
Expand Down
2 changes: 2 additions & 0 deletions SieveUnitTests/Abstractions/Entity/IPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface IPost: IBaseEntity
string Title { get; set; }
[Sieve(CanFilter = true, CanSort = true)]
int LikeCount { get; set; }
[Sieve(CanFilter = true)]
float MeanRating { get; set; }
[Sieve(CanFilter = true, CanSort = true)]
int CommentCount { get; set; }
[Sieve(CanFilter = true, CanSort = true)]
Expand Down
3 changes: 3 additions & 0 deletions SieveUnitTests/Entities/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class Post : BaseEntity, IPost

[Sieve(CanFilter = true, CanSort = true)]
public int LikeCount { get; set; } = new Random().Next(0, 1000);

[Sieve(CanFilter = true)]
public float MeanRating { get; set; }

[Sieve(CanFilter = true, CanSort = true)]
public int CommentCount { get; set; } = new Random().Next(0, 1000);
Expand Down
76 changes: 76 additions & 0 deletions SieveUnitTests/General.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public General(ITestOutputHelper testOutputHelper)
Id = 0,
Title = "A",
LikeCount = 100,
MeanRating = 3.5f,
IsDraft = true,
CategoryId = null,
TopComment = new Comment { Id = 0, Text = "A1" },
Expand All @@ -50,6 +51,7 @@ public General(ITestOutputHelper testOutputHelper)
Id = 1,
Title = "B",
LikeCount = 50,
MeanRating = 3.5f,
IsDraft = false,
CategoryId = 1,
TopComment = new Comment { Id = 3, Text = "B1" },
Expand All @@ -60,6 +62,7 @@ public General(ITestOutputHelper testOutputHelper)
Id = 2,
Title = "C",
LikeCount = 0,
MeanRating = 3.5f,
CategoryId = 1,
TopComment = new Comment { Id = 2, Text = "C1" },
FeaturedComment = new Comment { Id = 6, Text = "C2" }
Expand All @@ -69,6 +72,7 @@ public General(ITestOutputHelper testOutputHelper)
Id = 3,
Title = "D",
LikeCount = 3,
MeanRating = 3.5f,
IsDraft = true,
CategoryId = 2,
TopComment = new Comment { Id = 1, Text = "D1" },
Expand All @@ -79,6 +83,7 @@ public General(ITestOutputHelper testOutputHelper)
Id = 4,
Title = "Yen",
LikeCount = 5,
MeanRating = 3.5f,
IsDraft = true,
CategoryId = 5,
TopComment = new Comment { Id = 4, Text = "Yen3" },
Expand Down Expand Up @@ -848,5 +853,76 @@ public void CanFilterWithEscapedOperators(string filter)
Assert.Equal(1, resultCount);
}

[Theory]
[InlineData("en-US", "1.2")]
[InlineData("de-DE", @"1\,2")]
public void ParseFloatsWithChangedCultureInfo_Works(string culture, string value)
{
// ARRANGE
var posts = new List<Post>
{
new Post
{
Id = 1,
Title = "E\\",
LikeCount = 4,
MeanRating = 1.2f,
IsDraft = true,
CategoryId = 1,
TopComment = new Comment { Id = 1, Text = "E1" },
FeaturedComment = new Comment { Id = 7, Text = "E2" }
}
}.AsQueryable();

var optionsAccessor = new SieveOptionsAccessor();
optionsAccessor.Value.CultureNameOfTypeConversion = culture;

var processor = new ApplicationSieveProcessor(optionsAccessor,
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());

var model = new SieveModel() { Filters = $"MeanRating=={value}" };

// ACT
var result = processor.Apply(model, posts);

// ASSERT
Assert.NotNull(result);
}

[Theory]
[InlineData("en", @"1\,2")]
[InlineData("de", "1.2")]
public void ParseFloatsWithChangedCultureInfo_Fails(string culture, string value)
{
// ARRANGE
var posts = new List<Post>
{
new Post
{
Id = 1,
Title = "E\\",
LikeCount = 4,
MeanRating = 1.9f,
IsDraft = true,
CategoryId = 1,
TopComment = new Comment { Id = 1, Text = "E1" },
FeaturedComment = new Comment { Id = 7, Text = "E2" }
}
}.AsQueryable();

var optionsAccessor = new SieveOptionsAccessor();
optionsAccessor.Value.CultureNameOfTypeConversion = culture;

var processor = new ApplicationSieveProcessor(optionsAccessor,
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());

var model = new SieveModel() { Filters = $"MeanRating=={value}" };

// ACT, ASSERT
Assert.Throws<SieveException>(() => processor.Apply(model, posts));
}

}
}