Allowing lists to use :eq instead of :ctn

This commit is contained in:
Zoe Roux 2020-09-27 03:39:25 +02:00
parent faa908de7b
commit e2cbbfcd17
4 changed files with 29 additions and 13 deletions

View File

@ -78,8 +78,9 @@ namespace Kyoo
{ {
Type type = typeof(T); Type type = typeof(T);
IEnumerable<PropertyInfo> properties = type.GetProperties() IEnumerable<PropertyInfo> properties = type.GetProperties()
.Where(x => x.CanRead && x.CanWrite .Where(x => x.CanRead
&& Attribute.GetCustomAttribute(x, typeof(NotMergableAttribute)) == null); && x.CanWrite
&& Attribute.GetCustomAttribute(x, typeof(NotMergableAttribute)) == null);
foreach (PropertyInfo property in properties) foreach (PropertyInfo property in properties)
{ {
@ -101,8 +102,9 @@ namespace Kyoo
Type type = typeof(T); Type type = typeof(T);
IEnumerable<PropertyInfo> properties = type.GetProperties() IEnumerable<PropertyInfo> properties = type.GetProperties()
.Where(x => x.CanRead && x.CanWrite .Where(x => x.CanRead
&& Attribute.GetCustomAttribute(x, typeof(NotMergableAttribute)) == null); && x.CanWrite
&& Attribute.GetCustomAttribute(x, typeof(NotMergableAttribute)) == null);
foreach (PropertyInfo property in properties) foreach (PropertyInfo property in properties)
{ {
@ -129,8 +131,9 @@ namespace Kyoo
Type type = typeof(T); Type type = typeof(T);
IEnumerable<PropertyInfo> properties = type.GetProperties() IEnumerable<PropertyInfo> properties = type.GetProperties()
.Where(x => x.CanRead && x.CanWrite .Where(x => x.CanRead
&& Attribute.GetCustomAttribute(x, typeof(NotMergableAttribute)) == null); && x.CanWrite
&& Attribute.GetCustomAttribute(x, typeof(NotMergableAttribute)) == null);
foreach (PropertyInfo property in properties) foreach (PropertyInfo property in properties)
{ {

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
@ -51,27 +52,40 @@ namespace Kyoo.CommonApi
MemberExpression propertyExpr = Expression.Property(param, property); MemberExpression propertyExpr = Expression.Property(param, property);
ConstantExpression valueExpr = null; ConstantExpression valueExpr = null;
if (operand != "ctn" && !typeof(IResource).IsAssignableFrom(propertyExpr.Type)) bool isList = typeof(IEnumerable).IsAssignableFrom(propertyExpr.Type);
if (operand != "ctn" && !typeof(IResource).IsAssignableFrom(propertyExpr.Type) && !isList)
{ {
Type propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; Type propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
object val = string.IsNullOrEmpty(value) || value.Equals("null", StringComparison.OrdinalIgnoreCase) object val;
try
{
val = string.IsNullOrEmpty(value) || value.Equals("null", StringComparison.OrdinalIgnoreCase)
? null ? null
: Convert.ChangeType(value, propertyType); : Convert.ChangeType(value, propertyType);
}
catch (InvalidCastException)
{
throw new ArgumentException("Comparing two differents value's type.");
}
valueExpr = Expression.Constant(val, property.PropertyType); valueExpr = Expression.Constant(val, property.PropertyType);
} }
Expression condition = operand switch Expression condition = operand switch
{ {
"eq" when isList => ContainsResourceExpression(propertyExpr, value),
"ctn" => ContainsResourceExpression(propertyExpr, value),
"eq" when valueExpr == null => ResourceEqual(propertyExpr, value), "eq" when valueExpr == null => ResourceEqual(propertyExpr, value),
"not" when valueExpr == null => ResourceEqual(propertyExpr, value, true), "not" when valueExpr == null => ResourceEqual(propertyExpr, value, true),
"eq" => Expression.Equal(propertyExpr, valueExpr), "eq" => Expression.Equal(propertyExpr, valueExpr),
"not" => Expression.NotEqual(propertyExpr, valueExpr!), "not" => Expression.NotEqual(propertyExpr, valueExpr!),
"lt" => StringCompatibleExpression(Expression.LessThan, propertyExpr, valueExpr), "lt" => StringCompatibleExpression(Expression.LessThan, propertyExpr, valueExpr),
"lte" => StringCompatibleExpression(Expression.LessThanOrEqual, propertyExpr, valueExpr), "lte" => StringCompatibleExpression(Expression.LessThanOrEqual, propertyExpr, valueExpr),
"gt" => StringCompatibleExpression(Expression.GreaterThan, propertyExpr, valueExpr), "gt" => StringCompatibleExpression(Expression.GreaterThan, propertyExpr, valueExpr),
"gte" => StringCompatibleExpression(Expression.GreaterThanOrEqual, propertyExpr, valueExpr), "gte" => StringCompatibleExpression(Expression.GreaterThanOrEqual, propertyExpr, valueExpr),
"ctn" => ContainsResourceExpression(propertyExpr, value), _ => throw new ArgumentException($"Invalid operand: {operand}")
_ => throw new ArgumentException($"Invalid operand: {operand}")
}; };
if (expression != null) if (expression != null)

View File

@ -4,7 +4,6 @@ using System.Threading.Tasks;
using Kyoo.CommonApi; using Kyoo.CommonApi;
using Kyoo.Controllers; using Kyoo.Controllers;
using Kyoo.Models; using Kyoo.Models;
using Kyoo.Models.Exceptions;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Linq; using System.Linq;

@ -1 +1 @@
Subproject commit 3216846879e75fdc1b9ec4efc1da7599ee6e9e2c Subproject commit 7a9808719933a092e56fc26be8f5c40bf7894e7c