/// <summary>
        /// Pulls the config from the provider, using the path passed in.
        /// </summary>
        /// <param name="keyList">The key list built with the unique keys.</param>
        /// <param name="provider">The provider to build config from.</param>
        /// <param name="path">The path to find the config for.</param>
        /// <returns>List&lt;System.String&gt; of all config keys.</returns>
        private static List <string> GetKeyNames(List <string> keyList, IConfigurationProvider provider, string path)
        {
            // Grab distinct keys to parse for children.
            var distinctKeys = provider.GetChildKeys(new List <string>(), path).Distinct();

            foreach (var key in distinctKeys)
            {
                // Full path of key.
                var fullPath = GetFullKeyPath(path, key);

                // If there are children of this config node, then recursively call this method again, otherwise add to key path.
                var hasChildren = provider.GetChildKeys(new List <string>(), fullPath).Any();

                if (hasChildren)
                {
                    AddChildKeys(keyList, provider, fullPath);
                }
                else
                {
                    keyList.Add(fullPath);
                }
            }

            return(keyList);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Pulls the config from the provider, using the path passed in.
        /// </summary>
        /// <param name="keyList">The key list built with the unique keys.</param>
        /// <param name="provider">The provider to build config from.</param>
        /// <param name="path">The path to find the config for.</param>
        /// <returns>List&lt;System.String&gt; of all config keys.</returns>
        private static List <string> GetKeyNames(List <string> keyList, IConfigurationProvider provider, string path)
        {
            // Grab distinct keys to parse for children.
            var distinctKeys = provider.GetChildKeys(new List <string>(), path).Distinct();

            foreach (var key in distinctKeys)
            {
                // Full path of key.
                var newPath = (string.IsNullOrEmpty(path) ? null : path) +
                              (!string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(key) ? ":" : null) +
                              (string.IsNullOrEmpty(key) ? null : key);

                // If there are children of this config node, then recursively call this method again, otherwise add to key path.
                var hasChildren = provider.GetChildKeys(new List <string>(), newPath).Any();
                if (hasChildren)
                {
                    AddKeys(keyList, newPath, provider);
                }
                else
                {
                    keyList.Add(newPath);
                }
            }

            return(keyList);
        }
Ejemplo n.º 3
0
        public void Setup()
        {
            // Of course we should test these first but I'm too lazy.
            IConfigurationSource   source   = new DiConfigurationSource(@"Server=MYLLY; User Id=ConfigReader; Password=ConfigReader;", "TEST");
            IConfigurationProvider provider = source.Build(null);

            provider.Load();

            // Mock these objects.
            var collection = new Mock <ServiceCollectionWrapper>();

            // XXX : This is a mess. Unmess this sometime.
            // We load configuration provided by our DiConfigurationProvider (using DiConfigurationSource as source)
            // and return configurations to caller.
            var diConfiguration = new Mock <IConfigurationSection>();

            diConfiguration.Setup(f => f.GetChildren())
            .Returns(() =>
            {
                return(provider.GetChildKeys(Enumerable.Empty <string>(), "DiConfiguration").Distinct().Select(parentKey =>
                {
                    var mock = new Mock <IConfigurationSection>();
                    mock.Setup(f => f.Key).Returns(parentKey);
                    mock.Setup(f => f.GetChildren())
                    .Returns(() => provider.GetChildKeys(Enumerable.Empty <string>(), "DiConfiguration:" + parentKey).Distinct().Select(childKey =>
                    {
                        var mock = new Mock <IConfigurationSection>();
                        mock.Setup(f => f.GetSection(It.IsNotNull <string>())).Returns((string section) =>
                        {
                            var mock = new Mock <IConfigurationSection>();
                            mock.Setup(f => f.Value).Returns(() => provider.TryGet($"DiConfiguration:{parentKey}:{childKey}:{section}", out string value) ? value : "");
                            return mock.Object;
                        });
                        return mock.Object;
                    }));
                    return mock.Object;
                }));
            });

            // Main/head configuration. Returns only DI configuration mockup.
            var configuration = new Mock <IConfiguration>();

            configuration.Setup(f => f.GetSection(It.IsAny <string>()))
            .Returns(diConfiguration.Object);

            // Builds DI services and fetches the one and only service we actually need to run the tests.
            // DiServiceBuilder should be tested also before calling it.
            DiServiceBuilder.AddDiServices(collection.Object, configuration.Object);
            carSalesman = collection.Object.GetService(typeof(ICarSalesman)) as ICarSalesman;
        }
        private IEnumerable <string> Keys(string parentPath)
        {
            var prefix = parentPath == null
                    ? string.Empty
                    : parentPath + ConfigurationPath.KeyDelimiter;

            var childKeys = provider
                            .GetChildKeys(Enumerable.Empty <string>(), parentPath)
                            .Distinct()
                            .Select(k => prefix + k).ToList();

            if (childKeys.Any())
            {
                var keys = new List <string>();
                foreach (var key in childKeys)
                {
                    keys.AddRange(Keys(key));
                }
                return(keys);
            }
            else if (string.IsNullOrEmpty(parentPath))
            {
                return(Enumerable.Empty <string>());
            }
            else
            {
                return(new[] { parentPath });
            }
        }
        /// <summary>
        /// Получение списка ключей для <see cref="IConfigurationProvider"/>
        /// </summary>
        /// <param name="configurationProvider">Провайдер конфигурации.</param>
        /// <returns>Список ключей.</returns>
        public static string[] GetKeys(this IConfigurationProvider configurationProvider)
        {
            var keys      = new List <string>();
            var childKeys = configurationProvider.GetChildKeys(Enumerable.Empty <string>(), null).Distinct().ToArray();

            keys.AddRange(childKeys);
            AddKeys(keys, configurationProvider, childKeys);
            return(keys.ToArray());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets the distinct section names (top-level keys) for which the given <see cref="IConfigurationProvider"/> provides configuration data.
        /// </summary>
        /// <param name="provider">The <see cref="IConfigurationProvider"/> instance from which to get section names.</param>
        /// <returns>An array of the section names (top-level keys) for which the given <see cref="IConfigurationProvider"/>
        /// provides configuration.</returns>
        internal static string[] GetSectionNames(this IConfigurationProvider provider)
        {
            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            return(provider.GetChildKeys(Enumerable.Empty <string>(), null).Distinct().ToArray());
        }
        /// <summary> Appends the keys in a provider below the root key specified. </summary>
        /// <param name="provider"> The provider. </param>
        /// <param name="rootKey"> The root key. </param>
        /// <param name="builder"> The builder to which to append the keys. </param>
        /// <param name="path"> The path to the current key (can be empty if root). </param>
        private static void AppendKeys(IConfigurationProvider provider, string?rootKey, StringBuilder builder, string path)
        {
            List <string> keys = string.IsNullOrWhiteSpace(path)
            ? provider.GetChildKeys(Enumerable.Empty <string>(), rootKey).ToList()
            : provider.GetChildKeys(Enumerable.Empty <string>(), path).ToList();

            if (keys.Count <= 0)
            {
                builder.Append("    ");

                builder.AppendLine(!string.IsNullOrWhiteSpace(path) ? path : rootKey);

                return;
            }

            foreach (var key in keys.Distinct())
            {
                AppendKeys(provider, key, builder, !string.IsNullOrWhiteSpace(path) ? string.Concat(path, ":", key) : key);
            }
        }
 private static void AddKeys(List <string> keys, IConfigurationProvider configurationProvider, IEnumerable <string> parentKeys)
 {
     foreach (var parentPath in parentKeys)
     {
         var childKeys     = configurationProvider.GetChildKeys(Enumerable.Empty <string>(), parentPath).Distinct();
         var fullChildKeys = childKeys.Select(s => ConfigurationPath.Combine(parentPath, s)).ToArray();
         if (fullChildKeys.Length > 0)
         {
             keys.AddRange(fullChildKeys);
             AddKeys(keys, configurationProvider, fullChildKeys);
         }
     }
 }
        private List <string> GetAllKeys(IConfigurationProvider provider, string parentPath, List <string> initialKeys)
        {
            foreach (var key in provider.GetChildKeys(Enumerable.Empty <string>(), parentPath).Distinct())
            {
                var currentKey = string.IsNullOrEmpty(parentPath) ? key : $"{parentPath}:{key}";
                GetAllKeys(provider, currentKey, initialKeys);

                if (!initialKeys.Any(k => k.StartsWith(currentKey)))
                {
                    initialKeys.Add(currentKey);
                }
            }
            return(initialKeys);
        }
Ejemplo n.º 10
0
        private static IEnumerable <string> GetProviderKeys(IConfigurationProvider provider, string parentPath)
        {
            string prefix = parentPath == null
                        ? string.Empty
                        : parentPath + ConfigurationPath.KeyDelimiter;

            List <string> keys      = new List <string>();
            List <string> childKeys = provider.GetChildKeys(Enumerable.Empty <string>(), parentPath)
                                      .Distinct()
                                      .Select(k => prefix + k).ToList();

            keys.AddRange(childKeys);
            foreach (var key in childKeys)
            {
                keys.AddRange(GetProviderKeys(provider, key));
            }
            return(keys);
        }
Ejemplo n.º 11
0
        private HashSet <string> GetFullKeyNames(IConfigurationProvider provider, string rootKey, HashSet <string> initialKeys)
        {
            foreach (var key in provider.GetChildKeys(Enumerable.Empty <string>(), rootKey).Distinct(StringComparer.OrdinalIgnoreCase))
            {
                var surrogateKey = key;
                if (rootKey != null)
                {
                    surrogateKey = rootKey + ":" + key;
                }

                GetFullKeyNames(provider, surrogateKey, initialKeys);

                if (!initialKeys.Any(k => k.StartsWith(surrogateKey)))
                {
                    initialKeys.Add(surrogateKey);
                }
            }

            return(initialKeys);
        }
Ejemplo n.º 12
0
        public static HashSet <string> GetFullKeyNames(this IConfigurationProvider provider, string rootKey, HashSet <string> initialKeys)
        {
            foreach (var key in provider.GetChildKeys(Enumerable.Empty <string>(), rootKey))
            {
                string surrogateKey = key;
                if (rootKey != null)
                {
                    surrogateKey = rootKey + ":" + key;
                }

                GetFullKeyNames(provider, surrogateKey, initialKeys);

                if (!initialKeys.Any(k => k.StartsWith(surrogateKey)))
                {
                    initialKeys.Add(surrogateKey);
                }
            }

            return(initialKeys);
        }
Ejemplo n.º 13
0
        private static IList <string> getChildKeysFromProvider(IConfigurationProvider provider, string path)
        {
            var result = new List <string>();
            var keys   = provider.GetChildKeys(new string[0], path).ToList();

            foreach (var subkey in keys)
            {
                var subResults = getChildKeysFromProvider(provider, subkey);
                if (subResults.Any())
                {
                    result.AddRange(subResults);
                }
                else
                {
                    result.Add(string.IsNullOrEmpty(path) ? subkey : $"{path}{ConfigurationPath.KeyDelimiter}{subkey}");
                }
            }

            return(result.Distinct().ToList());
        }
Ejemplo n.º 14
0
        private void GetChildKeys(IConfigurationProvider provider)
        {
            var result = provider.GetChildKeys(Enumerable.Empty <string>(), "folder/");

            Assert.Equal(new[] { "folder/key3", "folder/key4" }, result);
        }
 public IEnumerable <string> GetChildKeys(IEnumerable <string> earlierKeys, string parentPath)
 {
     return(_provider.GetChildKeys(earlierKeys, parentPath));
 }