-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathChainedConfigurationProvider.cs
94 lines (84 loc) · 3.63 KB
/
ChainedConfigurationProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
namespace OmniSharp.Extensions.DebugAdapter.Client.Configuration
{
/// <summary>
/// Chained implementation of <see cref="IConfigurationProvider"/>
/// </summary>
internal class ChainedConfigurationProvider : IConfigurationProvider, IDisposable
{
private readonly IConfiguration _config;
private readonly bool _shouldDisposeConfig;
/// <summary>
/// Initialize a new instance from the source configuration.
/// </summary>
/// <param name="source">The source configuration.</param>
public ChainedConfigurationProvider(ChainedConfigurationSource source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
_config = source.Configuration ?? throw new ArgumentException(nameof(source));
_shouldDisposeConfig = source.ShouldDisposeConfiguration;
}
/// <summary>
/// Tries to get a configuration value for the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <returns><c>True</c> if a value for the specified key was found, otherwise <c>false</c>.</returns>
public bool TryGet(string key, out string value)
{
value = _config[key];
return !string.IsNullOrEmpty(value);
}
/// <summary>
/// Sets a configuration value for the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public void Set(string key, string value) => _config[key] = value;
/// <summary>
/// Returns a change token if this provider supports change tracking, null otherwise.
/// </summary>
/// <returns>The change token.</returns>
public IChangeToken GetReloadToken() => _config.GetReloadToken();
/// <summary>
/// Loads configuration values from the source represented by this <see cref="IConfigurationProvider"/>.
/// </summary>
public void Load() { }
/// <summary>
/// Returns the immediate descendant configuration keys for a given parent path based on this
/// <see cref="IConfigurationProvider"/>s data and the set of keys returned by all the preceding
/// <see cref="IConfigurationProvider"/>s.
/// </summary>
/// <param name="earlierKeys">The child keys returned by the preceding providers for the same parent path.</param>
/// <param name="parentPath">The parent path.</param>
/// <returns>The child keys.</returns>
public IEnumerable<string> GetChildKeys(
IEnumerable<string> earlierKeys,
string? parentPath)
{
IConfiguration section = parentPath == null ? _config : _config.GetSection(parentPath);
var children = section.GetChildren();
var keys = new List<string>();
keys.AddRange(children.Select(c => c.Key));
return keys.Concat(earlierKeys)
.OrderBy(k => k, ConfigurationKeyComparer.Instance);
}
/// <inheritdoc />
public void Dispose()
{
if (_shouldDisposeConfig)
{
(_config as IDisposable)?.Dispose();
}
}
}
}