Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove most LINQ usage from Microsoft.Extensions.Configuration #44825

Merged
merged 1 commit into from
Nov 18, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Extensions.Configuration
Expand Down Expand Up @@ -78,11 +77,14 @@ public IEnumerable<string> GetChildKeys(
string parentPath)
{
IConfiguration section = parentPath == null ? _config : _config.GetSection(parentPath);
IEnumerable<IConfigurationSection> children = section.GetChildren();
var keys = new List<string>();
keys.AddRange(children.Select(c => c.Key));
return keys.Concat(earlierKeys)
.OrderBy(k => k, ConfigurationKeyComparer.Instance);
foreach (IConfigurationSection child in section.GetChildren())
{
keys.Add(child.Key);
}
keys.AddRange(earlierKeys);
keys.Sort(ConfigurationKeyComparer.Comparison);
return keys;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class ConfigurationKeyComparer : IComparer<string>
/// </summary>
public static ConfigurationKeyComparer Instance { get; } = new ConfigurationKeyComparer();

/// <summary>A comparer delegate with the default instance.</summary>
internal static Comparison<string> Comparison { get; } = Instance.Compare;

/// <summary>
/// Compares two strings.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Threading;
using Microsoft.Extensions.Primitives;

Expand Down Expand Up @@ -62,13 +62,35 @@ public virtual IEnumerable<string> GetChildKeys(
IEnumerable<string> earlierKeys,
string parentPath)
{
string prefix = parentPath == null ? string.Empty : parentPath + ConfigurationPath.KeyDelimiter;
var results = new List<string>();

return Data
.Where(kv => kv.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
.Select(kv => Segment(kv.Key, prefix.Length))
.Concat(earlierKeys)
.OrderBy(k => k, ConfigurationKeyComparer.Instance);
if (parentPath is null)
{
foreach (KeyValuePair<string, string> kv in Data)
{
results.Add(Segment(kv.Key, 0));
}
}
else
{
Debug.Assert(ConfigurationPath.KeyDelimiter == ":");

foreach (KeyValuePair<string, string> kv in Data)
{
if (kv.Key.Length > parentPath.Length &&
kv.Key.StartsWith(parentPath, StringComparison.OrdinalIgnoreCase) &&
kv.Key[parentPath.Length] == ':')
{
results.Add(Segment(kv.Key, parentPath.Length + 1));
}
}
}

results.AddRange(earlierKeys);

results.Sort(ConfigurationKeyComparer.Comparison);

return results;
}

private static string Segment(string key, int prefixLength)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.Extensions.Primitives;

Expand Down Expand Up @@ -66,7 +65,7 @@ public string this[string key]
}
set
{
if (!_providers.Any())
if (_providers.Count == 0)
{
throw new InvalidOperationException(SR.Error_NoSources);
}
Expand Down