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