Refactor managed configuration methods to use IDictionary for flexibility

This commit is contained in:
Thorsten Sommer 2025-10-19 10:40:31 +02:00
parent 8d2bf62cf2
commit fa235c8a00
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 12 additions and 10 deletions

View File

@ -622,7 +622,7 @@ public static partial class ManagedConfiguration
/// <returns>True when the configuration was successfully processed, otherwise false.</returns> /// <returns>True when the configuration was successfully processed, otherwise false.</returns>
public static bool TryProcessConfiguration<TClass>( public static bool TryProcessConfiguration<TClass>(
Expression<Func<Data, TClass>> configSelection, Expression<Func<Data, TClass>> configSelection,
Expression<Func<TClass, Dictionary<string, string>>> propertyExpression, Expression<Func<TClass, IDictionary<string, string>>> propertyExpression,
Guid configPluginId, Guid configPluginId,
LuaTable settings, LuaTable settings,
bool dryRun) 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. // 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: // Instead of using ArrayLength, we use HashMapCount to get the number of key-value pairs:
var len = valueTable.HashMapCount; 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. // 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 // 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 both key and value were read successfully, add them to the dictionary:
if (hadKey && hadValue) if (hadKey && hadValue)
dict[key] = value; configuredValue[key] = value;
} }
configuredValue = dict;
successful = true; successful = true;
} }

View File

@ -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="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> /// <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="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> /// <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<Data, TClass>>? configSelection,
Expression<Func<TClass, Dictionary<string, string>>> propertyExpression, Expression<Func<TClass, IDictionary<string, string>>> propertyExpression,
Dictionary<string, string> defaultValues) TDict defaultValues)
where TDict : IDictionary<string, string>, new()
{ {
// When called from the JSON deserializer by using the standard constructor, // When called from the JSON deserializer by using the standard constructor,
// we ignore the register call and return the default value: // we ignore the register call and return the default value:
if (configSelection is null) if (configSelection is null)
return []; return new();
var configPath = Path(configSelection, propertyExpression); var configPath = Path(configSelection, propertyExpression);
@ -262,7 +264,7 @@ public static partial class ManagedConfiguration
return defaultValues; return defaultValues;
// Not registered yet, so we register it now: // 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, Default = defaultValues,
}; };