Exemplo n.º 1
0
        /// <summary>
        /// Tries to resolve tenant and artifact based on the incoming request
        /// </summary>
        /// <param name="path">The request path</param>
        /// <param name="request">The request data</param>
        /// <param name="tenant">The resolved tenant</param>
        /// <param name="artifact">The created artifact</param>
        /// <returns>Whether the resolution worked</returns>
        protected bool TryResolveTenantAndArtifact(string path, IDictionary <string, StringValues> request, out TenantId tenant, out T artifact)
        {
            if (path[0] != '/')
            {
                path = $"/{path}";
            }

            var artifactType = _artifactTypes.GetTypeFor(path);
            var properties   = new NullFreeDictionary <string, object>();

            tenant = request["TenantId"].First().ParseTo(typeof(TenantId)) as TenantId;

            foreach (var property in artifactType.GetProperties())
            {
                if (request.TryGetValue(property.Name, out var values))
                {
                    if (TryConvertFormValuesTo(property.PropertyType, values, out var result))
                    {
                        if (result.GetType().IsConcept())
                        {
                            result = result.GetConceptValue();
                        }
                        properties.Add(property.Name, result);
                    }
                }
            }

            artifact = _objectFactory.Build(artifactType, new PropertyBag(properties)) as T;

            return(artifact != null);
        }
        static PropertyBag ToPropertyBag(Dictionary <string, object> target)
        {
            var nonNullDictionary = new NullFreeDictionary <string, object>();

            target.ForEach(kvp =>
            {
                if (kvp.Value == null)
                {
                    return;
                }

                var valueType = kvp.Value.GetType();
                if (valueType == typeof(object[]))
                {
                    var instances = (from object obj in kvp.Value as IEnumerable select IsComplexType(obj) ? ToPropertyBag(obj as Dictionary <string, object>) : obj).ToList();
                    nonNullDictionary.Add(new KeyValuePair <string, object>(kvp.Key, instances));
                }
                else
                {
                    nonNullDictionary.Add(IsComplexType(kvp.Value)
                        ? new KeyValuePair <string, object>(kvp.Key, ToPropertyBag(kvp.Value as Dictionary <string, object>))
                        : kvp);
                }
            });

            return(new PropertyBag(nonNullDictionary));
        }
Exemplo n.º 3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="source"></param>
 public void Perform(NullFreeDictionary <string, object> source)
 {
     if (_action != null)
     {
         _action(source);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Converts the PropertyBag to a <see cref="NullFreeDictionary{Tstring,Tobject}" />
        /// </summary>
        /// <returns>Instance of <see cref="NullFreeDictionary{Tstring,Tobject}" /></returns>
        public NullFreeDictionary <string, object> ToNullFreeDictionary()
        {
            var dictionary = new NullFreeDictionary <string, object>();

            this.AsDictionary().ForEach(kvp =>
            {
                object val = kvp.Value.GetType() == typeof(PropertyBag) ? ((PropertyBag)kvp.Value).ToNullFreeDictionary() : kvp.Value;
                dictionary.Add(kvp.Key, val);
            }
                                        );
            return(dictionary);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Read value from <see cref="MapField{TKey, TValue}"/>
        /// </summary>
        /// <param name="propertyBag"><see cref="MapField{TKey, TValue}"/> to read from</param>
        /// <returns>The <see cref="Dolittle.PropertyBags.PropertyBag"/></returns>
        public static Dolittle.PropertyBags.PropertyBag ToCLR(this MapField <string, System.Protobuf.Value> propertyBag)
        {
            var nullFreedictionary = new NullFreeDictionary <string, object>();

            propertyBag.ForEach(keyValue =>
            {
                var value = keyValue.Value.ToCLR();
                if (value != null)
                {
                    nullFreedictionary.Add(keyValue.Key, value);
                }
            });
            return(new Dolittle.PropertyBags.PropertyBag(nullFreedictionary));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Convert from <see cref="MapField{key,value}"/> to <see cref="PropertyBag"/>
        /// </summary>
        /// <param name="mapField"><see cref="MapField{key,value}"/> to convert from</param>
        /// <returns>Converted <see cref="PropertyBag"/></returns>
        public static PropertyBag ToPropertyBag(this MapField <string, System.Protobuf.Object> mapField)
        {
            var dictionary = new NullFreeDictionary <string, object>();

            mapField.ForEach(keyValue =>
            {
                var value = keyValue.Value.ToCLR();
                if (value != null)
                {
                    dictionary.Add(keyValue.Key, value);
                }
            });
            return(new PropertyBag(dictionary));
        }
        /// <summary>
        /// Serialize a <see cref="BsonDocument"/> to a <see cref="PropertyBag"/>
        /// </summary>
        /// <param name="doc"></param>
        public static PropertyBag Deserialize(BsonDocument doc)
        {
            var bsonAsDictionary  = doc.ToDictionary();
            var nonNullDictionary = new NullFreeDictionary <string, object>();

            bsonAsDictionary.ForEach(kvp =>
            {
                if (kvp.Value != null)
                {
                    nonNullDictionary.Add(kvp);
                }
            });
            var propertyBag = new PropertyBag(nonNullDictionary);

            return(propertyBag);
        }
        /// <summary>
        /// Creates a <see cref="PropertyBag"/> from an object.
        /// Maps primitive properties and complex objects to <see cref="PropertyBag"/> recursively
        /// </summary>
        /// <param name="obj">Object to convert</param>
        /// <returns></returns>
        public static PropertyBag ToPropertyBag(this object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            NullFreeDictionary <string, object> values = new NullFreeDictionary <string, object>();

            foreach (var property in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                var value = property.PropertyType.GetPropertyBagObjectValue(property.GetValue(obj));
                values.Add(property.Name, value);
            }
            return(new PropertyBag(values));
        }
        //TODO: this should be in Fundamentals Dolittle.PropertyBags
        static PropertyBag ToPropertyBag(Dictionary <string, object> dictionary)
        {
            if (dictionary == null)
            {
                return(null);
            }

            var values = new NullFreeDictionary <string, object>();

            foreach (var kvp in dictionary)
            {
                if (kvp.Value != null)
                {
                    values.Add(kvp.Key, kvp.Value is Dictionary <string, object>?(kvp.Value as Dictionary <string, object>).ToPropertyBag() : kvp.Value);
                }
            }
            return(new PropertyBag(values));
        }
Exemplo n.º 10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="values"></param>
 public PropertyBag(NullFreeDictionary <string, object> values) : base(values)
 {
 }
Exemplo n.º 11
0
 /// <summary>
 /// Perform a change.
 /// </summary>
 /// <param name="source">Source to change.</param>
 public void Perform(NullFreeDictionary <string, object> source)
 {
     _action?.Invoke(source);
 }