From 6e10ae885c354ff48c0550e2a0232a1c5e6d8f53 Mon Sep 17 00:00:00 2001 From: Michael Tyson Date: Sun, 27 Oct 2019 13:30:28 -0400 Subject: [PATCH] WIP - Intial concept --- Fig.Core/CompositeSettingsDictionary.cs | 12 ++++-- Fig.Core/Fig.Core.csproj | 3 ++ Fig.Core/Settings.cs | 55 +++++++++++++++++++++++++ Fig.Tests/UntypedSettingsTests.cs | 35 ++++++++++++++++ 4 files changed, 102 insertions(+), 3 deletions(-) diff --git a/Fig.Core/CompositeSettingsDictionary.cs b/Fig.Core/CompositeSettingsDictionary.cs index 7226c18..426d6c7 100644 --- a/Fig.Core/CompositeSettingsDictionary.cs +++ b/Fig.Core/CompositeSettingsDictionary.cs @@ -1,3 +1,4 @@ +using System.Linq; using System; using System.Collections.Generic; using System.Text.RegularExpressions; @@ -17,7 +18,10 @@ public CompositeSettingsDictionary() _dictionaries = new List(); } - public void Add(SettingsDictionary settingsDictionary) + public IEnumerable Keys + => _dictionaries.Cast>().SelectMany(d => d.Keys); + + public void Add(SettingsDictionary settingsDictionary) => _dictionaries.Add(settingsDictionary); public bool TryGetValue(string key, string env, out string value) @@ -37,7 +41,7 @@ public bool TryGetValue(string key, string env, out string value) } /// - /// Look for ${key} and replace with values from the dictionary + /// Look for ${key} and replace with values from the dictionary /// /// The string to expand /// A specific configuration to use if key is unqualified @@ -48,13 +52,15 @@ public string ExpandVariables(string template, string configuration = null) var regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); return regex.Replace(template, m => GetCurrentEnvironment(m, configuration)); } + private string GetCurrentEnvironment(Match m, string configuration) { - var key = m.Groups["key"]. Value; + var key = m.Groups["key"].Value; var env = m.Groups["env"].Success ? m.Groups["env"].Value : configuration; TryGetValue(key, env, out var result); return result; } + private bool ConcatIndices(SettingsDictionary sd, string key, out string value) { var indices = new List(); diff --git a/Fig.Core/Fig.Core.csproj b/Fig.Core/Fig.Core.csproj index 229cb48..c54db00 100644 --- a/Fig.Core/Fig.Core.csproj +++ b/Fig.Core/Fig.Core.csproj @@ -17,4 +17,7 @@ + + + diff --git a/Fig.Core/Settings.cs b/Fig.Core/Settings.cs index 7174e36..e051597 100644 --- a/Fig.Core/Settings.cs +++ b/Fig.Core/Settings.cs @@ -1,9 +1,11 @@ +using System.Text; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; +using ConsoleTables; namespace Fig { @@ -105,6 +107,59 @@ public Settings(string bindingPath = null, IStringConverter converter = null) Environment = ""; } + /// + /// Diagnostic report as string representation of current configuration and source for each property. + /// + public string Report() + { + var reportBuilder = new StringBuilder( + $"[{this.GetType().Name} Diagnostic Report]{System.Environment.NewLine}{System.Environment.NewLine}"); + var keyTrackingValues = new Dictionary(); + var defaultKeyValuePair = new KeyValuePair(); + + foreach (var k in SettingsDictionary.Keys) + { + var keyEnvironment = k.Contains(":") + ? k.Split(':').Skip(1).LastOrDefault() ?? String.Empty + : String.Empty; + var key = (keyEnvironment == String.Empty) + ? k + : k.Substring(0, k.Length - keyEnvironment.Length - 1); + + if (SettingsDictionary.TryGetValue(key, Environment, out var value)) + { + var environment = (keyEnvironment == Environment) ? keyEnvironment : String.Empty; + var foundKvp = keyTrackingValues.FirstOrDefault((_k) => _k.Key.ToUpper() == key.ToUpper()); + + if (!foundKvp.Equals(defaultKeyValuePair)) + { + var (trackedEnviroment, trackedValue) = foundKvp.Value; + + if (trackedEnviroment == String.Empty && environment != String.Empty) + { + keyTrackingValues[foundKvp.Key] = (environment, trackedValue); + } + + continue; + } + + keyTrackingValues.Add(key, (environment, value)); + } + } + + var table = new ConsoleTable("Key", "Value", "Enviroment"); + + foreach (var kvp in keyTrackingValues) + { + var (environment, value) = kvp.Value; + table.AddRow(kvp.Key, value, environment); + } + + reportBuilder.Append(table.ToString()); + + return reportBuilder.ToString(); + } + private string GetBindingPath() { //Untyped Settings class should bind to the root of the tree diff --git a/Fig.Tests/UntypedSettingsTests.cs b/Fig.Tests/UntypedSettingsTests.cs index 52fed76..fe92390 100644 --- a/Fig.Tests/UntypedSettingsTests.cs +++ b/Fig.Tests/UntypedSettingsTests.cs @@ -1,3 +1,5 @@ +using System.Linq; +using System.Diagnostics; using System; using System.Collections.Generic; using NUnit.Framework; @@ -75,5 +77,38 @@ public void DefaultKicksInWhenSettingsIsMissing() var actual = _settings.Get("missing", () => expected); Assert.AreEqual(actual, expected); } + + [Test] + public void WhenReportReturnsValue() + { + var report = this._settings.Report(); + var hasValue = !String.IsNullOrWhiteSpace(report); + + Assert.IsTrue(hasValue); + } + + [Test] + public void WhenEnvironmentAndReportReturnsValue() + { + _settings.SetEnvironment("test"); + var report = this._settings.Report(); + var hasValue = report + .Split(System.Environment.NewLine) + .Any(l => l.Contains("| a.b | a.b:test | test |")); + + Assert.IsTrue(hasValue); + } + + [Test] + public void WhenNoEnvironmentAndReportReturnsValue() + { + var report = this._settings.Report(); + var hasValue = report + .Split(System.Environment.NewLine) + .Any(l => l.Contains("| a.b | a.b | |")); + + Assert.IsTrue(hasValue); + } + } } \ No newline at end of file