Skip to content

Commit

Permalink
feat: update API (mutualize some gPRC messages)
Browse files Browse the repository at this point in the history
  • Loading branch information
aneojgurhem committed Jul 26, 2023
1 parent 4e4bd77 commit 3c840b2
Show file tree
Hide file tree
Showing 26 changed files with 638 additions and 1,337 deletions.
2 changes: 1 addition & 1 deletion Common/src/ArmoniK.Core.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


<ItemGroup>
<PackageReference Include="ArmoniK.Api.Core" Version="3.11.0-featfilters.106.e78be07" />
<PackageReference Include="ArmoniK.Api.Core" Version="3.11.0-featfilters.108.7af3868" />
<PackageReference Include="Calzolari.Grpc.AspNetCore.Validation" Version="6.3.0" />
<PackageReference Include="Grpc.HealthCheck" Version="2.54.0" />
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" />
Expand Down
109 changes: 77 additions & 32 deletions Common/src/gRPC/FilterRangeExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,62 +30,107 @@ namespace ArmoniK.Core.Common.gRPC;
public static class FilterRangeExt
{
/// <summary>
/// Convert gRPC field to <see cref="ExpressionType" /> that represent which operation has to be performed
/// Generate a filter <see cref="Expression" /> for operations on strings
/// </summary>
/// <param name="filterOperator">The gRPC Enum</param>
/// <typeparam name="T">Type of the value and field on which the operation is applied</typeparam>
/// <typeparam name="TStatus">Status enum type used for the comparison</typeparam>
/// <param name="filterOperator">The gRPC enum that selects the operation</param>
/// <param name="field">The <see cref="Expression" /> to select the field on which to apply the operation</param>
/// <param name="value">Value for the operation</param>
/// <returns>
/// The <see cref="ExpressionType" /> that represent the operation
/// The <see cref="Expression" /> that represents the operation on the field with the given value
/// </returns>
public static ExpressionType ToExpressionType(this FilterStatusOperator filterOperator)
public static Expression<Func<T, bool>> ToFilter<T, TStatus>(this FilterStatusOperator filterOperator,
Expression<Func<T, object?>> field,
TStatus value)
=> filterOperator switch
{
FilterStatusOperator.Unspecified => throw new ArgumentOutOfRangeException(),
FilterStatusOperator.Equal => ExpressionType.Equal,
FilterStatusOperator.NotEqual => ExpressionType.NotEqual,
_ => throw new ArgumentOutOfRangeException(),
FilterStatusOperator.Equal => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.Equal),
FilterStatusOperator.NotEqual => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.NotEqual),
_ => throw new ArgumentOutOfRangeException(),
};


/// <summary>
/// Convert gRPC field to <see cref="ExpressionType" /> that represent which operation has to be performed
/// Generate a filter <see cref="Expression" /> for operations on strings
/// </summary>
/// <param name="filterOperator">The gRPC Enum</param>
/// <typeparam name="T">Type of the value and field on which the operation is applied</typeparam>
/// <param name="filterOperator">The gRPC enum that selects the operation</param>
/// <param name="field">The <see cref="Expression" /> to select the field on which to apply the operation</param>
/// <param name="value">Value for the operation</param>
/// <returns>
/// The <see cref="ExpressionType" /> that represent the operation
/// The <see cref="Expression" /> that represents the operation on the field with the given value
/// </returns>
public static ExpressionType ToExpressionType(this FilterDateOperator filterOperator)
public static Expression<Func<T, bool>> ToFilter<T>(this FilterDateOperator filterOperator,
Expression<Func<T, object?>> field,
DateTime? value)
=> filterOperator switch
{
FilterDateOperator.Before => ExpressionType.LessThan,
FilterDateOperator.BeforeOrEqual => ExpressionType.LessThanOrEqual,
FilterDateOperator.Equal => ExpressionType.Equal,
FilterDateOperator.NotEqual => ExpressionType.NotEqual,
FilterDateOperator.AfterOrEqual => ExpressionType.GreaterThanOrEqual,
FilterDateOperator.After => ExpressionType.GreaterThan,
FilterDateOperator.Unspecified => throw new ArgumentOutOfRangeException(),
_ => throw new ArgumentOutOfRangeException(),
FilterDateOperator.Unspecified => throw new ArgumentOutOfRangeException(),
FilterDateOperator.Before => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.LessThan),
FilterDateOperator.BeforeOrEqual => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.LessThanOrEqual),
FilterDateOperator.Equal => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.Equal),
FilterDateOperator.AfterOrEqual => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.GreaterThanOrEqual),
FilterDateOperator.After => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.GreaterThan),
FilterDateOperator.NotEqual => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.NotEqual),
_ => throw new ArgumentOutOfRangeException(),
};


