Exemple #1
0
        public virtual Task MergeAsync(TKey key, TValue value, CancellationToken cancellationToken = default)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var properties = DictionaryConverter.ToDictionary(value);

            if (!properties.Any())
            {
                return(TaskUtil.CompletedTask);
            }
            var existingValue = GetOrCreateValue(key);

            foreach (var propertyKeyValue in properties)
            {
                var property = typeof(TValue)
                               .GetProperty(
                    propertyKeyValue.Key,
                    BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

                if (property != null)
                {
                    try
                    {
                        if (property.PropertyType.GetTypeInfo().IsEnum)
                        {
                            property.SetValue(
                                existingValue,
                                Enum.Parse(property.PropertyType, propertyKeyValue.Value.ToString(), true));
                        }
                        else
                        {
                            property.SetValue(
                                existingValue,
                                propertyKeyValue.Value);
                        }
                    }
                    catch
                    {
                        var parse = TypeUtil.GetParseFuncForType(property.PropertyType);
                        if (parse != null)
                        {
                            property.SetValue(existingValue, parse(propertyKeyValue.Value.ToString()));
                        }
                        else
                        {
                            throw new InvalidOperationException($"Cannot set value for property '{property.Name}'");
                        }
                    }
                }
            }

            return(Task.CompletedTask);
        }
Exemple #2
0
        public void Test_Create_empty_Dictionary(string dictionaryString)
        {
            var cut = new DictionaryConverter();

            var actual = cut.ToDictionary(dictionaryString);

            Assert.Equal(new Dictionary <string, string>(), actual);
        }
Exemple #3
0
        public void Test_throw_exception(string dictionaryString)
        {
            var cut = new DictionaryConverter();

            var actual = Assert.Throws <ArgumentOutOfRangeException>(() =>
                                                                     cut.ToDictionary(dictionaryString));

            Assert.Contains(dictionaryString, actual.Message);
        }
Exemple #4
0
        public void Test_Create_Dictionary_with_one_entry(string dictionaryString, string expected)
        {
            var cut = new DictionaryConverter();

            var dic = cut.ToDictionary(dictionaryString);

            var length = dictionaryString.IndexOf("=", StringComparison.Ordinal);
            var key    = dictionaryString.Substring(0, length);

            var actual = dic[key];

            Assert.Equal(expected, actual);
        }
Exemple #5
0
        public void Test_Create_Dictionary_with_two_entries(string dictionaryString, params string[] expected)
        {
            // a very flexible test...
            var cut = new DictionaryConverter();

            var dic = cut.ToDictionary(dictionaryString);

            var keys = dic.Keys.ToList();

            Assert.All(keys, key =>
            {
                var index  = keys.IndexOf(key);
                var actual = dic[key];

                Assert.Equal(expected[index], actual);
            }
                       );
        }