From d3c4069fea06ba2c12afc7ccc132df3f85dc2941 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Tue, 27 Apr 2021 19:41:05 -0300 Subject: [PATCH] Add final missing immutable facade, ConfigSection Fixes #61 --- src/Config/ConfigSection.cs | 49 +++++++++----- src/Config/ConfigSectionExtensions.cs | 92 +++++++++++++-------------- 2 files changed, 78 insertions(+), 63 deletions(-) diff --git a/src/Config/ConfigSection.cs b/src/Config/ConfigSection.cs index e4acdf6..53fb769 100644 --- a/src/Config/ConfigSection.cs +++ b/src/Config/ConfigSection.cs @@ -6,13 +6,13 @@ namespace DotNetConfig /// /// Provides access to a specific section and optional subsection. /// - public class ConfigSection + public record ConfigSection { internal ConfigSection(Config config, string section, string? subsection) => (Config, Section, Subsection) = (config, section, subsection); - internal Config Config { get; } + internal Config Config { get; init; } internal string Section { get; } @@ -23,28 +23,32 @@ internal ConfigSection(Config config, string section, string? subsection) /// /// The variable to assign. /// Value add to the variable. - public void AddBoolean(string variable, bool value) => Config.AddBoolean(Section, Subsection, variable, value); + public ConfigSection AddBoolean(string variable, bool value) + => this with { Config = Config.AddBoolean(Section, Subsection, variable, value) }; /// /// Adds a value to a multi-valued variable in the current section/subsection. /// /// The variable to assign. /// Value add to the variable. - public void AddDateTime(string variable, DateTime value) => Config.AddDateTime(Section, Subsection, variable, value); + public ConfigSection AddDateTime(string variable, DateTime value) + => this with { Config = Config.AddDateTime(Section, Subsection, variable, value) }; /// /// Adds a value to a multi-valued variable in the current section/subsection. /// /// The variable to assign. /// Value add to the variable. - public void AddNumber(string variable, long value) => Config.AddNumber(Section, Subsection, variable, value); + public ConfigSection AddNumber(string variable, long value) + => this with { Config = Config.AddNumber(Section, Subsection, variable, value) }; /// /// Adds a value to a multi-valued variable in the current section/subsection. /// /// The variable to assign. /// Value add to the variable. - public void AddString(string variable, string value) => Config.AddString(Section, Subsection, variable, value); + public ConfigSection AddString(string variable, string value) + => this with { Config = Config.AddString(Section, Subsection, variable, value) }; /// /// Gets all values from a multi-valued variable from the current section/subsection, @@ -52,7 +56,8 @@ internal ConfigSection(Config config, string section, string? subsection) /// /// The variable to remove. /// Filter returned entries to those where the value matches the given expression. - public IEnumerable GetAll(string variable, string? valueRegex = null) => Config.GetAll(Section, Subsection, variable, valueRegex); + public IEnumerable GetAll(string variable, string? valueRegex = null) + => Config.GetAll(Section, Subsection, variable, valueRegex); /// /// Gets a string variable and applies path normalization to it, resolving @@ -69,7 +74,8 @@ internal ConfigSection(Config config, string section, string? subsection) /// The variable to assign. /// Value to assign to the matching variables. /// Filter returned entries to those where the value matches the given expression. - public void SetAllBoolean(string variable, bool value, string? valueRegex = null) => Config.SetAllBoolean(Section, Subsection, variable, value, valueRegex); + public ConfigSection SetAllBoolean(string variable, bool value, string? valueRegex = null) + => this with { Config = Config.SetAllBoolean(Section, Subsection, variable, value, valueRegex) }; /// /// Sets the value of all matching variables in the current section/subsection. @@ -77,7 +83,8 @@ internal ConfigSection(Config config, string section, string? subsection) /// The variable to assign. /// Value to assign to the matching variables. /// Filter returned entries to those where the value matches the given expression. - public void SetAllDateTime(string variable, DateTime value, string? valueRegex = null) => Config.SetAllDateTime(Section, Subsection, variable, value, valueRegex); + public ConfigSection SetAllDateTime(string variable, DateTime value, string? valueRegex = null) + => this with { Config = Config.SetAllDateTime(Section, Subsection, variable, value, valueRegex) }; /// /// Sets the value of all matching variables in the current section/subsection. @@ -85,7 +92,8 @@ internal ConfigSection(Config config, string section, string? subsection) /// The variable to assign. /// Value to assign to the matching variables. /// Filter returned entries to those where the value matches the given expression. - public void SetAllNumber(string variable, long value, string? valueRegex = null) => Config.SetAllNumber(Section, Subsection, variable, value, valueRegex); + public ConfigSection SetAllNumber(string variable, long value, string? valueRegex = null) + => this with { Config = Config.SetAllNumber(Section, Subsection, variable, value, valueRegex) }; /// /// Sets the value of all matching variables in the current section/subsection. @@ -93,7 +101,8 @@ internal ConfigSection(Config config, string section, string? subsection) /// The variable to assign. /// Value to assign to the matching variables. /// Filter returned entries to those where the value matches the given expression. - public void SetAllString(string variable, string value, string? valueRegex = null) => Config.SetAllString(Section, Subsection, variable, value, valueRegex); + public ConfigSection SetAllString(string variable, string value, string? valueRegex = null) + => this with { Config = Config.SetAllString(Section, Subsection, variable, value, valueRegex) }; /// /// Sets the value of a variable in the current section/subsection. @@ -101,7 +110,8 @@ internal ConfigSection(Config config, string section, string? subsection) /// The variable to assign. /// Value to assign to the variable. /// Filter returned entries to those where the value matches the given expression. - public void SetBoolean(string variable, bool value, string? valueRegex = null) => Config.SetBoolean(Section, Subsection, variable, value, valueRegex); + public ConfigSection SetBoolean(string variable, bool value, string? valueRegex = null) + => this with { Config = Config.SetBoolean(Section, Subsection, variable, value, valueRegex) }; /// /// Sets the value of a variable in the current section/subsection. @@ -109,7 +119,8 @@ internal ConfigSection(Config config, string section, string? subsection) /// The variable to assign. /// Value to assign to the variable. /// Filter returned entries to those where the value matches the given expression. - public void SetDateTime(string variable, DateTime value, string? valueRegex = null) => Config.SetDateTime(Section, Subsection, variable, value, valueRegex); + public ConfigSection SetDateTime(string variable, DateTime value, string? valueRegex = null) + => this with { Config = Config.SetDateTime(Section, Subsection, variable, value, valueRegex) }; /// /// Sets the value of a variable in the current section/subsection. @@ -117,7 +128,8 @@ internal ConfigSection(Config config, string section, string? subsection) /// The variable to assign. /// Value to assign to the variable. /// Filter returned entries to those where the value matches the given expression. - public void SetNumber(string variable, long value, string? valueRegex = null) => Config.SetNumber(Section, Subsection, variable, value, valueRegex); + public ConfigSection SetNumber(string variable, long value, string? valueRegex = null) + => this with { Config = Config.SetNumber(Section, Subsection, variable, value, valueRegex) }; /// /// Sets the value of a variable in the current section/subsection. @@ -125,7 +137,8 @@ internal ConfigSection(Config config, string section, string? subsection) /// The variable to assign. /// Value to assign to the variable. /// Filter returned entries to those where the value matches the given expression. - public void SetString(string variable, string value, string? valueRegex = null) => Config.SetString(Section, Subsection, variable, value, valueRegex); + public ConfigSection SetString(string variable, string value, string? valueRegex = null) + => this with { Config = Config.SetString(Section, Subsection, variable, value, valueRegex) }; /// /// Tries to retrieve a variable value from configuration. @@ -163,13 +176,15 @@ internal ConfigSection(Config config, string section, string? subsection) /// Removes a variable from the current section/subsection. /// /// The variable to remove. - public void Unset(string variable) => Config.Unset(Section, Subsection, variable); + public ConfigSection Unset(string variable) + => this with { Config = Config.Unset(Section, Subsection, variable) }; /// /// Removes all values from a multi-valued variable from the current section/subsection. /// /// The variable to remove. /// Filter returned entries to those where the value matches the given expression. - public void UnsetAll(string variable, string? valueRegex = null) => Config.UnsetAll(Section, Subsection, variable, valueRegex); + public ConfigSection UnsetAll(string variable, string? valueRegex = null) + => this with { Config = Config.UnsetAll(Section, Subsection, variable, valueRegex) }; } } diff --git a/src/Config/ConfigSectionExtensions.cs b/src/Config/ConfigSectionExtensions.cs index 4c55b13..554919f 100644 --- a/src/Config/ConfigSectionExtensions.cs +++ b/src/Config/ConfigSectionExtensions.cs @@ -16,8 +16,8 @@ public static class ConfigSectionExtensions /// The variable to assign. /// Value add to the variable. /// The configuration level to operate on. - public static void AddBoolean(this ConfigSection config, string variable, bool value, ConfigLevel level) - => config.Config.AddBoolean(config.Section, config.Subsection, variable, value, level); + public static ConfigSection AddBoolean(this ConfigSection config, string variable, bool value, ConfigLevel level) + => config with { Config = config.Config.AddBoolean(config.Section, config.Subsection, variable, value, level) }; /// /// Adds a value to a multi-valued variable in the given section and optional subsection. @@ -26,8 +26,8 @@ public static void AddBoolean(this ConfigSection config, string variable, bool v /// The variable to assign. /// Value add to the variable. /// The configuration level to operate on. - public static void AddDateTime(this ConfigSection config, string variable, DateTime value, ConfigLevel level) - => config.Config.AddDateTime(config.Section, config.Subsection, variable, value, level); + public static ConfigSection AddDateTime(this ConfigSection config, string variable, DateTime value, ConfigLevel level) + => config with { Config = config.Config.AddDateTime(config.Section, config.Subsection, variable, value, level) }; /// /// Adds a value to a multi-valued variable in the given section and optional subsection. @@ -36,8 +36,8 @@ public static void AddDateTime(this ConfigSection config, string variable, DateT /// The variable to assign. /// Value add to the variable. /// The configuration level to operate on. - public static void AddNumber(this ConfigSection config, string variable, long value, ConfigLevel level) - => config.Config.AddNumber(config.Section, config.Subsection, variable, value, level); + public static ConfigSection AddNumber(this ConfigSection config, string variable, long value, ConfigLevel level) + => config with { Config = config.Config.AddNumber(config.Section, config.Subsection, variable, value, level) }; /// /// Adds a value to a multi-valued variable in the given section and optional subsection. @@ -46,8 +46,8 @@ public static void AddNumber(this ConfigSection config, string variable, long va /// The variable to assign. /// Value add to the variable. /// The configuration level to operate on. - public static void AddString(this ConfigSection config, string variable, string value, ConfigLevel level) - => config.Config.AddString(config.Section, config.Subsection, variable, value, level); + public static ConfigSection AddString(this ConfigSection config, string variable, string value, ConfigLevel level) + => config with { Config = config.Config.AddString(config.Section, config.Subsection, variable, value, level) }; /// /// Retrieves a variable value from configuration. @@ -92,8 +92,8 @@ public static void AddString(this ConfigSection config, string variable, string /// The variable to assign. /// Value to assign to the variable. /// The configuration level to operate on. - public static void SetBoolean(this ConfigSection config, string variable, bool value, ConfigLevel level) - => config.Config.SetBoolean(config.Section, config.Subsection, variable, value, null, level); + public static ConfigSection SetBoolean(this ConfigSection config, string variable, bool value, ConfigLevel level) + => config with { Config = config.Config.SetBoolean(config.Section, config.Subsection, variable, value, null, level) }; /// /// Sets the value of a variable in the given section and optional subsection. @@ -103,8 +103,8 @@ public static void SetBoolean(this ConfigSection config, string variable, bool v /// Value to assign to the variable. /// Filter returned entries to those where the value matches the given expression. /// The configuration level to operate on. - public static void SetBoolean(this ConfigSection config, string variable, bool value, string? valueRegex, ConfigLevel level) - => config.Config.SetBoolean(config.Section, config.Subsection, variable, value, valueRegex, level); + public static ConfigSection SetBoolean(this ConfigSection config, string variable, bool value, string? valueRegex, ConfigLevel level) + => config with { Config = config.Config.SetBoolean(config.Section, config.Subsection, variable, value, valueRegex, level) }; /// /// Sets the value of a variable in the given section and optional subsection. @@ -113,8 +113,8 @@ public static void SetBoolean(this ConfigSection config, string variable, bool v /// The variable to assign. /// Value to assign to the variable. /// The configuration level to operate on. - public static void SetDateTime(this ConfigSection config, string variable, DateTime value, ConfigLevel level) - => config.Config.SetDateTime(config.Section, config.Subsection, variable, value, null, level); + public static ConfigSection SetDateTime(this ConfigSection config, string variable, DateTime value, ConfigLevel level) + => config with { Config = config.Config.SetDateTime(config.Section, config.Subsection, variable, value, null, level) }; /// /// Sets the value of a variable in the given section and optional subsection. @@ -124,8 +124,8 @@ public static void SetDateTime(this ConfigSection config, string variable, DateT /// Value to assign to the variable. /// Filter returned entries to those where the value matches the given expression. /// The configuration level to operate on. - public static void SetDateTime(this ConfigSection config, string variable, DateTime value, string? valueRegex, ConfigLevel level) - => config.Config.SetDateTime(config.Section, config.Subsection, variable, value, valueRegex, level); + public static ConfigSection SetDateTime(this ConfigSection config, string variable, DateTime value, string? valueRegex, ConfigLevel level) + => config with { Config = config.Config.SetDateTime(config.Section, config.Subsection, variable, value, valueRegex, level) }; /// /// Sets the value of a variable in the given section and optional subsection. @@ -134,8 +134,8 @@ public static void SetDateTime(this ConfigSection config, string variable, DateT /// The variable to assign. /// Value to assign to the variable. /// The configuration level to operate on. - public static void SetNumber(this ConfigSection config, string variable, long value, ConfigLevel level) - => config.Config.SetNumber(config.Section, config.Subsection, variable, value, null, level); + public static ConfigSection SetNumber(this ConfigSection config, string variable, long value, ConfigLevel level) + => config with { Config = config.Config.SetNumber(config.Section, config.Subsection, variable, value, null, level) }; /// /// Sets the value of a variable in the given section and optional subsection. @@ -145,8 +145,8 @@ public static void SetNumber(this ConfigSection config, string variable, long va /// Value to assign to the variable. /// Filter returned entries to those where the value matches the given expression. /// The configuration level to operate on. - public static void SetNumber(this ConfigSection config, string variable, long value, string? valueRegex, ConfigLevel level) - => config.Config.SetNumber(config.Section, config.Subsection, variable, value, valueRegex, level); + public static ConfigSection SetNumber(this ConfigSection config, string variable, long value, string? valueRegex, ConfigLevel level) + => config with { Config = config.Config.SetNumber(config.Section, config.Subsection, variable, value, valueRegex, level) }; /// /// Sets the value of a variable in the given section and optional subsection. @@ -155,8 +155,8 @@ public static void SetNumber(this ConfigSection config, string variable, long va /// The variable to assign. /// Value to assign to the variable. /// The configuration level to operate on. - public static void SetString(this ConfigSection config, string variable, string value, ConfigLevel level) - => config.Config.SetString(config.Section, config.Subsection, variable, value, null, level); + public static ConfigSection SetString(this ConfigSection config, string variable, string value, ConfigLevel level) + => config with { Config = config.Config.SetString(config.Section, config.Subsection, variable, value, null, level) }; /// /// Sets the value of a variable in the given section and optional subsection. @@ -166,8 +166,8 @@ public static void SetString(this ConfigSection config, string variable, string /// Value to assign to the variable. /// Filter returned entries to those where the value matches the given expression. /// The configuration level to operate on. - public static void SetString(this ConfigSection config, string variable, string value, string? valueRegex, ConfigLevel level) - => config.Config.SetString(config.Section, config.Subsection, variable, value, valueRegex, level); + public static ConfigSection SetString(this ConfigSection config, string variable, string value, string? valueRegex, ConfigLevel level) + => config with { Config = config.Config.SetString(config.Section, config.Subsection, variable, value, valueRegex, level) }; /// /// Sets the value of all matching variables in the given section and optional subsection. @@ -176,8 +176,8 @@ public static void SetString(this ConfigSection config, string variable, string /// The variable to assign. /// Value to assign to the matching variables. /// The configuration level to operate on. - public static void SetAllBoolean(this ConfigSection config, string variable, bool value, ConfigLevel level) - => config.Config.SetAllBoolean(config.Section, config.Subsection, variable, value, null, level); + public static ConfigSection SetAllBoolean(this ConfigSection config, string variable, bool value, ConfigLevel level) + => config with { Config = config.Config.SetAllBoolean(config.Section, config.Subsection, variable, value, null, level) }; /// /// Sets the value of all matching variables in the given section and optional subsection. @@ -187,8 +187,8 @@ public static void SetAllBoolean(this ConfigSection config, string variable, boo /// Value to assign to the matching variables. /// Filter returned entries to those where the value matches the given expression. /// The configuration level to operate on. - public static void SetAllBoolean(this ConfigSection config, string variable, bool value, string? valueRegex, ConfigLevel level) - => config.Config.SetAllBoolean(config.Section, config.Subsection, variable, value, valueRegex, level); + public static ConfigSection SetAllBoolean(this ConfigSection config, string variable, bool value, string? valueRegex, ConfigLevel level) + => config with { Config = config.Config.SetAllBoolean(config.Section, config.Subsection, variable, value, valueRegex, level) }; /// /// Sets the value of all matching variables in the given section and optional subsection. @@ -197,8 +197,8 @@ public static void SetAllBoolean(this ConfigSection config, string variable, boo /// The variable to assign. /// Value to assign to the matching variables. /// The configuration level to operate on. - public static void SetAllDateTime(this ConfigSection config, string variable, DateTime value, ConfigLevel level) - => config.Config.SetAllDateTime(config.Section, config.Subsection, variable, value, null, level); + public static ConfigSection SetAllDateTime(this ConfigSection config, string variable, DateTime value, ConfigLevel level) + => config with { Config = config.Config.SetAllDateTime(config.Section, config.Subsection, variable, value, null, level) }; /// /// Sets the value of all matching variables in the given section and optional subsection. @@ -208,8 +208,8 @@ public static void SetAllDateTime(this ConfigSection config, string variable, Da /// Value to assign to the matching variables. /// Filter returned entries to those where the value matches the given expression. /// The configuration level to operate on. - public static void SetAllDateTime(this ConfigSection config, string variable, DateTime value, string? valueRegex, ConfigLevel level) - => config.Config.SetAllDateTime(config.Section, config.Subsection, variable, value, valueRegex, level); + public static ConfigSection SetAllDateTime(this ConfigSection config, string variable, DateTime value, string? valueRegex, ConfigLevel level) + => config with { Config = config.Config.SetAllDateTime(config.Section, config.Subsection, variable, value, valueRegex, level) }; /// /// Sets the value of all matching variables in the given section and optional subsection. @@ -218,8 +218,8 @@ public static void SetAllDateTime(this ConfigSection config, string variable, Da /// The variable to assign. /// Value to assign to the matching variables. /// The configuration level to operate on. - public static void SetAllNumber(this ConfigSection config, string variable, long value, ConfigLevel level) - => config.Config.SetAllNumber(config.Section, config.Subsection, variable, value, null, level); + public static ConfigSection SetAllNumber(this ConfigSection config, string variable, long value, ConfigLevel level) + => config with { Config = config.Config.SetAllNumber(config.Section, config.Subsection, variable, value, null, level) }; /// /// Sets the value of all matching variables in the given section and optional subsection. @@ -229,8 +229,8 @@ public static void SetAllNumber(this ConfigSection config, string variable, long /// Value to assign to the matching variables. /// Filter returned entries to those where the value matches the given expression. /// The configuration level to operate on. - public static void SetAllNumber(this ConfigSection config, string variable, long value, string? valueRegex, ConfigLevel level) - => config.Config.SetAllNumber(config.Section, config.Subsection, variable, value, valueRegex, level); + public static ConfigSection SetAllNumber(this ConfigSection config, string variable, long value, string? valueRegex, ConfigLevel level) + => config with { Config = config.Config.SetAllNumber(config.Section, config.Subsection, variable, value, valueRegex, level) }; /// /// Sets the value of all matching variables in the given section and optional subsection. @@ -239,8 +239,8 @@ public static void SetAllNumber(this ConfigSection config, string variable, long /// The variable to assign. /// Value to assign to the matching variables. /// The configuration level to operate on. - public static void SetAllString(this ConfigSection config, string variable, string value, ConfigLevel level) - => config.Config.SetAllString(config.Section, config.Subsection, variable, value, null, level); + public static ConfigSection SetAllString(this ConfigSection config, string variable, string value, ConfigLevel level) + => config with { Config = config.Config.SetAllString(config.Section, config.Subsection, variable, value, null, level) }; /// /// Sets the value of all matching variables in the given section and optional subsection. @@ -250,8 +250,8 @@ public static void SetAllString(this ConfigSection config, string variable, stri /// Value to assign to the matching variables. /// Filter returned entries to those where the value matches the given expression. /// The configuration level to operate on. - public static void SetAllString(this ConfigSection config, string variable, string value, string? valueRegex, ConfigLevel level) - => config.Config.SetAllString(config.Section, config.Subsection, variable, value, valueRegex, level); + public static ConfigSection SetAllString(this ConfigSection config, string variable, string value, string? valueRegex, ConfigLevel level) + => config with { Config = config.Config.SetAllString(config.Section, config.Subsection, variable, value, valueRegex, level) }; /// /// Removes a variable from the given section and optional subsection. @@ -259,8 +259,8 @@ public static void SetAllString(this ConfigSection config, string variable, stri /// The configuration section to operate on. /// The variable to remove. /// The configuration level to operate on. - public static void Unset(this ConfigSection config, string variable, ConfigLevel level) - => config.Config.Unset(config.Section, config.Subsection, variable, level); + public static ConfigSection Unset(this ConfigSection config, string variable, ConfigLevel level) + => config with { Config = config.Config.Unset(config.Section, config.Subsection, variable, level) }; /// /// Removes all values from a multi-valued variable from the given section and optional subsection. @@ -268,8 +268,8 @@ public static void Unset(this ConfigSection config, string variable, ConfigLevel /// The configuration section to operate on. /// The variable to remove. /// The configuration level to operate on. - public static void UnsetAll(this ConfigSection config, string variable, ConfigLevel level) - => config.Config.UnsetAll(config.Section, config.Subsection, variable, null, level); + public static ConfigSection UnsetAll(this ConfigSection config, string variable, ConfigLevel level) + => config with { Config = config.Config.UnsetAll(config.Section, config.Subsection, variable, null, level) }; /// /// Removes all values from a multi-valued variable from the given section and optional subsection. @@ -278,7 +278,7 @@ public static void UnsetAll(this ConfigSection config, string variable, ConfigLe /// The variable to remove. /// Filter returned entries to those where the value matches the given expression. /// The configuration level to operate on. - public static void UnsetAll(this ConfigSection config, string variable, string? valueRegex, ConfigLevel level) - => config.Config.UnsetAll(config.Section, config.Subsection, variable, valueRegex, level); + public static ConfigSection UnsetAll(this ConfigSection config, string variable, string? valueRegex, ConfigLevel level) + => config with { Config = config.Config.UnsetAll(config.Section, config.Subsection, variable, valueRegex, level) }; } }