Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Renames
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoK committed Aug 1, 2016
1 parent 9f51d15 commit 8084ac0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ public static string GetConnectionString(this IConfiguration configuration, stri
/// </summary>
/// <param name="configuration">The <see cref="IConfiguration"/> to enumerate.</param>
/// <returns>An enumeration of key value pairs.</returns>
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this IConfiguration configuration) => configuration.AsEnumerable(removePathFromChildKeys: false);
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this IConfiguration configuration) => configuration.AsEnumerable(makePathsRelative: false);

/// <summary>
/// Get the enumeration of key value pairs within the <see cref="IConfiguration" />
/// </summary>
/// <param name="configuration">The <see cref="IConfiguration"/> to enumerate.</param>
/// <param name="removePathFromChildKeys">If true, removes the configuration path from the child keys.</param>
/// <param name="makePathsRelative">If true, the child keys returned will have the current configuration's Path trimmed from the front.</param>
/// <returns>An enumeration of key value pairs.</returns>
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this IConfiguration configuration, bool removePathFromChildKeys)
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this IConfiguration configuration, bool makePathsRelative)
{
var stack = new Stack<IConfiguration>();
stack.Push(configuration);
var rootSection = configuration as IConfigurationSection;
var prefixLength = (removePathFromChildKeys && rootSection != null) ? rootSection.Path.Length + 1 : 0;
var prefixLength = (makePathsRelative && rootSection != null) ? rootSection.Path.Length + 1 : 0;
while (stack.Count > 0)
{
var config = stack.Pop();
var section = config as IConfigurationSection;
// Don't include the sections value if we are removing paths, since it will be an empty key
if (section != null && (!removePathFromChildKeys || config != configuration))
if (section != null && (!makePathsRelative || config != configuration))
{
yield return new KeyValuePair<string, string>(section.Path.Substring(prefixLength), section.Value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void AsEnumerateFlattensIntoDictionaryTest(bool removePath)
configurationBuilder.Add(memConfigSrc2);
configurationBuilder.Add(memConfigSrc3);
var config = configurationBuilder.Build();
var dict = config.AsEnumerable(removePathFromChildKeys: removePath).ToDictionary(k => k.Key, v => v.Value);
var dict = config.AsEnumerable(makePathsRelative: removePath).ToDictionary(k => k.Key, v => v.Value);

// Assert
Assert.Equal("Value1", dict["Mem1"]);
Expand Down Expand Up @@ -153,19 +153,19 @@ public void AsEnumerateStripsKeyFromChildren()

var config = configurationBuilder.Build();

var dict = config.GetSection("Mem1").AsEnumerable(removePathFromChildKeys: true).ToDictionary(k => k.Key, v => v.Value);
var dict = config.GetSection("Mem1").AsEnumerable(makePathsRelative: true).ToDictionary(k => k.Key, v => v.Value);
Assert.Equal(3, dict.Count);
Assert.Equal("NoKeyValue1", dict[""]);
Assert.Equal("ValueInMem1", dict["KeyInMem1"]);
Assert.Equal("ValueDeep1", dict["KeyInMem1:Deep1"]);

var dict2 = config.GetSection("Mem2").AsEnumerable(removePathFromChildKeys: true).ToDictionary(k => k.Key, v => v.Value);
var dict2 = config.GetSection("Mem2").AsEnumerable(makePathsRelative: true).ToDictionary(k => k.Key, v => v.Value);
Assert.Equal(3, dict2.Count);
Assert.Equal("NoKeyValue2", dict2[""]);
Assert.Equal("ValueInMem2", dict2["KeyInMem2"]);
Assert.Equal("ValueDeep2", dict2["KeyInMem2:Deep2"]);

var dict3 = config.GetSection("Mem3").AsEnumerable(removePathFromChildKeys: true).ToDictionary(k => k.Key, v => v.Value);
var dict3 = config.GetSection("Mem3").AsEnumerable(makePathsRelative: true).ToDictionary(k => k.Key, v => v.Value);
Assert.Equal(5, dict3.Count);
Assert.Equal("NoKeyValue3", dict3[""]);
Assert.Equal("ValueInMem3", dict3["KeyInMem3"]);
Expand Down

0 comments on commit 8084ac0

Please sign in to comment.