Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Fig.Core/CompositeSettingsDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
Expand All @@ -17,7 +18,10 @@ public CompositeSettingsDictionary()
_dictionaries = new List<SettingsDictionary>();
}

public void Add(SettingsDictionary settingsDictionary)
public IEnumerable<string> Keys
=> _dictionaries.Cast<IDictionary<string, string>>().SelectMany(d => d.Keys);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cast is redundant


public void Add(SettingsDictionary settingsDictionary)
=> _dictionaries.Add(settingsDictionary);

public bool TryGetValue(string key, string env, out string value)
Expand All @@ -37,7 +41,7 @@ public bool TryGetValue(string key, string env, out string value)
}

/// <summary>
/// Look for ${key} and replace with values from the dictionary
/// Look for ${key} and replace with values from the dictionary
/// </summary>
/// <param name="template">The string to expand</param>
/// <param name="configuration">A specific configuration to use if key is unqualified</param>
Expand All @@ -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<string>();
Expand Down
3 changes: 3 additions & 0 deletions Fig.Core/Fig.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ConsoleTables" Version="2.3.0" />
</ItemGroup>
</Project>
55 changes: 55 additions & 0 deletions Fig.Core/Settings.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -105,6 +107,59 @@ public Settings(string bindingPath = null, IStringConverter converter = null)
Environment = "";
}

/// <summary>
/// Diagnostic report as string representation of current configuration and source for each property.
/// </summary>
public string Report()
{
var reportBuilder = new StringBuilder(
$"[{this.GetType().Name} Diagnostic Report]{System.Environment.NewLine}{System.Environment.NewLine}");
var keyTrackingValues = new Dictionary<string, (string, string)>();
var defaultKeyValuePair = new KeyValuePair<string, (string, string)>();

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
Expand Down
35 changes: 35 additions & 0 deletions Fig.Tests/UntypedSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq;
using System.Diagnostics;
using System;
using System.Collections.Generic;
using NUnit.Framework;
Expand Down Expand Up @@ -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);
}

}
}