Beispiel #1
0
        internal static IEnumerable <KeyValuePair <string, string> > ConvertToConfig(
            this KVPair kvPair,
            string rootKey,
            IConfigurationParser parser)
        {
            using (Stream stream = new MemoryStream(kvPair.Value))
            {
                return(parser
                       .Parse(stream)
                       .Select(pair =>
                {
                    var key = $"{kvPair.Key.TrimEnd('/')}:{pair.Key}"
                              .Replace('/', ':')
                              .TrimStart(rootKey.ToCharArray())
                              .TrimStart(':')
                              .TrimEnd(':');
                    if (string.IsNullOrEmpty(key))
                    {
                        throw new InvalidKeyPairException(
                            "The key must not be null or empty. Ensure that there is at least one key under the root of the config or that the data there contains more than just a single value.");
                    }

                    return new KeyValuePair <string, string>(key, pair.Value);
                }));
            }
        }
        public void GetFilledOptions(object configModel, string text, IConfigurationParser parser)
        {
            ParsedObject parsedObject = null;

            try
            {
                parsedObject = parser.Parse(text);
            }
            catch
            {
                throw new Exception("Couldn't parse file");
            }

            FillOptions(configModel, parsedObject);
        }
Beispiel #3
0
        private async Task <IDictionary <string, string> > RequestConfigurationAsync()
        {
            var encodedConfigurationName = WebUtility.UrlEncode(_source.ConfigurationName);

            _logger.LogInformation("Requesting remote configuration {ConfigurationName} from {BaseAddress}.", _source.ConfigurationName, HttpClient.BaseAddress);

            try
            {
                using (var response = await HttpClient.GetAsync(encodedConfigurationName))
                {
                    _logger.LogInformation("Received response status code {StatusCode} from endpoint for configuration '{ConfigurationName}'.",
                                           response.StatusCode, _source.ConfigurationName);

                    if (response.IsSuccessStatusCode)
                    {
                        using (var stream = await response.Content.ReadAsStreamAsync())
                        {
                            _logger.LogInformation("Parsing remote configuration response stream ({Length:N0} bytes) for configuration '{ConfigurationName}'.",
                                                   stream.Length, _source.ConfigurationName);

                            Hash = ComputeHash(stream);
                            _logger.LogInformation("Computed hash for Configuration '{ConfigurationName}' is {Hash}.", _source.ConfigurationName, Hash);

                            stream.Position = 0;
                            var data = _parser.Parse(stream);

                            _logger.LogInformation("Configuration updated for '{ConfigurationName}'.", _source.ConfigurationName);

                            return(data);
                        }
                    }

                    if (!_source.Optional)
                    {
                        throw new Exception($"Error calling remote configuration endpoint: {response.StatusCode} - {response.ReasonPhrase}");
                    }
                }
            }
            catch (Exception)
            {
                if (!_source.Optional)
                {
                    throw;
                }
            }

            return(null);
        }
Beispiel #4
0
        public static Dictionary <string, string> GetValueAsDictionary(this KeyValueNode node, IConfigurationParser parser)
        {
            if (node.FullKey == null)
            {
                throw new ArgumentException("Key is empty");
            }

            if (string.IsNullOrWhiteSpace(node.Value))
            {
                return(new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
                {
                    { node.FullKey, null }
                });
            }

            using (var configStream = new MemoryStream(node.RawValue))
            {
                return(new Dictionary <string, string>(
                           parser.Parse(configStream),
                           StringComparer.OrdinalIgnoreCase));
            }
        }
Beispiel #5
0
 public T GetOptions <T>(string fileOptionsPath) where T : new()
 {
     return(configParser.Parse <T>(fileOptionsPath));
 }
Beispiel #6
0
 public T Parse <T>() where T : new()
 {
     return(parser.Parse <T>(path));
 }
        public void GetReferencesFromString(string text, int expectedResults, Type[] expectedTypes)
        {
            var result = _parser.Parse(text);

            Assert.NotNull(result);
            Assert.Equal(expectedResults, result.Count);
            Assert.Equal(expectedTypes.Length, result.Count);

            for (var i = 0; i < result.Count; ++i)
            {
                Assert.IsType(expectedTypes[i], result[i]);
            }
        }
 public Configuration GetConfiguration()
 {
     return(configParser.Parse <Configuration>(contents));
 }
Beispiel #9
0
 public T GetOptions <T>(string fileConfig) where T : new()
 {
     return(configurationParser.Parse <T>(fileConfig));
 }