/// <summary>
/// Convert gRPC field to <see cref="ExpressionType" /> that represent which operation has to be performed
/// Generate a filter <see cref="Expression" /> for operations on strings
/// </summary>
/// <param name="filterOperator">The gRPC Enum</param>
/// <typeparam name="T">Type of the value and field on which the operation is applied</typeparam>
/// <param name="filterOperator">The gRPC enum that selects the operation</param>
/// <param name="field">The <see cref="Expression" /> to select the field on which to apply the operation</param>
/// <param name="value">Value for the operation</param>
/// <returns>
/// The <see cref="ExpressionType" /> that represent the operation
/// The <see cref="Expression" /> that represents the operation on the field with the given value
/// </returns>
public static ExpressionType ToExpressionType(this FilterNumberOperator filterOperator)
public static Expression<Func<T, bool>> ToFilter<T>(this FilterNumberOperator filterOperator,
Expression<Func<T, object?>> field,
long value)
=> filterOperator switch
{
FilterNumberOperator.LessThan => ExpressionType.LessThan,
FilterNumberOperator.LessThanOrEqual => ExpressionType.LessThanOrEqual,
FilterNumberOperator.Equal => ExpressionType.Equal,
FilterNumberOperator.NotEqual => ExpressionType.NotEqual,
FilterNumberOperator.GreaterThanOrEqual => ExpressionType.GreaterThanOrEqual,
FilterNumberOperator.GreaterThan => ExpressionType.GreaterThan,
FilterNumberOperator.Unspecified => throw new ArgumentOutOfRangeException(),
_ => throw new ArgumentOutOfRangeException(),
FilterNumberOperator.Unspecified => throw new ArgumentOutOfRangeException(),
FilterNumberOperator.LessThan => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.LessThan),
FilterNumberOperator.LessThanOrEqual => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.LessThanOrEqual),
FilterNumberOperator.Equal => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.Equal),
FilterNumberOperator.NotEqual => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.NotEqual),
FilterNumberOperator.GreaterThanOrEqual => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.GreaterThanOrEqual),
FilterNumberOperator.GreaterThan => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.GreaterThan),
_ => throw new ArgumentOutOfRangeException(),
};


/// <summary>
/// Generate a filter <see cref="Expression" /> for operations on strings
/// </summary>
Expand Down
110 changes: 5 additions & 105 deletions Common/src/gRPC/ListApplicationsRequestExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,95 +57,6 @@ public static class ListApplicationsRequestExt
}
}

/// <summary>
/// Converts gRPC message filter into an <see cref="Expression" /> that represents the filter condition
/// </summary>
/// <param name="filter">The gPRC filter</param>
/// <returns>
/// The <see cref="Expression" /> that represents the filter condition
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">the given message is not recognized</exception>
public static Expression<Func<TaskData, bool>> ToExpression(this FilterString filter)
=> filter.Field.FieldCase switch
{
ApplicationField.FieldOneofCase.None => throw new ArgumentOutOfRangeException(),
ApplicationField.FieldOneofCase.ApplicationField_ => filter.Operator.ToFilter(filter.Field.ApplicationField_.Field.ToField(),
filter.Value),
_ => throw new ArgumentOutOfRangeException(),
};

/// <summary>
/// Converts gRPC message filter into an <see cref="Expression" /> that represents the filter condition
/// </summary>
/// <param name="filter">The gPRC filter</param>
/// <returns>
/// The <see cref="Expression" /> that represents the filter condition
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">the given message is not recognized</exception>
public static Expression<Func<TaskData, bool>> ToExpression(this FilterNumber filter)
=> filter.Field.FieldCase switch
{
ApplicationField.FieldOneofCase.None => throw new ArgumentOutOfRangeException(),
ApplicationField.FieldOneofCase.ApplicationField_ => ExpressionBuilders.MakeBinary(filter.Field.ApplicationField_.Field.ToField(),
filter.Value,
filter.Operator.ToExpressionType()),
_ => throw new ArgumentOutOfRangeException(),
};

