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

Expose UnaryExpressionHelpers and BinaryExpressionHelpers #290

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
98 changes: 98 additions & 0 deletions src/UiPath.Workflow.Runtime/Expressions/AddAssign.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// This file is part of Core WF which is licensed under the MIT license.
// See LICENSE file in the project root for full license information.

using System.Activities.Validation;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Numerics;

namespace System.Activities.Expressions
{
/// <summary>
/// A code activity which incrementally assigns a numeral.
/// </summary>
/// <example>
/// int myNum = 5;
/// myNum += 5; // 10.
/// </example>
/// <typeparam name="TLeft">The operand to modify.</typeparam>
/// <typeparam name="TRight">The operand with which to modify <typeparamref name="TLeft"/>.</typeparam>
/// <typeparam name="TResult">The resulting type of hte operation.</typeparam>
public sealed class AddAssign<TLeft, TRight, TResult> : CodeActivity<TResult>
#if NET7_0_OR_GREATER
where TLeft : INumber<TLeft>
where TRight : INumber<TRight>
where TResult : INumber<TResult>
#endif
{
private static Func<TLeft, TRight, TResult>? checkedOperationFunction = null;
private static Func<TLeft, TRight, TResult>? uncheckedOperationFunction = null;

/// <summary>
/// Gets or sets the left operand which will be modified by this operation.
/// </summary>
[RequiredArgument]
public InArgument<TLeft> Left { get; set; } = new();

/// <summary>
/// Gets or sets the right operand which will be the modifier for this operation.
/// </summary>
[RequiredArgument]
public InArgument<TRight> Right { get; set; } = new();

/// <summary>
/// Gets or sets a value indicating whether this operation will happen in a checked or unchecked context.
/// </summary>
[DefaultValue(true)]
public bool IsChecked { get; set; } = true;

/// <inheritdoc/>
protected override void CacheMetadata(CodeActivityMetadata metadata)
{
BinaryExpressionHelper.OnGetArguments(metadata, Left, Right);

if (IsChecked)
{
EnsureOperationFunction(metadata, ref checkedOperationFunction, ExpressionType.AddAssignChecked);
}
else
{
EnsureOperationFunction(metadata, ref uncheckedOperationFunction, ExpressionType.AddAssign);
}
}

private static void EnsureOperationFunction
(
CodeActivityMetadata metadata,
ref Func<TLeft, TRight, TResult>? operationFunction,
ExpressionType operatorType
)
{
if (operationFunction == null)
{
if (!BinaryExpressionHelper.TryGenerateLinqDelegate(
operatorType,
out operationFunction,
out ValidationError? validationError))
{
metadata.AddValidationError(validationError);
}
}
}

/// <inheritdoc/>
protected override TResult Execute(CodeActivityContext context)
{
TLeft leftValue = Left.Get(context);
TRight rightValue = Right.Get(context);

// If user changed checked flag between open and execution, an NRE may be thrown.
// This is by design.
// Nullability check silenced because the corresponding value is guaranteed to be
// non-null
return IsChecked
? checkedOperationFunction!(leftValue, rightValue)
: uncheckedOperationFunction!(leftValue, rightValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@

namespace System.Activities.Expressions;

internal static class BinaryExpressionHelper
/// <summary>
/// Helpers for binary expressions.
/// </summary>
public static class BinaryExpressionHelper
{
/// <summary>
/// Binds metadata when getting arguments.
/// </summary>
/// <typeparam name="TLeft">The type of the left argument.</typeparam>
/// <typeparam name="TRight">The type of the right argument.</typeparam>
/// <param name="metadata">The metadata.</param>
/// <param name="left">The left argument.</param>
/// <param name="right">The right argument.</param>
public static void OnGetArguments<TLeft, TRight>(CodeActivityMetadata metadata, InArgument<TLeft> left, InArgument<TRight> right)
{
RuntimeArgument rightArgument = new("Right", typeof(TRight), ArgumentDirection.In, true);
Expand All @@ -26,6 +37,16 @@ public static void OnGetArguments<TLeft, TRight>(CodeActivityMetadata metadata,
});
}

/// <summary>
/// Generates a <see cref="System.Linq"/> delegate.
/// </summary>
/// <typeparam name="TLeft">The type of the left argument.</typeparam>
/// <typeparam name="TRight">The type of the right argument.</typeparam>
/// <typeparam name="TResult">The return type of the operation.</typeparam>
/// <param name="operatorType">The type of expression.</param>
/// <param name="function">The resulting <see cref="Func{T1, T2, TResult}"/>.</param>
/// <param name="validationError">If the operation failed, the error.</param>
/// <returns><see langword="true"/> if the operation was successful; otherwise, <see langword="false"/>.</returns>
public static bool TryGenerateLinqDelegate<TLeft, TRight, TResult>(ExpressionType operatorType, out Func<TLeft, TRight, TResult> function, out ValidationError validationError)
{
function = null;
Expand Down
57 changes: 57 additions & 0 deletions src/UiPath.Workflow.Runtime/Expressions/Decrement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// This file is part of Core WF which is licensed under the MIT license.
// See LICENSE file in the project root for full license information.

using System.Activities.Validation;
using System.Linq.Expressions;
using System.Numerics;

namespace System.Activities.Expressions
{
/// <summary>
/// A code activity which decrements a numeral.
/// </summary>
/// <typeparam name="TNumeral">A numeric value, such as <see langword="int" /> or <see langword="long" />.</typeparam>
public sealed class Decrement<TNumeral> : CodeActivity<TNumeral>
#if NET7_0_OR_GREATER
where TNumeral : IIncrementOperators<TNumeral>
#endif
{
private static Func<TNumeral, TNumeral>? operationFunction = null!;

/// <summary>
/// Gets or sets the numeral value that will be incremented.
/// </summary>
[RequiredArgument]
public InOutArgument<TNumeral> Numeral { get; set; } = new();

/// <inheritdoc/>
protected override void CacheMetadata(CodeActivityMetadata metadata)
{
UnaryExpressionHelper.OnGetArguments(metadata, Numeral);
EnsureOperationFunction(metadata, ref operationFunction!, ExpressionType.Decrement);
}

private static void EnsureOperationFunction
(
CodeActivityMetadata metadata,
ref Func<TNumeral, TNumeral> operationFunction,
ExpressionType operatorType
)
{
if (operationFunction is null)
{
if (!UnaryExpressionHelper.TryGenerateLinqDelegate(operatorType, out operationFunction!, out ValidationError? validationError))
{
metadata.AddValidationError(validationError);
}
}
}

/// <inheritdoc/>
protected override TNumeral Execute(CodeActivityContext context)
{
TNumeral value = Numeral.Get(context);
return operationFunction!.Invoke(value);
}
}
}
57 changes: 57 additions & 0 deletions src/UiPath.Workflow.Runtime/Expressions/Increment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// This file is part of Core WF which is licensed under the MIT license.
// See LICENSE file in the project root for full license information.

using System.Activities.Validation;
using System.Linq.Expressions;
using System.Numerics;

namespace System.Activities.Expressions
{
/// <summary>
/// A code activity which increments a numeral.
/// </summary>
/// <typeparam name="TNumeral">A numeric value, such as <see langword="int" /> or <see langword="long" />.</typeparam>
public sealed class Increment<TNumeral> : CodeActivity<TNumeral>
#if NET7_0_OR_GREATER
where TNumeral : IIncrementOperators<TNumeral>
#endif
{
private static Func<TNumeral, TNumeral>? operationFunction = null!;

/// <summary>
/// Gets or sets the numeral value that will be incremented.
/// </summary>
[RequiredArgument]
public InOutArgument<TNumeral> Numeral { get; set; } = new();

/// <inheritdoc/>
protected override void CacheMetadata(CodeActivityMetadata metadata)
{
UnaryExpressionHelper.OnGetArguments<TNumeral>(metadata, Numeral);
EnsureOperationFunction(metadata, ref operationFunction!, ExpressionType.Increment);
}

private static void EnsureOperationFunction
(
CodeActivityMetadata metadata,
ref Func<TNumeral, TNumeral> operationFunction,
ExpressionType operatorType
)
{
if (operationFunction is null)
{
if (!UnaryExpressionHelper.TryGenerateLinqDelegate(operatorType, out operationFunction!, out ValidationError? validationError))
{
metadata.AddValidationError(validationError);
}
}
}

/// <inheritdoc/>
protected override TNumeral Execute(CodeActivityContext context)
{
TNumeral value = Numeral.Get(context);
return operationFunction!.Invoke(value);
}
}
}
98 changes: 98 additions & 0 deletions src/UiPath.Workflow.Runtime/Expressions/SubtractAssign.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// This file is part of Core WF which is licensed under the MIT license.
// See LICENSE file in the project root for full license information.

using System.Activities.Validation;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Numerics;

namespace System.Activities.Expressions
{
/// <summary>
/// A code activity which deccrementally assigns a numeral.
/// </summary>
/// <example>
/// int myNum = 5;
/// myNum += 5; // 10.
/// </example>
/// <typeparam name="TLeft">The operand to modify.</typeparam>
/// <typeparam name="TRight">The operand with which to modify <typeparamref name="TLeft"/>.</typeparam>
/// <typeparam name="TResult">The resulting type of hte operation.</typeparam>
public sealed class SubtractAssign<TLeft, TRight, TResult> : CodeActivity<TResult>
#if NET7_0_OR_GREATER
where TLeft : INumber<TLeft>
where TRight : INumber<TRight>
where TResult : INumber<TResult>
#endif
{
private static Func<TLeft, TRight, TResult>? checkedOperationFunction = null;
private static Func<TLeft, TRight, TResult>? uncheckedOperationFunction = null;

/// <summary>
/// Gets or sets the left operand which will be modified by this operation.
/// </summary>
[RequiredArgument]
public InArgument<TLeft> Left { get; set; } = new();

/// <summary>
/// Gets or sets the right operand which will be the modifier for this operation.
/// </summary>
[RequiredArgument]
public InArgument<TRight> Right { get; set; } = new();

/// <summary>
/// Gets or sets a value indicating whether this operation will happen in a checked or unchecked context.
/// </summary>
[DefaultValue(true)]
public bool IsChecked { get; set; } = true;

/// <inheritdoc/>
protected override void CacheMetadata(CodeActivityMetadata metadata)
{
BinaryExpressionHelper.OnGetArguments(metadata, Left, Right);

if (IsChecked)
{
EnsureOperationFunction(metadata, ref checkedOperationFunction, ExpressionType.SubtractAssignChecked);
}
else
{
EnsureOperationFunction(metadata, ref uncheckedOperationFunction, ExpressionType.SubtractAssign);
}
}

private static void EnsureOperationFunction
(
CodeActivityMetadata metadata,
ref Func<TLeft, TRight, TResult>? operationFunction,
ExpressionType operatorType
)
{
if (operationFunction == null)
{
if (!BinaryExpressionHelper.TryGenerateLinqDelegate(
operatorType,
out operationFunction,
out ValidationError? validationError))
{
metadata.AddValidationError(validationError);
}
}
}

/// <inheritdoc/>
protected override TResult Execute(CodeActivityContext context)
{
TLeft leftValue = Left.Get(context);
TRight rightValue = Right.Get(context);

// If user changed checked flag between open and execution, an NRE may be thrown.
// This is by design.
// Nullability check silenced because the corresponding value is guaranteed to be
// non-null
return IsChecked
? checkedOperationFunction!(leftValue, rightValue)
: uncheckedOperationFunction!(leftValue, rightValue);
}
}
}
Loading