private void ProcessProperties() { if (string.IsNullOrEmpty(_source)) { return; } _source = _source.Replace("\\r\\n", Environment.NewLine); string[] rows = _source.Split(Environment.NewLine.ToCharArray()); dynamic properties = new DynamicConfigurationValues(true); string rexGroupPattern = @"\s*\[\s*(\w+)\s*\]\s*"; string current = string.Empty; var validRows = from row in rows where !string.IsNullOrEmpty(row.Trim()) && row.Trim().Length > 2 && !row.TrimStart().StartsWith("#") select row; foreach (string row in validRows) { Match match = Regex.Match(row, rexGroupPattern); if (match.Success) { current = match.Groups[1].Value; current = SanitizeSectionTitle(current); if (!properties.HasProperty(current)) { properties[current] = new DynamicConfigurationValues(); } } else { if (row.Contains("=") && !string.IsNullOrEmpty(current)) { string[] pair = row.Trim().Split("=".ToCharArray()); if (pair.Length == 2) { if (!properties[current].HasProperty(pair[0])) { properties[current][pair[0]] = pair[1]; } else { if (!(properties[current][pair[0]] is IEnumerable <string>)) { string singlePreviousValue = properties[current][pair[0]]; properties[current][pair[0]] = new List <string>(); properties[current][pair[0]].Add(singlePreviousValue); } properties[current][pair[0]].Add(pair[1]); } } } } } _properties = properties; }
public void ShouldCreateDynamicDictionary() { dynamic values = new DynamicConfigurationValues(); values["hello"] = "world"; Assert.AreEqual(values.hello, "world"); }
public void ShouldGetValidInstanceOfMappedObjectFromDynamicValue() { dynamic dv = new DynamicConfigurationValues(); dv.test_name = "Rui"; dv.test_id = 100; dv.test_birth = DateTime.Parse("1975/04/24"); User actual = ValueFactory.TryGet <User>(dv); User expected = UserHelper.GetUserRui(); Assert.AreEqual(actual.ToString(), expected.ToString()); }
public void ShouldGetAValueBasedOnMemberNamesWithoutMapping() { dynamic dv = new DynamicConfigurationValues(); dv.name = "Rui"; dv.id = 111; Contact expected = new Contact(); expected.Name = "Rui"; expected.Id = 111; Contact actual = ValueFactory.TryGet <Contact>(dv); Assert.AreEqual(actual, expected); }
private void ProcessProperties() { if (string.IsNullOrEmpty(_source)) { return; } _source = _source.Replace("\\r\\n",Environment.NewLine); string[] rows = _source.Split(Environment.NewLine.ToCharArray()); dynamic properties = new DynamicConfigurationValues(true); string rexGroupPattern = @"\s*\[\s*(\w+)\s*\]\s*"; string current = string.Empty; var validRows = from row in rows where !string.IsNullOrEmpty(row.Trim()) && row.Trim().Length > 2 && !row.TrimStart().StartsWith("#") select row; foreach (string row in validRows) { Match match = Regex.Match(row, rexGroupPattern); if (match.Success) { current = match.Groups[1].Value; current = SanitizeSectionTitle(current); if (!properties.HasProperty(current)) { properties[current] = new DynamicConfigurationValues(); } } else { if (row.Contains("=") && !string.IsNullOrEmpty(current)) { string[] pair = row.Trim().Split("=".ToCharArray()); if(pair.Length==2) { if (!properties[current].HasProperty(pair[0])) { properties[current][pair[0]] = pair[1]; } else { if (!(properties[current][pair[0]] is IEnumerable<string>)) { string singlePreviousValue = properties[current][pair[0]]; properties[current][pair[0]] = new List<string>(); properties[current][pair[0]].Add(singlePreviousValue); } properties[current][pair[0]].Add(pair[1]); } } } } } _properties = properties; }