/// <summary>
/// Converts gRPC message filter into an <see cref="Expression" /> that represents the filter condition
/// </summary>
/// <param name="filter">The gPRC filter</param>
/// <returns>
/// The <see cref="Expression" /> that represents the filter condition
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">the given message is not recognized</exception>
public static Expression<Func<TaskData, bool>> ToExpression(this FilterDate filter)
=> filter.Field.FieldCase switch
{
ApplicationField.FieldOneofCase.None => throw new ArgumentOutOfRangeException(),
ApplicationField.FieldOneofCase.ApplicationField_ => ExpressionBuilders.MakeBinary(filter.Field.ApplicationField_.Field.ToField(),
filter.Value.ToDateTime(),
filter.Operator.ToExpressionType()),
_ => throw new ArgumentOutOfRangeException(),
};

/// <summary>
/// Converts gRPC message filter into an <see cref="Expression" /> that represents the filter condition
/// </summary>
/// <param name="filter">The gPRC filter</param>
/// <returns>
/// The <see cref="Expression" /> that represents the filter condition
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">the given message is not recognized</exception>
public static Expression<Func<TaskData, bool>> ToExpression(this FilterBoolean filter)
=> filter.Field.FieldCase switch
{
ApplicationField.FieldOneofCase.None => throw new ArgumentOutOfRangeException(),
ApplicationField.FieldOneofCase.ApplicationField_ => ExpressionBuilders.MakeBinary(filter.Field.ApplicationField_.Field.ToField(),
filter.Value,
ExpressionType.Equal),
_ => throw new ArgumentOutOfRangeException(),
};

/// <summary>
/// Converts gRPC message filter into an <see cref="Expression" /> that represents the filter condition
/// </summary>
/// <param name="filter">The gPRC filter</param>
/// <returns>
/// The <see cref="Expression" /> that represents the filter condition
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">the given message is not recognized</exception>
public static Expression<Func<TaskData, bool>> ToExpression(this FilterArray filter)
=> filter.Field.FieldCase switch
{
ApplicationField.FieldOneofCase.None => throw new ArgumentOutOfRangeException(),
ApplicationField.FieldOneofCase.ApplicationField_ => filter.Operator.ToFilter(filter.Field.ApplicationField_.Field.ToField(),
filter.Value),
_ => throw new ArgumentOutOfRangeException(),
};


/// <summary>
/// Converts gRPC message filters into an <see cref="Expression" /> that represents the filter conditions
/// </summary>
Expand All @@ -168,24 +79,13 @@ public static Expression<Func<TaskData, bool>> ToApplicationFilter(this Filters
var predicateAnd = PredicateBuilder.New<TaskData>(data => true);
foreach (var filterField in filtersAnd.And)
{
switch (filterField.FilterCase)
switch (filterField.ValueConditionCase)
{
case FilterField.FilterOneofCase.String:
predicateAnd = predicateAnd.And(filterField.String.ToExpression());
break;
case FilterField.FilterOneofCase.Number:
predicateAnd = predicateAnd.And(filterField.Number.ToExpression());
break;
case FilterField.FilterOneofCase.Date:
predicateAnd = predicateAnd.And(filterField.Date.ToExpression());
break;
case FilterField.FilterOneofCase.Boolean:
predicateAnd = predicateAnd.And(filterField.Boolean.ToExpression());
break;
case FilterField.FilterOneofCase.Array:
predicateAnd = predicateAnd.And(filterField.Array.ToExpression());
case FilterField.ValueConditionOneofCase.FilterString:
predicateAnd = predicateAnd.And(filterField.FilterString.Operator.ToFilter(filterField.Field.ApplicationField_.Field.ToField(),
filterField.FilterString.Value));
break;
case FilterField.FilterOneofCase.None:
case FilterField.ValueConditionOneofCase.None:
default:
throw new ArgumentOutOfRangeException();
}
Expand Down
Loading

0 comments on commit 3c840b2

Please sign in to comment.