/// <summary>
        /// Updates only few selected fields in a data item.
        /// </summary>
        /// <param name="correlation_id">(optional) transaction id to trace execution through call chain.</param>
        /// <param name="id">an id of data item to be updated.</param>
        /// <param name="data">a map with fields to be updated.</param>
        /// <returns>updated item.</returns>
        public async Task <T> UpdatePartiallyAsync(string correlationId, K id, AnyValueMap data)
        {
            _lock.EnterWriteLock();

            object item;

            try
            {
                var index = _items.FindIndex(x => x.Id.Equals(id));

                if (index < 0)
                {
                    return(default(T));
                }

                item = _items[index];

                var properties = ObjectReader.GetProperties(data.GetAsObject());
                ObjectWriter.SetProperties(item, properties);

                _items[index] = (T)item;

                _logger.Trace(correlationId, "Partially updated item {0}", id);
            }
            finally
            {
                _lock.ExitWriteLock();
            }

            await SaveAsync(correlationId);

            return((T)item);
        }
Пример #2
0
        public void TestGetArrayProperties()
        {
            AnyValueArray list = AnyValueArray.FromValues(123, "ABC");

            var names = ObjectReader.GetPropertyNames(list);

            Assert.Equal(2, names.Count);
            Assert.Contains("0", names);
            Assert.Contains("1", names);

            var values = ObjectReader.GetProperties(list);

            Assert.Equal(2, values.Count);
            Assert.Equal(123, values["0"]);
            Assert.Equal("ABC", values["1"]);

            var array = new object[] { 123, "ABC" };

            names = ObjectReader.GetPropertyNames(array);
            Assert.Equal(2, names.Count);
            Assert.Contains("0", names);
            Assert.Contains("1", names);

            values = ObjectReader.GetProperties(array);
            Assert.Equal(2, values.Count);
            Assert.Equal(123, values["0"]);
            Assert.Equal("ABC", values["1"]);
        }
        protected internal override void PerformValidation(string path, object value, List <ValidationResult> results)
        {
            base.PerformValidation(path, value, results);

            if (value == null)
            {
                return;
            }

            var properties = ObjectReader.GetProperties(value);

            // Process defined properties
            if (Properties != null)
            {
                foreach (var propertySchema in Properties)
                {
                    string processedName = null;

                    foreach (var entry in properties)
                    {
                        string propertyName  = entry.Key;
                        object propertyValue = entry.Value;
                        // Find properties case insensitive
                        if (propertyName.Equals(propertySchema.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            propertySchema.PerformValidation(path, propertyValue, results);
                            processedName = propertyName;
                            break;
                        }
                    }

                    if (processedName == null)
                    {
                        propertySchema.PerformValidation(path, null, results);
                    }
                    else
                    {
                        properties.Remove(processedName);
                    }
                }
            }

            // Process unexpected properties
            foreach (var entry in properties)
            {
                string propertyPath = string.IsNullOrWhiteSpace(path)
                    ? entry.Key : path + "." + entry.Key;

                results.Add(
                    new ValidationResult(
                        propertyPath,
                        ValidationResultType.Warning,
                        "UNEXPECTED_PROPERTY",
                        "Found unexpected property " + entry.Key,
                        null,
                        entry.Key
                        )
                    );
            }
        }
Пример #4
0
        public void TestGetObjectProperties()
        {
            var obj   = new TestClass();
            var names = ObjectReader.GetPropertyNames(obj);

            Assert.Equal(3, names.Count);
            Assert.Contains("PublicField", names);
            Assert.Contains("PublicProp", names);

            var map = ObjectReader.GetProperties(obj);

            Assert.Equal(3, map.Count);
            Assert.Equal("ABC", map["PublicField"]);
            Assert.NotNull(map["PublicProp"]);
        }
Пример #5
0
        public void TestGetMapProperties()
        {
            AnyValueMap map = AnyValueMap.FromTuples(
                "key1", 123,
                "key2", "ABC"
                );
            var names = ObjectReader.GetPropertyNames(map);

            Assert.Equal(2, names.Count);
            Assert.Contains("key1", names);
            Assert.Contains("key2", names);

            var values = ObjectReader.GetProperties(map);

            Assert.Equal(2, values.Count);
            Assert.Equal(123, values["key1"]);
            Assert.Equal("ABC", values["key2"]);
        }