using System.Linq.Expressions; using AIStudio.Settings.DataModel; namespace AIStudio.Settings; public static partial class ManagedConfiguration { /// /// Registers a configuration setting with a default value. /// /// /// When called from the JSON deserializer, the configSelection parameter will be null. /// In this case, the method will return the default value without registering the setting. /// /// The expression to select the configuration class. /// The expression to select the property within the configuration class. /// The default value to use when the setting is not configured. /// The type of the configuration class. /// The type of the property within the configuration class. /// The default value. public static TValue Register( Expression>? configSelection, Expression> propertyExpression, TValue defaultValue) where TValue : struct { // 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 defaultValue; var configPath = Path(configSelection, propertyExpression); // If the metadata already exists for this configuration path, we return the default value: if (METADATA.ContainsKey(configPath)) return defaultValue; // Not registered yet, so we register it now: METADATA[configPath] = new ConfigMeta(configSelection, propertyExpression) { Default = defaultValue, }; return defaultValue; } /// /// Registers a configuration setting with a default value. /// /// /// When called from the JSON deserializer, the configSelection parameter will be null. /// In this case, the method will return the default value without registering the setting. /// /// The expression to select the configuration class. /// The expression to select the property within the configuration class. /// The default value to use when the setting is not configured. /// The type of the configuration class. /// The default value. public static string Register( Expression>? configSelection, Expression> propertyExpression, string defaultValue) { // 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 defaultValue; var configPath = Path(configSelection, propertyExpression); // If the metadata already exists for this configuration path, we return the default value: if (METADATA.ContainsKey(configPath)) return defaultValue; // Not registered yet, so we register it now: METADATA[configPath] = new ConfigMeta(configSelection, propertyExpression) { Default = defaultValue, }; return defaultValue; } /// /// Registers a configuration setting with a default value for a IList of TValue. /// /// /// If the configSelection parameter is null, the method returns a list containing the default value /// without registering the configuration setting. /// /// The expression to select the configuration class. /// The expression to select the property within the configuration class. /// The default value to use when the setting is not configured. /// The type of the configuration class. /// The type of the elements in the list within the configuration class. /// A list containing the default value. public static List Register( Expression>? configSelection, Expression>> propertyExpression, TValue defaultValue) { // 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 [defaultValue]; var configPath = Path(configSelection, propertyExpression); // If the metadata already exists for this configuration path, we return the default value: if (METADATA.ContainsKey(configPath)) return [defaultValue]; // Not registered yet, so we register it now: METADATA[configPath] = new ConfigMeta>(configSelection, propertyExpression) { Default = [defaultValue], }; return [defaultValue]; } /// /// Registers a configuration setting with multiple default values. /// /// /// When called with a null configSelection parameter, the method ignores the register call and directly returns the default values. /// If the configuration path already exists in the metadata, the method also returns the default values without registering new metadata. /// /// The expression used to select the configuration class. /// The expression used to select the property within the configuration class. /// The list of default values to be used when the configuration setting is not defined. /// The type of the configuration class. /// The type of the elements within the property list. /// The list of default values. public static List Register( Expression>? configSelection, Expression>> propertyExpression, IList defaultValues) { // 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 [..defaultValues]; var configPath = Path(configSelection, propertyExpression); // If the metadata already exists for this configuration path, we return the default value: if (METADATA.ContainsKey(configPath)) return [..defaultValues]; // Not registered yet, so we register it now: METADATA[configPath] = new ConfigMeta>(configSelection, propertyExpression) { Default = [..defaultValues], }; return [..defaultValues]; } /// /// Registers a configuration setting with a default value. /// /// /// When called with a null configSelection, this method returns the default value without registering the setting. /// /// The expression to select the configuration class. /// The expression to select the set within the configuration class. /// The default value to use when the setting is not configured. /// The type of the configuration class. /// The type of the values within the set. /// A set containing the default value. public static HashSet Register( Expression>? configSelection, Expression>> propertyExpression, TValue defaultValue) { // 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 [defaultValue]; var configPath = Path(configSelection, propertyExpression); // If the metadata already exists for this configuration path, we return the default value: if (METADATA.ContainsKey(configPath)) return [defaultValue]; // Not registered yet, so we register it now: METADATA[configPath] = new ConfigMeta>(configSelection, propertyExpression) { Default = new HashSet { defaultValue }, }; return [defaultValue]; } /// /// Registers a configuration setting with a collection of default values. /// /// /// When the method is invoked with a null configSelection, the configuration path /// is ignored, and the specified default values are returned without registration. /// /// The expression that selects the configuration class from the root Data model. /// The expression to select the property within the configuration class. /// The default collection of values to use when the setting is not configured. /// The type of the configuration class from which the property is selected. /// The type of the elements in the collection associated with the configuration property. /// A set containing the default values. public static HashSet Register( Expression>? configSelection, Expression>> propertyExpression, IList defaultValues) { // 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 [..defaultValues]; var configPath = Path(configSelection, propertyExpression); // If the metadata already exists for this configuration path, we return the default value: if (METADATA.ContainsKey(configPath)) return [..defaultValues]; // Not registered yet, so we register it now: METADATA[configPath] = new ConfigMeta>(configSelection, propertyExpression) { Default = new HashSet(defaultValues), }; return [..defaultValues]; } /// /// Registers a configuration setting with a default dictionary of string key-value pairs. /// /// /// When the method is invoked with a null configSelection, the configuration path /// is ignored, and the specified default values are returned without registration. /// /// The expression that selects the configuration class from the root Data model. /// The expression to select the property within the configuration class. /// The default dictionary of values to use when the setting is not configured. /// The type of the configuration class from which the property is selected. /// >The type of the dictionary within the configuration class. /// A dictionary containing the default values. public static TDict Register( Expression>? configSelection, Expression>> propertyExpression, TDict defaultValues) where TDict : IDictionary, 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 new(); var configPath = Path(configSelection, propertyExpression); // If the metadata already exists for this configuration path, we return the default value: if (METADATA.ContainsKey(configPath)) return defaultValues; // Not registered yet, so we register it now: METADATA[configPath] = new ConfigMeta>(configSelection, propertyExpression) { Default = defaultValues, }; return defaultValues; } }