Пример #1
0
        /// <summary>
        /// Converts the given <paramref name="entity"/> to a hubspot data entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="preformConversion">This by default performs a data conversion so that the HubSpot "property-value" syntax will be used for serialization. If this parameter is set to false no data conversion is performed (such that a standard object serialization can be performed). </param>
        /// <returns></returns>
        public dynamic ToHubspotDataEntity(IHubSpotEntity entity, bool preformConversion = true)
        {
            _logger.LogDebug("Convert ToHubspotDataEntity");
            dynamic mapped = new ExpandoObject();

            if (preformConversion)
            {
                mapped.Properties = new List <HubspotDataEntityProp>();

                _logger.LogDebug("Use nameValue mapping?: {0}", entity.IsNameValue);

                var allProps = entity.GetType().GetProperties();
                _logger.LogDebug("Have {0} props to map", allProps.Length);

                foreach (var prop in allProps)
                {
                    if (prop.HasIgnoreDataMemberAttribute())
                    {
                        continue;
                    }

                    var propSerializedName = prop.GetPropSerializedName();
                    _logger.LogDebug("Mapping prop: '{0}' with serialization name: '{1}'", prop.Name, propSerializedName);
                    if (prop.Name.Equals("RouteBasePath") || prop.Name.Equals("IsNameValue"))
                    {
                        continue;
                    }

                    // IF we have an complex type on the entity that we are trying to convert, let's NOT get the
                    // string value of it, but simply pass the object along - it will be serialized later as JSON...
                    var propValue = prop.GetValue(entity);
                    var value     = propValue.IsComplexType() ? propValue : propValue?.ToString();
                    var item      = new HubspotDataEntityProp
                    {
                        Property = propSerializedName,
                        Value    = value
                    };

                    if (entity.IsNameValue)
                    {
                        item.Property = null;
                        item.Name     = propSerializedName;
                    }
                    if (item.Value == null)
                    {
                        continue;
                    }

                    mapped.Properties.Add(item);
                }

                _logger.LogDebug("Mapping complete, returning data");
            }
            else
            {
                mapped = entity;
            }

            return(mapped);
        }
Пример #2
0
        /// <summary>
        /// Converts the given <paramref name="entity"/> to a simple list of name/values.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public IEnumerable <HubspotDataEntityProp> ToNameValueList(object entity)
        {
            _logger.LogDebug("Convert ToNameValueList");

            var properties = new List <HubspotDataEntityProp>();

            var allProps = entity.GetType().GetProperties();

            _logger.LogDebug("Have {0} props to map", allProps.Length);

            foreach (var prop in allProps)
            {
                if (prop.HasIgnoreDataMemberAttribute())
                {
                    continue;
                }

                var propSerializedName = prop.GetPropSerializedName();
                _logger.LogDebug("Mapping prop: '{0}' with serialization name: '{1}'", prop.Name, propSerializedName);
                if (prop.Name.Equals("RouteBasePath") || prop.Name.Equals("IsNameValue"))
                {
                    continue;
                }

                // IF we have an complex type on the entity that we are trying to convert, let's NOT get the
                // string value of it, but simply pass the object along - it will be serialized later as JSON...
                var propValue = prop.GetValue(entity);
                var value     = propValue.IsComplexType() ? propValue : propValue?.ToString();
                var item      = new HubspotDataEntityProp
                {
                    Property = null,
                    Name     = propSerializedName,
                    Value    = value
                };

                if (item.Value == null)
                {
                    continue;
                }

                properties.Add(item);
            }

            _logger.LogDebug("Mapping complete, returning data");

            return(properties);
        }