Exemple #1
0
        public static RedisInfo FromInfoString(string infoStr)
        {
            var info = new RedisInfo {
                FullInfoString = infoStr
            };

            RedisInfoSection currentSection = null;

            var lines = infoStr.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var line in lines)
            {
                if (line.StartsWith("#"))
                {
                    var          sectionName = line.Replace("# ", "");
                    PropertyInfo currentSectionProp;
                    if (_sectionMappings.TryGetValue(sectionName, out currentSectionProp))
                    {
                        currentSection = (RedisInfoSection)currentSectionProp.GetValue(info);
                    }
                    else
                    {
                        currentSection = new RedisInfoSection {
                            Name = sectionName, IsUnrecognized = true
                        };
                        if (info.UnrecognizedSections == null)
                        {
                            info.UnrecognizedSections = new List <RedisInfoSection>();
                        }
                        info.UnrecognizedSections.Add(currentSection);
                    }
                    continue;
                }
                if (currentSection == null)
                {
                    //TODO: Take care of global pre-2.6 case here
                    continue;
                }

                var splits = line.Split(StringSplits.Colon, 2);
                if (splits.Length != 2)
                {
                    currentSection.MapUnrecognizedLine(line);
                    continue;
                }

                string key = splits[0], value = splits[1];
                currentSection.AddLine(key, value);

                if (currentSection.IsUnrecognized)
                {
                    continue;
                }

                PropertyInfo propertyInfo;
                var          prop = _propertyMappings[currentSection.GetType()].TryGetValue(key, out propertyInfo) ? propertyInfo : null;
                if (prop == null)
                {
                    currentSection.MapUnrecognizedLine(key, value);
                    continue;
                }

                try
                {
                    if (prop.PropertyType == typeof(bool))
                    {
                        prop.SetValue(currentSection, value.IsNullOrEmptyReturn("0") != "0");
                    }
                    else
                    {
                        prop.SetValue(currentSection, Convert.ChangeType(value, prop.PropertyType));
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(string.Format("Error parsing '{0}' from {1} as {2} for {3}.{4}", value, key, prop.PropertyType.Name, currentSection.GetType(), prop.Name), e);
                }
            }

            return(info);
        }
        public static RedisInfo FromInfoString(string infoStr)
        {
            var info = new RedisInfo {FullInfoString = infoStr};

            RedisInfoSection currentSection = null;

            var lines = infoStr.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var line in lines)
            {
                if (line.StartsWith("#"))
                {
                    var sectionName = line.Replace("# ", "");
                    PropertyInfo currentSectionProp;
                    if (_sectionMappings.TryGetValue(sectionName, out currentSectionProp))
                    {
                        currentSection = (RedisInfoSection) currentSectionProp.GetValue(info);
                    }
                    else
                    {
                        currentSection = new RedisInfoSection {Name = sectionName, IsUnrecognized = true};
                        if (info.UnrecognizedSections == null)
                            info.UnrecognizedSections = new List<RedisInfoSection>();
                        info.UnrecognizedSections.Add(currentSection);
                    }
                    continue;
                }
                if (currentSection == null)
                {
                    //TODO: Take care of global pre-2.6 case here
                    continue;
                }

                var splits = line.Split(StringSplits.Colon, 2);
                if (splits.Length != 2)
                {
                    currentSection.MapUnrecognizedLine(line);
                    continue;
                }

                string key = splits[0], value = splits[1];
                currentSection.AddLine(key, value);

                if (currentSection.IsUnrecognized) 
                    continue;
                
                PropertyInfo propertyInfo;
                var prop = _propertyMappings[currentSection.GetType()].TryGetValue(key, out propertyInfo) ? propertyInfo : null;
                if (prop == null)
                {
                    currentSection.MapUnrecognizedLine(key, value);
                    continue;
                }

                try
                {
                    if (prop.PropertyType == typeof (bool))
                    {
                        prop.SetValue(currentSection, value.IsNullOrEmptyReturn("0") != "0");
                    }
                    else
                    {
                        prop.SetValue(currentSection, Convert.ChangeType(value, prop.PropertyType));
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(string.Format("Error parsing '{0}' from {1} as {2} for {3}.{4}", value, key, prop.PropertyType.Name, currentSection.GetType(), prop.Name), e);
                }
            }

            return info;
        }