Beispiel #1
0
        /// <inheritdoc/>
        public IEnumerable <ConfigData> ListConfigs(ConfigFilter filter = null)
        {
            IList <ConfigData> results = new List <ConfigData>();

            // include all values
            ISet <string> noNeedForDefault = new HashSet <string>();

            foreach (var appliesToSection in _root.GetChildren())
            {
                foreach (var configSection in appliesToSection.GetChildren())
                {
                    string key = configSection.Key;
                    if (_configDefinitionMap.TryGetValue(key, out var configDefinition))
                    {
                        (object value, string providerId) = GetConfigValueOrDefault(configSection, configDefinition);
                        ConfigScope scope = ConfigScopeHelper.GetScopeByProviderId(providerId);
                        results.Add(new ConfigData(configDefinition, value, scope, appliesToSection.Key));
                        // if a config is already set at global level, there's no need to return its default value
                        if (string.Equals(ConfigFilter.GlobalAppliesTo, appliesToSection.Key, StringComparison.OrdinalIgnoreCase))
                        {
                            noNeedForDefault.Add(configDefinition.Key);
                        }
                    }
                }
            }

            // include default values
            IEnumerable <string> keys = filter?.Keys ?? Enumerable.Empty <string>();

            bool isRegisteredKey(string key) => _configDefinitionMap.Keys.Contains(key, StringComparer.OrdinalIgnoreCase);

            IEnumerable <ConfigDefinition> configDefinitions = keys.Any() ? keys.Where(isRegisteredKey).Select(key => _configDefinitionMap[key]) : OrderedConfigDefinitionMap.Select(x => x.Value);

            configDefinitions.Where(x => !noNeedForDefault.Contains(x.Key)).Select(x => GetDefaultConfigData(x)).ForEach(x => results.Add(x));


            if (keys.Any())
            {
                results = results.Where(x => keys.Contains(x.Definition.Key, StringComparer.OrdinalIgnoreCase)).ToList();
            }

            string appliesTo = filter?.AppliesTo;

            if (!string.IsNullOrEmpty(appliesTo))
            {
                results = results.Where(x => string.Equals(appliesTo, x.AppliesTo, StringComparison.OrdinalIgnoreCase)).ToList();
            }

            return(results);
        }
Beispiel #2
0
        /// <inheritdoc/>
        public IEnumerable <ConfigData> ListConfigs(ConfigFilter filter = null)
        {
            string filterProviderId = null;
            bool   filterByScope    = filter != null && filter.Scope.HasValue;

            if (filterByScope)
            {
                filterProviderId = ConfigScopeHelper.GetProviderIdByScope(filter.Scope.Value);
            }

            IList <ConfigData> results          = new List <ConfigData>();
            ISet <string>      noNeedForDefault = new HashSet <string>();

            // if not filtering by default scope, include all values
            if (filterProviderId != Constants.ConfigProviderIds.None)
            {
                foreach (var appliesToSection in _root.GetChildren())
                {
                    foreach (var configSection in appliesToSection.GetChildren())
                    {
                        string key = configSection.Key;
                        if (_configDefinitionMap.TryGetValue(key, out var configDefinition))
                        {
                            if (filterByScope)
                            {
                                // try getting the config by the specific provider ID
                                object value = GetConfigValueOrDefault(configSection, configDefinition, filterProviderId);
                                if (value != null)
                                {
                                    results.Add(new ConfigData(configDefinition, value, filter.Scope.Value, appliesToSection.Key));
                                }
                            }
                            else
                            {
                                (object value, string providerId) = GetConfigValueOrDefault(configSection, configDefinition);
                                ConfigScope scope = ConfigScopeHelper.GetScopeByProviderId(providerId);
                                results.Add(new ConfigData(configDefinition, value, scope, appliesToSection.Key));
                                // if a config is already set at global level, there's no need to return its default value
                                if (string.Equals(ConfigFilter.GlobalAppliesTo, appliesToSection.Key, StringComparison.OrdinalIgnoreCase))
                                {
                                    noNeedForDefault.Add(configDefinition.Key);
                                }
                            }
                        }
                    }
                }
            }

            IEnumerable <string> keys = filter?.Keys ?? Enumerable.Empty <string>();

            // include default values
            if (filterByScope && filter.Scope.Value == ConfigScope.Default || !filterByScope)
            {
                bool isRegisteredKey(string key) => _configDefinitionMap.Keys.Contains(key, StringComparer.OrdinalIgnoreCase);

                IEnumerable <ConfigDefinition> configDefinitions = keys.Any() ? keys.Where(isRegisteredKey).Select(key => _configDefinitionMap[key]) : _configDefinitionMap.Select(x => x.Value);
                configDefinitions.Where(x => !noNeedForDefault.Contains(x.Key)).Select(x => GetDefaultConfigData(x)).ForEach(x => results.Add(x));
            }

            // filter by keys
            if (keys.Any())
            {
                results = results.Where(x => keys.Contains(x.Definition.Key, StringComparer.OrdinalIgnoreCase)).ToList();
            }

            // filter by appliesTo
            string appliesTo = filter?.AppliesTo;

            if (!string.IsNullOrEmpty(appliesTo))
            {
                results = results.Where(x => string.Equals(appliesTo, x.AppliesTo, StringComparison.OrdinalIgnoreCase)).ToList();
            }
            return(results.OrderBy(configData => configData.Definition.Key));
        }