Example #1
0
        internal ILookup <string, Dictionary <string, IConfigurationArgumentValue> > GetMethodCalls(IConfigurationSection directive)
        {
            var children = directive.GetChildren().ToList();

            var result =
                (from child in children
                 where child.Value != null // Plain string
                 select new { Name = child.Value, Args = new Dictionary <string, IConfigurationArgumentValue>() })
                .Concat(
                    (from child in children
                     where child.Value == null
                     let name = GetSectionName(child)
                                let callArgs = (from argument in child.GetSection("Args").GetChildren()
                                                select new {
                Name = argument.Key,
                Value = GetArgumentValue(argument)
            }).ToDictionary(p => p.Name, p => p.Value)
                                               select new { Name = name, Args = callArgs }))
                .ToLookup(p => p.Name, p => p.Args);

            return(result);

            IConfigurationArgumentValue GetArgumentValue(IConfigurationSection argumentSection)
            {
                IConfigurationArgumentValue argumentValue;

                if (argumentSection.Value != null)
                {
                    argumentValue = new StringArgumentValue(() => argumentSection.Value, argumentSection.GetReloadToken);
                }
                else
                {
                    argumentValue = new ConfigurationSectionArgumentValue(new ConfigurationReader(argumentSection, _configurationAssemblies, _dependencyContext));
                }

                return(argumentValue);
            }

            string GetSectionName(IConfigurationSection s)
            {
                var name = s.GetSection("Name");

                if (name.Value == null)
                {
                    throw new InvalidOperationException($"The configuration value in {name.Path} has no 'Name' element.");
                }

                return(name.Value);
            }
        }
Example #2
0
        internal ILookup <string, Dictionary <string, IConfigurationArgumentValue> > GetMethodCalls(IConfigurationSection directive)
        {
            var children = directive.GetChildren().ToList();

            var result =
                (from child in children
                 where child.Value != null // Plain string
                 select new { Name = child.Value, Args = new Dictionary <string, IConfigurationArgumentValue>() })
                .Concat(
                    (from child in children
                     where child.Value == null
                     let name = GetSectionName(child)
                                let callArgs = (from argument in child.GetSection("Args").GetChildren()
                                                select new
            {
                Name = argument.Key,
                Value = GetArgumentValue(argument)
            }).ToDictionary(p => p.Name, p => p.Value)
                                               select new { Name = name, Args = callArgs }))
                .ToLookup(p => p.Name, p => p.Args);

            return(result);

            IConfigurationArgumentValue GetArgumentValue(IConfigurationSection argumentSection)
            {
                IConfigurationArgumentValue argumentValue;

                // Reject configurations where an element has both scalar and complex
                // values as a result of reading multiple configuration sources.
                if (argumentSection.Value != null && argumentSection.GetChildren().Any())
                {
                    throw new InvalidOperationException(
                              $"The value for the argument '{argumentSection.Path}' is assigned different value " +
                              "types in more than one configuration source. Ensure all configurations consistently " +
                              "use either a scalar (int, string, boolean) or a complex (array, section, list, " +
                              "POCO, etc.) type for this argument value.");
                }

                if (argumentSection.Value != null)
                {
                    argumentValue = new StringArgumentValue(argumentSection.Value);
                }
                else
                {
                    argumentValue = new ObjectArgumentValue(argumentSection, _configurationAssemblies);
                }

                return(argumentValue);
            }

            string GetSectionName(IConfigurationSection s)
            {
                var name = s.GetSection("Name");

                if (name.Value == null)
                {
                    throw new InvalidOperationException($"The configuration value in {name.Path} has no 'Name' element.");
                }

                return(name.Value);
            }
        }