Skip to content

Commit

Permalink
Cleanup Warning in filtering (ChilliCream#4551)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Dec 14, 2021
1 parent 1763df6 commit ba77331
Show file tree
Hide file tree
Showing 60 changed files with 354 additions and 371 deletions.
12 changes: 12 additions & 0 deletions src/HotChocolate/Data/src/Data/DataResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/HotChocolate/Data/src/Data/DataResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,10 @@
<data name="ProjectionVisitor_CouldNotUnwrapType" xml:space="preserve">
<value>Could not unwrap runtime type of type {0}.</value>
</data>
<data name="GlobalIdInputValueFormatter_SpecifiedValueIsNotAValidId" xml:space="preserve">
<value>The specified value is not a valid ID value.</value>
</data>
<data name="GlobalIdInputValueFormatter_IdsHaveInvalidFormat" xml:space="preserve">
<value>The IDs `{0}` have an invalid format.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using HotChocolate.Data.Sorting;
using HotChocolate.Internal;
using HotChocolate.Types.Descriptors.Definitions;
using HotChocolate.Utilities;

namespace HotChocolate.Data.Filters;

Expand Down Expand Up @@ -165,7 +166,7 @@ private static void MergeFilterFields(
else
{
typeField = typeFields.FirstOrDefault(
t => t.Name.Equals(extensionField.Name));
t => t.Name.Value.EqualsOrdinal(extensionField.Name));
}

if (typeField is null)
Expand Down Expand Up @@ -204,7 +205,7 @@ private static void MergeSortEnumValues(
else
{
typeField = typeFields.FirstOrDefault(
t => t.Name.Equals(extensionField.Name));
t => t.Name.Value.EqualsOrdinal(extensionField.Name));
}

if (typeField is null)
Expand Down Expand Up @@ -235,7 +236,7 @@ private static void MergeSortFields(
foreach (InputFieldDefinition? extensionField in extensionFields)
{
InputFieldDefinition? typeField = typeFields.FirstOrDefault(
t => t.Name.Equals(extensionField.Name));
t => t.Name.Value.EqualsOrdinal(extensionField.Name));

if (typeField is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override void OnBeforeRegisterDependencies(

var descriptor = FilterInputTypeDescriptor.New(
discoveryContext.DescriptorContext,
def.EntityType,
def.EntityType!,
def.Scope);

ApplyCorrectScope(def, discoveryContext);
Expand Down Expand Up @@ -74,7 +74,7 @@ public override void OnBeforeCompleteName(

var descriptor = FilterInputTypeDescriptor.New(
completionContext.DescriptorContext,
def.EntityType,
def.EntityType!,
def.Scope);

SchemaTypeReference typeReference = TypeReference.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public static QueryableExecutable<T> Filter<T>(
IResolverContext context) =>
ExecuteFilter(enumerable, context, typeof(QueryableExecutable<T>));

[Obsolete]
private static T ExecuteFilter<T>(
this T input,
IResolverContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,27 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using HotChocolate.Utilities;
using static System.Reflection.BindingFlags;

namespace HotChocolate.Data.Filters.Expressions;

public static class FilterExpressionBuilder
{
#pragma warning disable CA1307
private static readonly MethodInfo _startsWith =
typeof(string).GetMethods().Single(m =>
m.Name.Equals(nameof(string.StartsWith))
&& m.GetParameters().Length == 1
&& m.GetParameters().Single().ParameterType == typeof(string));
ReflectionUtils.ExtractMethod<string>(x => x.StartsWith(default(string)!));

private static readonly MethodInfo _endsWith =
typeof(string).GetMethods().Single(m =>
m.Name.Equals(nameof(string.EndsWith))
&& m.GetParameters().Length == 1
&& m.GetParameters().Single().ParameterType == typeof(string));
ReflectionUtils.ExtractMethod<string>(x => x.EndsWith(default(string)!));

private static readonly MethodInfo _contains =
typeof(string).GetMethods().Single(m =>
m.Name.Equals(nameof(string.Contains))
&& m.GetParameters().Length == 1
&& m.GetParameters().Single().ParameterType == typeof(string));
ReflectionUtils.ExtractMethod<string>(x => x.Contains(default(string)!));
#pragma warning restore CA1307

private static readonly MethodInfo _createAndConvert =
typeof(FilterExpressionBuilder)
.GetMethod(
nameof(FilterExpressionBuilder.CreateAndConvertParameter),
BindingFlags.NonPublic | BindingFlags.Static)!;
.GetMethod(nameof(CreateAndConvertParameter), NonPublic | Static)!;

private static readonly MethodInfo _anyMethod =
typeof(Enumerable)
Expand All @@ -50,122 +43,89 @@ public static class FilterExpressionBuilder
Expression.Constant(null, typeof(object));

public static Expression Not(Expression expression)
{
return Expression.Not(expression);
}
=> Expression.Not(expression);

public static Expression Equals(
Expression property,
object? value)
{
return Expression.Equal(
property,
CreateParameter(value, property.Type));
}
=> Expression.Equal(property, CreateParameter(value, property.Type));

public static Expression NotEquals(
Expression property,
object? value)
{
return Expression.NotEqual(
property,
CreateParameter(value, property.Type));
}
=> Expression.NotEqual(property, CreateParameter(value, property.Type));

public static Expression In(
Expression property,
Type genericType,
object parsedValue)
object? parsedValue)
{
return Expression.Call(
typeof(Enumerable),
nameof(Enumerable.Contains),
new Type[] { genericType },
new Type[]
{
genericType
},
Expression.Constant(parsedValue),
property);
}

public static Expression GreaterThan(
Expression property,
object value)
{
return Expression.GreaterThan(
property,
CreateParameter(value, property.Type));
}
=> Expression.GreaterThan(property, CreateParameter(value, property.Type));

public static Expression GreaterThanOrEqual(
Expression property,
object value)
{
return Expression.GreaterThanOrEqual(
property,
CreateParameter(value, property.Type));
}
=> Expression.GreaterThanOrEqual(property, CreateParameter(value, property.Type));

public static Expression LowerThan(
Expression property,
object value)
{
return Expression.LessThan(
property,
CreateParameter(value, property.Type));
}
=> Expression.LessThan(property, CreateParameter(value, property.Type));

public static Expression LowerThanOrEqual(
Expression property,
object value)
{
return Expression.LessThanOrEqual(
property,
CreateParameter(value, property.Type));
}
=> Expression.LessThanOrEqual(property, CreateParameter(value, property.Type));

public static Expression StartsWith(
Expression property,
object value)
{
return Expression.AndAlso(
=> Expression.AndAlso(
Expression.NotEqual(property, _null),
Expression.Call(
property,
_startsWith,
CreateParameter(value, property.Type)));
}

public static Expression EndsWith(
Expression property,
object value)
{
return Expression.AndAlso(
=> Expression.AndAlso(
Expression.NotEqual(property, _null),
Expression.Call(
property,
_endsWith,
CreateParameter(value, property.Type)));
}

public static Expression Contains(
Expression property,
object value)
{
return Expression.AndAlso(
=> Expression.AndAlso(
Expression.NotEqual(property, _null),
Expression.Call(
property,
_contains,
CreateParameter(value, property.Type)));
}

public static Expression NotNull(Expression expression)
{
return Expression.NotEqual(expression, _null);
}
=> Expression.NotEqual(expression, _null);

public static Expression NotNullAndAlso(Expression property, Expression condition)
{
return Expression.AndAlso(NotNull(property), condition);
}
=> Expression.AndAlso(NotNull(property), condition);

public static Expression Any(
Type type,
Expand All @@ -181,44 +141,49 @@ public static Expression Any(
Type type,
Expression property,
LambdaExpression lambda)
{
return Expression.Call(
=> Expression.Call(
_anyWithParameter.MakeGenericMethod(type),
new Expression[] { property, lambda });
}
new Expression[]
{
property,
lambda
});

public static Expression Any(
Type type,
Expression property)
{
return Expression.Call(
_anyMethod.MakeGenericMethod(type),
new Expression[] { property });
new Expression[]
{
property
});
}

public static Expression All(
Type type,
Expression property,
LambdaExpression lambda)
{
return Expression.Call(
=> Expression.Call(
_allMethod.MakeGenericMethod(type),
new Expression[] { property, lambda });
}
new Expression[]
{
property,
lambda
});

public static Expression NotContains(
Expression property,
object value)
{
return Expression.OrElse(
=> Expression.OrElse(
Expression.Equal(
property,
_null),
Expression.Not(Expression.Call(
property,
_contains,
CreateParameter(value, property.Type))));
}

private static Expression CreateAndConvertParameter<T>(object value)
{
Expand All @@ -227,8 +192,11 @@ private static Expression CreateAndConvertParameter<T>(object value)
}

private static Expression CreateParameter(object? value, Type type)
{
return (Expression)_createAndConvert
.MakeGenericMethod(type).Invoke(null, new[] { value })!;
}
=> (Expression)_createAndConvert
.MakeGenericMethod(type)
.Invoke(null,
new[]
{
value
})!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public override Expression HandleOperation(
object? parsedValue)
{
Expression property = context.GetInstance();

if (parsedValue is null)
{
throw new GraphQLException(ErrorHelper.CreateNonNullError(field, value, context));
}

return FilterExpressionBuilder.Contains(property, parsedValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public override Expression HandleOperation(
object? parsedValue)
{
Expression property = context.GetInstance();

if (parsedValue is null)
{
throw new GraphQLException(ErrorHelper.CreateNonNullError(field, value, context));
}

return FilterExpressionBuilder.EndsWith(property, parsedValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public override Expression HandleOperation(
object? parsedValue)
{
Expression property = context.GetInstance();

if (parsedValue is null)
{
throw new GraphQLException(ErrorHelper.CreateNonNullError(field, value, context));
}

return FilterExpressionBuilder.NotContains(property, parsedValue);
}
}
Loading

0 comments on commit ba77331

Please sign in to comment.