From 28502f9019953bf7c296f52c85e608dd859ee686 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Thu, 21 Aug 2025 16:20:01 +0200 Subject: [PATCH] Introduce `IncrementResult` record and refactor `TryIncrement` to return structured result --- .../Tools/ExpressionExtensions.cs | 19 ++++++++++--------- .../Tools/IncrementResult.cs | 10 ++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 app/MindWork AI Studio/Tools/IncrementResult.cs diff --git a/app/MindWork AI Studio/Tools/ExpressionExtensions.cs b/app/MindWork AI Studio/Tools/ExpressionExtensions.cs index 1521678a..f631994d 100644 --- a/app/MindWork AI Studio/Tools/ExpressionExtensions.cs +++ b/app/MindWork AI Studio/Tools/ExpressionExtensions.cs @@ -44,20 +44,20 @@ public static class ExpressionExtensions /// The object that contains the property referenced by the expression. /// The type of the object that contains the property to be incremented. /// The type of the property to be incremented. - /// True if the property was successfully incremented, otherwise false. - public static bool TryIncrement(this Expression> expression, TIn data) where TOut : IBinaryInteger + /// An IncrementResult object containing the result of the increment operation. + public static IncrementResult TryIncrement(this Expression> expression, TIn data) where TOut : IBinaryInteger { // Ensure that the expression body is a member expression: if (expression.Body is not MemberExpression memberExpression) - return false; + return new(false, TOut.Zero); // Ensure that the member expression is a property: if (memberExpression.Member is not PropertyInfo propertyInfo) - return false; + return new(false, TOut.Zero);; // Ensure that the member expression has a target object: if (memberExpression.Expression is null) - return false; + return new(false, TOut.Zero);; // Get the target object for the expression, which is the object containing the property to increment: var targetObjectExpression = Expression.Lambda(memberExpression.Expression, expression.Parameters); @@ -68,14 +68,15 @@ public static class ExpressionExtensions // Was the compilation successful? if (targetObject is null) - return false; + return new(false, TOut.Zero);; // Read the current value of the property: if (propertyInfo.GetValue(targetObject) is not TOut value) - return false; + return new(false, TOut.Zero);; // Increment the value: - propertyInfo.SetValue(targetObject, value + TOut.CreateChecked(1)); - return true; + var nextValue = value + TOut.CreateChecked(1); + propertyInfo.SetValue(targetObject, nextValue); + return new(true, nextValue); } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Tools/IncrementResult.cs b/app/MindWork AI Studio/Tools/IncrementResult.cs new file mode 100644 index 00000000..76efecd4 --- /dev/null +++ b/app/MindWork AI Studio/Tools/IncrementResult.cs @@ -0,0 +1,10 @@ +using System.Numerics; + +namespace AIStudio.Tools; + +/// +/// Represents the result of an increment operation. It encapsulates whether the operation +/// was successful and the increased value. +/// +/// The type of the incremented value, constrained to implement the IBinaryInteger interface. +public sealed record IncrementResult(bool Success, TOut UpdatedValue) where TOut : IBinaryInteger; \ No newline at end of file