Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
3.1.0
Browse files Browse the repository at this point in the history
Attribute simplification
  • Loading branch information
DefGh committed Oct 27, 2023
1 parent f118b5b commit bb892a0
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 45 deletions.
24 changes: 3 additions & 21 deletions IEnumerableExtenders/Attributes/MappedToPropertyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,15 @@ namespace PandaTech.IEnumerableFilters.Attributes;
[AttributeUsage(AttributeTargets.Property)]
public class MappedToPropertyAttribute : Attribute
{
public Type? TargetConverterType;
public Type? BackwardConverterType;
public Type? ConverterType;
public readonly string TargetPropertyName;
public ComparisonType[]? ComparisonTypes;

public readonly bool Sortable = true;
public bool Encrypted = false;
public bool Sortable = true;

public MappedToPropertyAttribute(string property)
{
TargetPropertyName = property;
}

public ComparisonTypesDefault ComparisonTypesDefault
{
get => throw new Exception("This property is only for serialization");
set
{
ComparisonTypes = value switch
{
ComparisonTypesDefault.Numeric => DefaultComparisonTypes.Numeric,
ComparisonTypesDefault.String => DefaultComparisonTypes.String,
ComparisonTypesDefault.DateTime => DefaultComparisonTypes.DateTime,
ComparisonTypesDefault.Bool => DefaultComparisonTypes.Bool,
ComparisonTypesDefault.Guid => DefaultComparisonTypes.Guid,
ComparisonTypesDefault.Enum => DefaultComparisonTypes.Enum,
_ => throw new ArgumentOutOfRangeException(nameof(value), value, null)
};
}
}
}
67 changes: 51 additions & 16 deletions IEnumerableExtenders/EnumerableExtendersV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,39 @@ namespace PandaTech.IEnumerableFilters;

