From 31130212186510d0e4db9be5ada1cb1dfa383f4d Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Tue, 26 Aug 2025 10:50:42 +0200 Subject: [PATCH] Support prefix and postfix increment operations in `TryIncrement` method --- .../Tools/ExpressionExtensions.cs | 30 ++++++++++++++----- app/MindWork AI Studio/Tools/IncrementType.cs | 14 +++++++++ .../PluginSystem/PluginConfigurationObject.cs | 4 +-- 3 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 app/MindWork AI Studio/Tools/IncrementType.cs diff --git a/app/MindWork AI Studio/Tools/ExpressionExtensions.cs b/app/MindWork AI Studio/Tools/ExpressionExtensions.cs index f631994d..bb57fad3 100644 --- a/app/MindWork AI Studio/Tools/ExpressionExtensions.cs +++ b/app/MindWork AI Studio/Tools/ExpressionExtensions.cs @@ -42,10 +42,11 @@ public static class ExpressionExtensions /// An expression representing the property to be incremented. The property /// must be of type uint and belong to the provided object. /// The object that contains the property referenced by the expression. + /// The type of increment operation to perform (e.g., prefix or postfix). /// The type of the object that contains the property to be incremented. /// The type of the property to be incremented. /// An IncrementResult object containing the result of the increment operation. - public static IncrementResult TryIncrement(this Expression> expression, TIn data) where TOut : IBinaryInteger + public static IncrementResult TryIncrement(this Expression> expression, TIn data, IncrementType type) where TOut : IBinaryInteger { // Ensure that the expression body is a member expression: if (expression.Body is not MemberExpression memberExpression) @@ -53,11 +54,11 @@ public static class ExpressionExtensions // Ensure that the member expression is a property: if (memberExpression.Member is not PropertyInfo propertyInfo) - return new(false, TOut.Zero);; + return new(false, TOut.Zero); // Ensure that the member expression has a target object: if (memberExpression.Expression is null) - return new(false, TOut.Zero);; + 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,15 +69,28 @@ public static class ExpressionExtensions // Was the compilation successful? if (targetObject is null) - return new(false, TOut.Zero);; + return new(false, TOut.Zero); // Read the current value of the property: if (propertyInfo.GetValue(targetObject) is not TOut value) - return new(false, TOut.Zero);; + return new(false, TOut.Zero); // Increment the value: - var nextValue = value + TOut.CreateChecked(1); - propertyInfo.SetValue(targetObject, nextValue); - return new(true, nextValue); + switch (type) + { + case IncrementType.PRE: + var nextValue = value + TOut.CreateChecked(1); + propertyInfo.SetValue(targetObject, nextValue); + return new(true, nextValue); + + case IncrementType.POST: + var currentValue = value; + var incrementedValue = value + TOut.CreateChecked(1); + propertyInfo.SetValue(targetObject, incrementedValue); + return new(true, currentValue); + + default: + return new(false, TOut.Zero); + } } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Tools/IncrementType.cs b/app/MindWork AI Studio/Tools/IncrementType.cs new file mode 100644 index 00000000..a675f009 --- /dev/null +++ b/app/MindWork AI Studio/Tools/IncrementType.cs @@ -0,0 +1,14 @@ +namespace AIStudio.Tools; + +public enum IncrementType +{ + /// + /// Increments the value before returning it. So, the incremented value is returned. + /// + PRE, + + /// + /// Increments the value after returning it. So, the original value is returned. + /// + POST, +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Tools/PluginSystem/PluginConfigurationObject.cs b/app/MindWork AI Studio/Tools/PluginSystem/PluginConfigurationObject.cs index 60704414..5be55ddb 100644 --- a/app/MindWork AI Studio/Tools/PluginSystem/PluginConfigurationObject.cs +++ b/app/MindWork AI Studio/Tools/PluginSystem/PluginConfigurationObject.cs @@ -131,9 +131,9 @@ public sealed record PluginConfigurationObject // Case: The object does not exist, we have to add it else { - // Case: Increment the next number was successful - if (nextConfigObjectNumSelection.TryIncrement(SETTINGS_MANAGER.ConfigurationData) is { Success: true, UpdatedValue: var nextNum }) + if (nextConfigObjectNumSelection.TryIncrement(SETTINGS_MANAGER.ConfigurationData, IncrementType.POST) is { Success: true, UpdatedValue: var nextNum }) { + // Case: Increment the next number was successful configObject = configObject with { Num = nextNum }; storedObjects.Add((TClass)configObject); }