diff --git a/app/MindWork AI Studio/Settings/ManagedConfiguration.Parsing.cs b/app/MindWork AI Studio/Settings/ManagedConfiguration.Parsing.cs index a475937e..cbe3b968 100644 --- a/app/MindWork AI Studio/Settings/ManagedConfiguration.Parsing.cs +++ b/app/MindWork AI Studio/Settings/ManagedConfiguration.Parsing.cs @@ -91,7 +91,7 @@ public static partial class ManagedConfiguration LuaTable settings, bool dryRun, ISpanParsable? _ = null) - where TValue : ISpanParsable + where TValue : struct, ISpanParsable { // // Handle configured ISpanParsable values diff --git a/app/MindWork AI Studio/Settings/ManagedConfiguration.cs b/app/MindWork AI Studio/Settings/ManagedConfiguration.cs index 0bfb61b2..5cc7a700 100644 --- a/app/MindWork AI Studio/Settings/ManagedConfiguration.cs +++ b/app/MindWork AI Studio/Settings/ManagedConfiguration.cs @@ -12,7 +12,7 @@ public static partial class ManagedConfiguration /// /// Attempts to retrieve the configuration metadata for a given configuration selection and - /// property expression. + /// property expression (enum-based). /// /// /// When no configuration metadata is found, it returns a NoConfig instance with the default @@ -31,6 +31,7 @@ public static partial class ManagedConfiguration Expression> configSelection, Expression> propertyExpression, out ConfigMeta configMeta) + where TValue : Enum { var configPath = Path(configSelection, propertyExpression); if (METADATA.TryGetValue(configPath, out var value) && value is ConfigMeta meta) @@ -47,6 +48,84 @@ public static partial class ManagedConfiguration return false; } + /// + /// Attempts to retrieve the configuration metadata for a given configuration selection and + /// property expression (string-based). + /// + /// + /// When no configuration metadata is found, it returns a NoConfig instance with the default + /// value set to default(TValue). This allows the caller to handle the absence of configuration + /// gracefully. In such cases, the return value of the method will be false. + /// + /// The expression to select the configuration class. + /// The expression to select the property within the + /// configuration class. + /// The output parameter that will hold the configuration metadata + /// if found. + /// The type of the configuration class. + /// True if the configuration metadata was found, otherwise false. + public static bool TryGet( + Expression> configSelection, + Expression> propertyExpression, + out ConfigMeta configMeta) + { + var configPath = Path(configSelection, propertyExpression); + if (METADATA.TryGetValue(configPath, out var value) && value is ConfigMeta meta) + { + configMeta = meta; + return true; + } + + configMeta = new NoConfig(configSelection, propertyExpression) + { + Default = string.Empty, + }; + return false; + } + + /// + /// Attempts to retrieve the configuration metadata for a given configuration selection and + /// property expression (ISpanParsable-based). + /// + /// + /// When no configuration metadata is found, it returns a NoConfig instance with the default + /// value set to default(TValue). This allows the caller to handle the absence of configuration + /// gracefully. In such cases, the return value of the method will be false. + /// + /// The expression to select the configuration class. + /// The expression to select the property within the + /// configuration class. + /// The output parameter that will hold the configuration metadata + /// if found. + /// An optional parameter to help with method overload resolution. + /// The type of the configuration class. + /// The type of the property within the configuration class. + /// True if the configuration metadata was found, otherwise false. + + // ReSharper disable MethodOverloadWithOptionalParameter + public static bool TryGet( + Expression> configSelection, + Expression> propertyExpression, + out ConfigMeta configMeta, + ISpanParsable? _ = null) + where TValue : struct, ISpanParsable + { + var configPath = Path(configSelection, propertyExpression); + if (METADATA.TryGetValue(configPath, out var value) && value is ConfigMeta meta) + { + configMeta = meta; + return true; + } + + configMeta = new NoConfig(configSelection, propertyExpression) + { + Default = default!, + }; + return false; + } + + // ReSharper restore MethodOverloadWithOptionalParameter + /// /// Attempts to retrieve the configuration metadata for a list-based setting. /// @@ -163,23 +242,135 @@ public static partial class ManagedConfiguration /// The type of the configuration class. /// The type of the property within the configuration class. /// True if the configuration setting is left over and was reset, otherwise false. - public static bool IsConfigurationLeftOver(Expression> configSelection, Expression> propertyExpression, IEnumerable availablePlugins) + public static bool IsConfigurationLeftOver( + Expression> configSelection, + Expression> propertyExpression, + IEnumerable availablePlugins) + where TValue : Enum { if (!TryGet(configSelection, propertyExpression, out var configMeta)) return false; - if(configMeta.MangedByConfigPluginId == Guid.Empty || !configMeta.IsLocked) + if (configMeta.MangedByConfigPluginId == Guid.Empty || !configMeta.IsLocked) return false; - - // Check if the configuration plugin ID is valid against the available plugin IDs: + var plugin = availablePlugins.FirstOrDefault(x => x.Id == configMeta.MangedByConfigPluginId); if (plugin is null) { - // Remove the locked state: configMeta.ResetManagedState(); return true; } - + + return false; + } + + public static bool IsConfigurationLeftOver( + Expression> configSelection, + Expression> propertyExpression, + IEnumerable availablePlugins) + { + if (!TryGet(configSelection, propertyExpression, out var configMeta)) + return false; + + if (configMeta.MangedByConfigPluginId == Guid.Empty || !configMeta.IsLocked) + return false; + + var plugin = availablePlugins.FirstOrDefault(x => x.Id == configMeta.MangedByConfigPluginId); + if (plugin is null) + { + configMeta.ResetManagedState(); + return true; + } + + return false; + } + + // ReSharper disable MethodOverloadWithOptionalParameter + public static bool IsConfigurationLeftOver( + Expression> configSelection, + Expression> propertyExpression, + IEnumerable availablePlugins, + ISpanParsable? _ = null) + where TValue : struct, ISpanParsable + { + if (!TryGet(configSelection, propertyExpression, out var configMeta)) + return false; + + if (configMeta.MangedByConfigPluginId == Guid.Empty || !configMeta.IsLocked) + return false; + + var plugin = availablePlugins.FirstOrDefault(x => x.Id == configMeta.MangedByConfigPluginId); + if (plugin is null) + { + configMeta.ResetManagedState(); + return true; + } + + return false; + } + + // ReSharper restore MethodOverloadWithOptionalParameter + + public static bool IsConfigurationLeftOver( + Expression> configSelection, + Expression>> propertyExpression, + IEnumerable availablePlugins) + { + if (!TryGet(configSelection, propertyExpression, out var configMeta)) + return false; + + if (configMeta.MangedByConfigPluginId == Guid.Empty || !configMeta.IsLocked) + return false; + + var plugin = availablePlugins.FirstOrDefault(x => x.Id == configMeta.MangedByConfigPluginId); + if (plugin is null) + { + configMeta.ResetManagedState(); + return true; + } + + return false; + } + + public static bool IsConfigurationLeftOver( + Expression> configSelection, + Expression>> propertyExpression, + IEnumerable availablePlugins) + { + if (!TryGet(configSelection, propertyExpression, out var configMeta)) + return false; + + if (configMeta.MangedByConfigPluginId == Guid.Empty || !configMeta.IsLocked) + return false; + + var plugin = availablePlugins.FirstOrDefault(x => x.Id == configMeta.MangedByConfigPluginId); + if (plugin is null) + { + configMeta.ResetManagedState(); + return true; + } + + return false; + } + + public static bool IsConfigurationLeftOver( + Expression> configSelection, + Expression>> propertyExpression, + IEnumerable availablePlugins) + { + if (!TryGet(configSelection, propertyExpression, out var configMeta)) + return false; + + if (configMeta.MangedByConfigPluginId == Guid.Empty || !configMeta.IsLocked) + return false; + + var plugin = availablePlugins.FirstOrDefault(x => x.Id == configMeta.MangedByConfigPluginId); + if (plugin is null) + { + configMeta.ResetManagedState(); + return true; + } + return false; }