mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-11-23 10:50:21 +00:00
Refactor managed configuration methods to use IDictionary for flexibility
This commit is contained in:
parent
8d2bf62cf2
commit
fa235c8a00
@ -622,7 +622,7 @@ public static partial class ManagedConfiguration
|
||||
/// <returns>True when the configuration was successfully processed, otherwise false.</returns>
|
||||
public static bool TryProcessConfiguration<TClass>(
|
||||
Expression<Func<Data, TClass>> configSelection,
|
||||
Expression<Func<TClass, Dictionary<string, string>>> propertyExpression,
|
||||
Expression<Func<TClass, IDictionary<string, string>>> propertyExpression,
|
||||
Guid configPluginId,
|
||||
LuaTable settings,
|
||||
bool dryRun)
|
||||
@ -646,7 +646,8 @@ public static partial class ManagedConfiguration
|
||||
// Determine the length of the Lua table and prepare a dictionary to hold the parsed key-value pairs.
|
||||
// Instead of using ArrayLength, we use HashMapCount to get the number of key-value pairs:
|
||||
var len = valueTable.HashMapCount;
|
||||
var dict = new Dictionary<string, string>(len);
|
||||
if (len > 0)
|
||||
configuredValue.Clear();
|
||||
|
||||
// In order to iterate over all key-value pairs in the Lua table, we have to use TryGetNext.
|
||||
// Thus, we initialize the previous key variable to Nil and keep calling TryGetNext until
|
||||
@ -663,10 +664,9 @@ public static partial class ManagedConfiguration
|
||||
|
||||
// If both key and value were read successfully, add them to the dictionary:
|
||||
if (hadKey && hadValue)
|
||||
dict[key] = value;
|
||||
configuredValue[key] = value;
|
||||
}
|
||||
|
||||
configuredValue = dict;
|
||||
successful = true;
|
||||
}
|
||||
|
||||
|
||||
@ -232,7 +232,7 @@ public static partial class ManagedConfiguration
|
||||
|
||||
return [..defaultValues];
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Registers a configuration setting with a default dictionary of string key-value pairs.
|
||||
/// </summary>
|
||||
@ -244,16 +244,18 @@ public static partial class ManagedConfiguration
|
||||
/// <param name="propertyExpression">The expression to select the property within the configuration class.</param>
|
||||
/// <param name="defaultValues">The default dictionary of values to use when the setting is not configured.</param>
|
||||
/// <typeparam name="TClass">The type of the configuration class from which the property is selected.</typeparam>
|
||||
/// <typeparam name="TDict">>The type of the dictionary within the configuration class.</typeparam>
|
||||
/// <returns>A dictionary containing the default values.</returns>
|
||||
public static Dictionary<string, string> Register<TClass>(
|
||||
public static TDict Register<TClass, TDict>(
|
||||
Expression<Func<Data, TClass>>? configSelection,
|
||||
Expression<Func<TClass, Dictionary<string, string>>> propertyExpression,
|
||||
Dictionary<string, string> defaultValues)
|
||||
Expression<Func<TClass, IDictionary<string, string>>> propertyExpression,
|
||||
TDict defaultValues)
|
||||
where TDict : IDictionary<string, string>, new()
|
||||
{
|
||||
// When called from the JSON deserializer by using the standard constructor,
|
||||
// we ignore the register call and return the default value:
|
||||
if (configSelection is null)
|
||||
return [];
|
||||
return new();
|
||||
|
||||
var configPath = Path(configSelection, propertyExpression);
|
||||
|
||||
@ -262,7 +264,7 @@ public static partial class ManagedConfiguration
|
||||
return defaultValues;
|
||||
|
||||
// Not registered yet, so we register it now:
|
||||
METADATA[configPath] = new ConfigMeta<TClass, Dictionary<string, string>>(configSelection, propertyExpression)
|
||||
METADATA[configPath] = new ConfigMeta<TClass, IDictionary<string, string>>(configSelection, propertyExpression)
|
||||
{
|
||||
Default = defaultValues,
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user