public static class EnumerableExtendersV3
{
private static ComparisonType[]? ComparisonTypes(ComparisonTypesDefault typesDefault) =>
typesDefault switch
{
ComparisonTypesDefault.Numeric => DefaultComparisonTypes.Numeric,
ComparisonTypesDefault.String => DefaultComparisonTypes.String,
ComparisonTypesDefault.DateTime => DefaultComparisonTypes.DateTime,
ComparisonTypesDefault.Bool => DefaultComparisonTypes.Bool,
ComparisonTypesDefault.Guid => DefaultComparisonTypes.Guid,
ComparisonTypesDefault.Enum => DefaultComparisonTypes.Enum,
ComparisonTypesDefault.ByteArray => DefaultComparisonTypes.ByteArray,
_ => null
};

private static ComparisonTypesDefault GetComparisonTypesDefault(Type type)
{
if (type == typeof(int) || type == typeof(long) || type == typeof(decimal) || type == typeof(double) ||
type == typeof(float))
return ComparisonTypesDefault.Numeric;
if (type == typeof(string))
return ComparisonTypesDefault.String;
if (type == typeof(DateTime))
return ComparisonTypesDefault.DateTime;
if (type == typeof(bool))
return ComparisonTypesDefault.Bool;
if (type == typeof(Guid))
return ComparisonTypesDefault.Guid;
if (type.IsEnum)
return ComparisonTypesDefault.Enum;
if (type == typeof(byte[]))
return ComparisonTypesDefault.ByteArray;
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}

public static IQueryable<TModel> ApplyFilters<TModel, TDto>(this IQueryable<TModel> dbSet, List<FilterDto> filters)
{
var q = dbSet;
Expand Down Expand Up @@ -47,8 +80,8 @@ public static IQueryable<TModel> ApplyFilters<TModel, TDto>(this IQueryable<TMod
throw new PropertyNotFoundException(
$"Property {filter.Attribute.TargetPropertyName} not found in {typeof(TModel).Name}");

var filterType = filter.Attribute.TargetConverterType is not null
? filter.Attribute.TargetConverterType.GenericTypeArguments.First()
var filterType = filter.Attribute.ConverterType is not null
? filter.Attribute.ConverterType.GenericTypeArguments.First()
: filter.Type;

var filterTypeName = filterType.Name;
Expand Down Expand Up @@ -91,7 +124,7 @@ public static IQueryable<TModel> ApplyFilters<TModel, TDto>(this IQueryable<TMod
}

var converter =
Activator.CreateInstance(filter.Attribute.TargetConverterType ?? typeof(DirectConverter));
Activator.CreateInstance(filter.Attribute.ConverterType ?? typeof(DirectConverter));


var finalLambda = FilterLambdaBuilder.BuildLambdaString(new FilterKey
Expand All @@ -101,7 +134,7 @@ public static IQueryable<TModel> ApplyFilters<TModel, TDto>(this IQueryable<TMod
TargetPropertyName = targetProperty.Name
});

var method = converter!.GetType().GetMethods().First(x => x.Name == "Convert");
var method = converter!.GetType().GetMethods().First(x => x.Name == "ConvertTo");

for (var index = 0; index < filterDto.Values.Count; index++)
{
Expand Down Expand Up @@ -212,8 +245,8 @@ public static DistinctColumnValuesResult DistinctColumnValues<T, TDto>(this IQue
query2 = query.Select<object>(filter.Attribute.TargetPropertyName);
}

var converter = Activator.CreateInstance(filter.Attribute.BackwardConverterType ?? typeof(DirectConverter));
var method = converter!.GetType().GetMethods().First(x => x.Name == "Convert");
var converter = Activator.CreateInstance(filter.Attribute.ConverterType ?? typeof(DirectConverter));
var method = converter!.GetType().GetMethods().First(x => x.Name == "ConvertFrom");

IQueryable<object> query3;
try
Expand Down Expand Up @@ -285,8 +318,8 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
query2 = query.Select<object>(filter.Attribute.TargetPropertyName);
}

var converter = Activator.CreateInstance(filter.Attribute.BackwardConverterType ?? typeof(DirectConverter));
var method = converter!.GetType().GetMethods().First(x => x.Name == "Convert");
var converter = Activator.CreateInstance(filter.Attribute.ConverterType ?? typeof(DirectConverter));
var method = converter!.GetType().GetMethods().First(x => x.Name == "ConvertFrom");

IQueryable<object> query3;
try
Expand Down Expand Up @@ -337,7 +370,9 @@ public static List<FilterInfo> GetFilters(Assembly assembly, string tableName)
{
PropertyName = x.Name,
Table = tableName,
ComparisonTypes = (x.GetCustomAttribute<MappedToPropertyAttribute>()!.ComparisonTypes ?? new[]
ComparisonTypes = (x.GetCustomAttribute<MappedToPropertyAttribute>()!.ComparisonTypes ??
ComparisonTypes(GetComparisonTypesDefault(x.PropertyType))
?? new[]
{
ComparisonType.Equal,
ComparisonType.NotEqual,
Expand Down Expand Up @@ -426,7 +461,7 @@ public static List<FilterInfo> GetFilters(Assembly assembly, string tableName)
continue;
}

if (property.PropertyType == typeof(int))
if (targetProperty.PropertyType == typeof(int))
{
var lambda = Lambda<Func<TModel, int>>(propertyAccess, parameter);

Expand Down Expand Up @@ -484,7 +519,7 @@ public static List<FilterInfo> GetFilters(Assembly assembly, string tableName)
continue;
}

if (property.PropertyType == typeof(long))
if (targetProperty.PropertyType == typeof(long))
{
var lambda = Lambda<Func<TModel, long>>(propertyAccess, parameter);

Expand Down Expand Up @@ -542,7 +577,7 @@ public static List<FilterInfo> GetFilters(Assembly assembly, string tableName)
continue;
}

if (property.PropertyType == typeof(DateTime))
if (targetProperty.PropertyType == typeof(DateTime))
{
var lambda = Lambda<Func<TModel, DateTime>>(propertyAccess, parameter);
var res = aggregate.AggregateType switch
Expand Down Expand Up @@ -571,7 +606,7 @@ public static List<FilterInfo> GetFilters(Assembly assembly, string tableName)
continue;
}

if (property.PropertyType == typeof(decimal))
if (targetProperty.PropertyType == typeof(decimal))
{
var lambda = Lambda<Func<TModel, decimal>>(propertyAccess, parameter);

Expand Down Expand Up @@ -630,7 +665,7 @@ public static List<FilterInfo> GetFilters(Assembly assembly, string tableName)
continue;
}

if (property.PropertyType == typeof(double))
if (targetProperty.PropertyType == typeof(double))
{
var lambda = Lambda<Func<TModel, double>>(propertyAccess, parameter);

Expand Down Expand Up @@ -689,12 +724,12 @@ public static List<FilterInfo> GetFilters(Assembly assembly, string tableName)
continue;
}

if (property.PropertyType == typeof(Guid))
if (targetProperty.PropertyType == typeof(Guid))
{
throw new NotImplementedException();
}

if (property.PropertyType.IsClass)
if (targetProperty.PropertyType.IsClass)
{
throw new NotImplementedException();
}
Expand Down
7 changes: 6 additions & 1 deletion IEnumerableExtenders/FilterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ namespace PandaTech.IEnumerableFilters;

public class DirectConverter : IConverter<object, object>
{
public object Convert(object from)
public object ConvertTo(object from)
{
return from;
}

public object ConvertFrom(object to)
{
return to;
}
}

public static class FilterLambdaBuilder
Expand Down
19 changes: 18 additions & 1 deletion IEnumerableExtenders/Helpers/ComparisonTypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,28 @@ public enum ComparisonTypesDefault
DateTime,
Bool,
Guid,
Enum
Enum,
ByteArray
}

public static class DefaultComparisonTypes
{
public static readonly ComparisonType[] Default =
{
ComparisonType.In,
ComparisonType.NotIn,
ComparisonType.Equal,
ComparisonType.NotEqual,
};

public static readonly ComparisonType[] ByteArray =
{
ComparisonType.In,
ComparisonType.NotIn,
ComparisonType.Equal,
ComparisonType.NotEqual,
};

public static readonly ComparisonType[] Numeric =
{
ComparisonType.In,
Expand Down
6 changes: 4 additions & 2 deletions IEnumerableExtenders/IConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace PandaTech.IEnumerableFilters;

public interface IConverter<in TFrom, out TTo>
public interface IConverter<TFrom, TTo>
{
public TTo Convert(TFrom from);
public TTo ConvertTo(TFrom from);

public TFrom ConvertFrom(TTo to);
}
2 changes: 1 addition & 1 deletion IEnumerableExtenders/IEnumerableExtenders.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>PandaTech.IEnumerableFilters</RootNamespace>
<Version>3.0.9</Version>
<Version>3.1.0</Version>
<Authors>PandaTech</Authors>
<Description>This NuGet helps with filtering tables.</Description>
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-ienumerable-extenders-filters.git</RepositoryUrl>
Expand Down
14 changes: 12 additions & 2 deletions TestFilters/Controllers/Models/CatDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class CatDto
{
[JsonConverter(typeof(PandaJsonBaseConverterNotNullable))]
[MappedToProperty(nameof(Cat.Id),
TargetConverterType = typeof(PandaFilterBaseConverter))]
ConverterType = typeof(PandaFilterBaseConverter))]
public long Id { get; set; }

[MappedToProperty(nameof(Cat.Name))]
public string Name { get; set; } = null!;

[MappedToProperty(nameof(Cat.Age), ComparisonTypesDefault = ComparisonTypesDefault.Numeric)]
[MappedToProperty(nameof(Cat.Age))]
public int Age { get; set; }
}

Expand All @@ -27,5 +27,15 @@ public long Convert(string from)
{
return PandaBaseConverter.Base36ToBase10(from)!.Value;
}

public long ConvertTo(string from)
{
return PandaBaseConverter.Base36ToBase10(from)!.Value;
}

public string ConvertFrom(long to)
{
return PandaBaseConverter.Base10ToBase36(to)!;
}
}

2 changes: 1 addition & 1 deletion Tests/CounterpartyDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Tests;
[MappedToClass(typeof(Counterparty))]
public class CounterpartyDto
{
[MappedToProperty(nameof(Counterparty.Id), TargetConverterType = typeof(SomeConverter))]
[MappedToProperty(nameof(Counterparty.Id), ConverterType = typeof(SomeConverter))]
public string Id { get; set; }

[MappedToProperty(nameof(Counterparty.Name))]
Expand Down
10 changes: 10 additions & 0 deletions Tests/SomeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ public int Convert(string from)
{
return int.Parse(from);
}

public int ConvertTo(string from)
{
return int.Parse(from);
}

public string ConvertFrom(int to)
{
return to.ToString();
}
}

0 comments on commit bb892a0

Please sign in to comment.