Exemplo n.º 1
0
        /// <summary>
        /// Parses a configuration string to return a Configuration.
        /// </summary>
        /// <param name="configurationString"></param>
        /// <returns></returns>
        internal Configuration ParseConfiguration(string configurationString, bool permitUnknownValues = false)
        {
            var values = configurationString.Split(PropertySeperator);

            var valueSet = new PropertyValue[PropertiesByOrder.Length];

            for (int propertyIndex = 0, valueIndex = 0; propertyIndex < PropertiesByOrder.Length; propertyIndex++, valueIndex++)
            {
                var value    = valueIndex < values.Length ? values[valueIndex] : null;
                var property = PropertiesByOrder[propertyIndex];

                if (String.IsNullOrEmpty(value))
                {
                    if (property.DefaultValue != null)
                    {
                        valueSet[propertyIndex] = property.DefaultValue;
                        continue;
                    }
                    else
                    {
                        throw new ArgumentException($"No value was provided for property '{property.Name}' and no default value exists");
                    }
                }

                PropertyValue propertyValue;

                if (!AllPropertyValues.TryGetValue(value, out propertyValue))
                {
                    if (permitUnknownValues)
                    {
                        valueSet[propertyIndex] = new PropertyValue(value, property);
                        continue;
                    }
                    else
                    {
                        throw new ArgumentException($"Unknown value '{value}' found in configuration '{configurationString}'.  Expected property '{property.Name}' with one of values {String.Join(", ", PropertyValues[property].Select(v => v.Value))}.");
                    }
                }

                if (propertyValue.Property != property)
                {
                    // we have a known value but it is not for the expected property.
                    // so long as we have properties with defaultValues, set them
                    while (propertyValue.Property != property)
                    {
                        if (property.DefaultValue == null)
                        {
                            // we can't use this property at this index
                            throw new ArgumentException($"Property '{propertyValue.Property.Name}' value '{propertyValue.Value}' occurred at unexpected position in configuration '{configurationString}'");
                        }

                        // give this property its default value and advance to the next property
                        valueSet[propertyIndex++] = property.DefaultValue;

                        if (propertyIndex > PropertiesByOrder.Length)
                        {
                            // we ran out of possible properties.
                            throw new ArgumentException($"Property '{propertyValue.Property.Name}' value '{propertyValue.Value}' occurred at unexpected position in configuration '{configurationString}'");
                        }

                        property = PropertiesByOrder[propertyIndex];
                    }
                }

                // we found the position for this value.
                Debug.Assert(propertyValue.Property == property);
                valueSet[propertyIndex] = propertyValue;
            }

            return(new Configuration(valueSet));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses a configuration string to return a Configuration.
        /// </summary>
        /// <param name="configurationString"></param>
        /// <returns></returns>
        public Configuration ParseConfiguration(string configurationString, bool permitUnknownValues = false, Configuration baseConfiguration = null)
        {
            bool isPlaceHolderConfiguration = false;

            if (configurationString.StartsWith(NopConfigurationPrefix))
            {
                configurationString        = configurationString.Substring(1);
                isPlaceHolderConfiguration = true;
            }

            if (baseConfiguration != null)
            {
                if (baseConfiguration.Values == null ||
                    baseConfiguration.Values.Length != PropertiesByOrder.Length)
                {
                    throw new ArgumentException($"Cannot use {nameof(baseConfiguration)} {baseConfiguration} since it doesn't define the same number of properties.");
                }
            }

            var values = configurationString.Split(PropertySeparator);

            var valueSet = new PropertyValue[PropertiesByOrder.Length];

            for (int propertyIndex = 0, valueIndex = 0; propertyIndex < PropertiesByOrder.Length; propertyIndex++, valueIndex++)
            {
                var value    = valueIndex < values.Length ? values[valueIndex] : null;
                var property = PropertiesByOrder[propertyIndex];

                var defaultValue = GetDefaultValue(property, baseConfiguration?.Values[propertyIndex]);

                if (String.IsNullOrEmpty(value))
                {
                    if (defaultValue != null)
                    {
                        valueSet[propertyIndex] = defaultValue;
                        continue;
                    }
                    else
                    {
                        throw new ArgumentException($"No value was provided for property '{property.Name}' and no default value exists");
                    }
                }

                PropertyValue propertyValue;

                if (!AllPropertyValues.TryGetValue(value, out propertyValue))
                {
                    if (permitUnknownValues)
                    {
                        valueSet[propertyIndex] = new PropertyValue(value, property);
                        continue;
                    }
                    else
                    {
                        throw new ArgumentException($"Unknown value '{value}' found in configuration '{configurationString}'.  Expected property '{property.Name}' with one of values {String.Join(", ", PropertyValues[property].Select(v => v.Value))}.");
                    }
                }

                if (propertyValue.Property != property)
                {
                    // we have a known value but it is not for the expected property.
                    // so long as we have properties with defaultValues, set them
                    while (propertyValue.Property != property)
                    {
                        if (defaultValue == null)
                        {
                            // we can't use this property at this index
                            throw new ArgumentException($"Property '{propertyValue.Property.Name}' value '{propertyValue.Value}' occurred at unexpected position in configuration '{configurationString}'");
                        }

                        // give this property its default value and advance to the next property
                        valueSet[propertyIndex++] = defaultValue;

                        if (propertyIndex > PropertiesByOrder.Length)
                        {
                            // we ran out of possible properties.
                            throw new ArgumentException($"Property '{propertyValue.Property.Name}' value '{propertyValue.Value}' occurred at unexpected position in configuration '{configurationString}'");
                        }

                        property     = PropertiesByOrder[propertyIndex];
                        defaultValue = GetDefaultValue(property, baseConfiguration?.Values[propertyIndex]);
                    }
                }

                // we found the position for this value.
                Debug.Assert(propertyValue.Property == property);
                valueSet[propertyIndex] = propertyValue;
            }

            return(new Configuration(valueSet)
            {
                IsPlaceHolderConfiguration = isPlaceHolderConfiguration
            });

            PropertyValue GetDefaultValue(PropertyInfo property, PropertyValue baseValue)
            {
                if (baseValue != null && property.Insignificant)
                {
                    if (baseValue.Property != property)
                    {
                        throw new ArgumentException($"Cannot use {nameof(baseValue)} {baseValue} property {baseValue.Property} since it differs from property {property}.");
                    }

                    return(baseValue);
                }

                return(property.DefaultValue);
            }
        }