Skip to content

Commit

Permalink
Add final missing immutable facade, ConfigSection
Browse files Browse the repository at this point in the history
Fixes #61
  • Loading branch information
kzu committed Apr 27, 2021
1 parent d1cb1f3 commit d3c4069
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 63 deletions.
49 changes: 32 additions & 17 deletions src/Config/ConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace DotNetConfig
/// <summary>
/// Provides access to a specific section and optional subsection.
/// </summary>
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; }

Expand All @@ -23,36 +23,41 @@ internal ConfigSection(Config config, string section, string? subsection)
/// </summary>
/// <param name="variable">The variable to assign.</param>
/// <param name="value">Value add to the variable.</param>
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) };

/// <summary>
/// Adds a value to a multi-valued variable in the current section/subsection.
/// </summary>
/// <param name="variable">The variable to assign.</param>
/// <param name="value">Value add to the variable.</param>
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) };

/// <summary>
/// Adds a value to a multi-valued variable in the current section/subsection.
/// </summary>
/// <param name="variable">The variable to assign.</param>
/// <param name="value">Value add to the variable.</param>
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) };

/// <summary>
/// Adds a value to a multi-valued variable in the current section/subsection.
/// </summary>
/// <param name="variable">The variable to assign.</param>
/// <param name="value">Value add to the variable.</param>
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) };

/// <summary>
/// Gets all values from a multi-valued variable from the current section/subsection,
/// which optionally match the given value regular expression.
/// </summary>
/// <param name="variable">The variable to remove.</param>
/// <param name="valueRegex">Filter returned entries to those where the value matches the given expression.</param>
public IEnumerable<ConfigEntry> GetAll(string variable, string? valueRegex = null) => Config.GetAll(Section, Subsection, variable, valueRegex);
public IEnumerable<ConfigEntry> GetAll(string variable, string? valueRegex = null)
=> Config.GetAll(Section, Subsection, variable, valueRegex);

/// <summary>
/// Gets a string variable and applies path normalization to it, resolving
Expand All @@ -69,63 +74,71 @@ internal ConfigSection(Config config, string section, string? subsection)
/// <param name="variable">The variable to assign.</param>
/// <param name="value">Value to assign to the matching variables.</param>
/// <param name="valueRegex">Filter returned entries to those where the value matches the given expression.</param>
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) };

/// <summary>
/// Sets the value of all matching variables in the current section/subsection.
/// </summary>
/// <param name="variable">The variable to assign.</param>
/// <param name="value">Value to assign to the matching variables.</param>
/// <param name="valueRegex">Filter returned entries to those where the value matches the given expression.</param>
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) };

/// <summary>
/// Sets the value of all matching variables in the current section/subsection.
/// </summary>
/// <param name="variable">The variable to assign.</param>
/// <param name="value">Value to assign to the matching variables.</param>
/// <param name="valueRegex">Filter returned entries to those where the value matches the given expression.</param>
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) };

/// <summary>
/// Sets the value of all matching variables in the current section/subsection.
/// </summary>
/// <param name="variable">The variable to assign.</param>
/// <param name="value">Value to assign to the matching variables.</param>
/// <param name="valueRegex">Filter returned entries to those where the value matches the given expression.</param>
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) };

/// <summary>
/// Sets the value of a variable in the current section/subsection.
/// </summary>
/// <param name="variable">The variable to assign.</param>
/// <param name="value">Value to assign to the variable.</param>
/// <param name="valueRegex">Filter returned entries to those where the value matches the given expression.</param>
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) };

/// <summary>
/// Sets the value of a variable in the current section/subsection.
/// </summary>
/// <param name="variable">The variable to assign.</param>
/// <param name="value">Value to assign to the variable.</param>
/// <param name="valueRegex">Filter returned entries to those where the value matches the given expression.</param>
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) };

/// <summary>
/// Sets the value of a variable in the current section/subsection.
/// </summary>
/// <param name="variable">The variable to assign.</param>
/// <param name="value">Value to assign to the variable.</param>
/// <param name="valueRegex">Filter returned entries to those where the value matches the given expression.</param>
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) };

/// <summary>
/// Sets the value of a variable in the current section/subsection.
/// </summary>
/// <param name="variable">The variable to assign.</param>
/// <param name="value">Value to assign to the variable.</param>
/// <param name="valueRegex">Filter returned entries to those where the value matches the given expression.</param>
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) };

/// <summary>
/// Tries to retrieve a variable value from configuration.
Expand Down Expand Up @@ -163,13 +176,15 @@ internal ConfigSection(Config config, string section, string? subsection)
/// Removes a variable from the current section/subsection.
/// </summary>
/// <param name="variable">The variable to remove.</param>
public void Unset(string variable) => Config.Unset(Section, Subsection, variable);
public ConfigSection Unset(string variable)
=> this with { Config = Config.Unset(Section, Subsection, variable) };

/// <summary>
/// Removes all values from a multi-valued variable from the current section/subsection.
/// </summary>
/// <param name="variable">The variable to remove.</param>
/// <param name="valueRegex">Filter returned entries to those where the value matches the given expression.</param>
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) };
}
}
Loading

0 comments on commit d3c4069

Please sign in to comment.