/// <summary>
        ///		Implements the mutation logic for this <see cref="ToDefaultValueAttribute" />.
        /// </summary>
        /// <param name="value">The value to mutate.</param>
        /// <param name="context">Describes the <paramref name="value" /> being mutated and provides services and context for mutation.</param>
        /// <returns>The type's default value when the specified <paramref name="value" /> is in <see cref="Values" />.</returns>
        protected override object MutateValue(object value, IMutationContext context)
        {
            if (_defaultValue == null && value != null)
            {
                var type = value.GetType();

                if (type.GetTypeInfo().IsValueType)
                {
                    // Is there better way to get the default value?
                    _defaultValue = Activator.CreateInstance(type);
                }
            }

            if (Values.Count() <= 0)
            {
                if (value == _defaultValue || value.Equals(_defaultValue))
                {
                    value = AttributeValue(context) ?? _defaultValue;
                }
            }
            else
            {
                foreach (var testValue in Values)
                {
                    if (value == testValue || (value != null && value.Equals(testValue)))
                    {
                        value = AttributeValue(context) ?? _defaultValue;
                    }
                }
            }

            _defaultValue = null;

            return(value);
        }
 void IMutableObject.Mutate(IMutationContext context)
 {
     if (EnforceRange)
     {
         Integer = Math.Max(1, Math.Min(99, Integer));
     }
 }
 /// <summary>
 ///		Implements the mutation logic for this <see cref="ApplyMaxLengthAttribute" />.
 /// </summary>
 /// <param name="value">The value to mutate.</param>
 /// <param name="context">Describes the <paramref name="value" /> being mutated and provides services and context for mutation.</param>
 /// <returns>The truncated string data when the specified <paramref name="value" /> exceeds the maximum allowable length.</returns>
 protected override object MutateValue(object value, IMutationContext context)
 {
     if (value != null)
     {
         if (
             _descriptor == null && value is string valueAsString &&
             (
                 _maximumLength > -1 || TryGetStringLengthOrMaxLengthAttributeValue(context, out _maximumLength)
             ) &&
             valueAsString.Length > _maximumLength
             )
         {
             value = valueAsString.Substring(0, _maximumLength);
         }
         else if (
             value is ICollection valueAsCollection &&
             (
                 _maximumLength > -1 || TryGetMaxLengthAttributeValue(context, out _maximumLength)
             ) &&
             valueAsCollection.Count > _maximumLength
             )
         {
             value = _descriptor?.CollectionMutator?.Invoke(_maximumLength, valueAsCollection);
         }
     }
        /// <summary>
        ///		Retrieves the set of attributes for the property.
        /// </summary>
        /// <param name="context">The context that describes the object containing property.</param>
        /// <param name="property">The <see cref="PropertyInfo" /> that describes the property.</param>
        /// <returns>The collection of attributes.</returns>
        internal IEnumerable <Attribute> GetPropertyAttributes(IMutationContext context, PropertyInfo property)
        {
            var typeItem = GetTypeStoreItem(context.ObjectInstance.GetType());
            var item     = typeItem.GetPropertyStoreItem(property.Name);

            return(item.Attributes);
        }
        /// <summary>
        ///		Retrieves the Type of the given property.
        /// </summary>
        /// <param name="context">The context that describes the object containing property.</param>
        /// <param name="property">The <see cref="PropertyInfo" /> that describes the property.</param>
        /// <returns>The type of the specified property.</returns>
        internal Type GetPropertyType(IMutationContext context, PropertyInfo property)
        {
            var typeItem = GetTypeStoreItem(context.ObjectInstance.GetType());
            var item     = typeItem.GetPropertyStoreItem(property.Name);

            return(item.PropertyType);
        }
Ejemplo n.º 6
0
        protected override object MutateValue(object value, IMutationContext context)
        {
            if (value is TestClass typedValue)
            {
                typedValue.IntergerTen = 100;
            }

            return(value);
        }
Ejemplo n.º 7
0
        /// <summary>
        ///		Mutates the given value according to this <see cref="MutationAttribute" />.
        /// </summary>
        /// <param name="value">The value to mutate.</param>
        /// <param name="context">Describes the <paramref name="value" /> being mutated and provides services and context for mutation.</param>
        /// <returns>The resulting mutated value.</returns>
        /// <exception cref="ArgumentNullException">When <paramref name="context" /> is required and <c>null</c>.</exception>
        public object Mutate(object value, IMutationContext context = null)
        {
            if (RequiresContext && context == null)
            {
                throw new ArgumentNullException(nameof(context), "A mutation context is required by this mutation attribute.");
            }

            return(MutateValue(value, context));
        }
Ejemplo n.º 8
0
        /// <summary>
        ///		Implements the mutation logic for this <see cref="ToLowerAttribute" />.
        /// </summary>
        /// <param name="value">The value to mutate.</param>
        /// <param name="context">Describes the <paramref name="value" /> being mutated and provides services and context for mutation.</param>
        /// <returns>The specified <paramref name="value" /> converted to lowercase.</returns>
        protected override object MutateValue(object value, IMutationContext context)
        {
            if (value is string valueAsString && !String.IsNullOrWhiteSpace(valueAsString))
            {
                return(CultureInfo.TextInfo.ToLower(valueAsString));
            }

            if (value is char valueAsChar)
            {
                return(CultureInfo.TextInfo.ToLower(valueAsChar));
            }

            return(value);
        }
        /// <summary>
        ///		Mutates the specified value against the current context and the specified <see cref="MutationAttribute" />s.
        /// </summary>
        /// <typeparam name="T">The type to consult during mutation.</typeparam>
        /// <param name="context">Describes the type of object being mutated and provides services and context for mutation.</param>
        /// <param name="attributes">The list of <see cref="MutationAttribute" />s to modify the specified <paramref name="value" /> against.</param>
        /// <param name="value">The value to be mutated.</param>
        /// <returns>The value that has been modified according to any associated <see cref="MutationAttribute" />s and <see cref="IMutableObject" /> implementation.</returns>
        /// <exception cref="ArgumentNullException">When <paramref name="context" /> is required and <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">When <paramref name="attributes" /> is <c>null</c>.</exception>
        private static T GetMutatedValue <T>(IMutationContext context, IEnumerable <MutationAttribute> attributes, T value)
        {
            if (attributes == null)
            {
                throw new ArgumentNullException(nameof(attributes));
            }

            foreach (var attribute in attributes.OrderBy(x => x.Priority))
            {
                value = (T)attribute.Mutate(value, context);
            }

            return(value);
        }
        /// <summary>
        ///		Implements the mutation logic for this <see cref="RegexReplaceAttribute" />.
        /// </summary>
        /// <param name="value">The value to mutate.</param>
        /// <param name="context">Describes the <paramref name="value" /> being mutated and provides services and context for mutation.</param>
        /// <returns>A new string that is identical to the input string, except that the replacement string takes the place of each matched string. If the regular expression pattern is not matched in the current instance, the method returns the current instance unchanged.</returns>
        protected override object MutateValue(object value, IMutationContext context)
        {
            if (value != null && value is string valueAsString)
            {
                foreach (string pattern in Patterns)
                {
                    valueAsString = new Regex(pattern, Options).Replace(valueAsString, Replacement);
                }

                value = valueAsString;
            }

            return(value);
        }
        /// <summary>
        ///		Implements the mutation logic for this <see cref="ReplaceAttribute" />.
        /// </summary>
        /// <param name="value">The value to mutate.</param>
        /// <param name="context">Describes the <paramref name="value" /> being mutated and provides services and context for mutation.</param>
        /// <returns>A string that is equivalent to the current <paramref name="value" /> except that all instances of specified <see cref="Antecedents" /> are replaced with the value of <see cref="Replacement" />. If none of the <see cref="Antecedents" /> are found in the current <paramref name="value" />, the method returns the current <paramref name="value" /> unchanged.</returns>
        protected override object MutateValue(object value, IMutationContext context)
        {
            if (value != null && value is string valueAsString)
            {
                foreach (string antecedent in Antecedents)
                {
                    valueAsString = valueAsString.Replace(antecedent, Replacement);
                }

                value = valueAsString;
            }

            return(value);
        }
        /// <summary>
        ///		Implements the mutation logic for this <see cref="ToNumericAttribute" />.
        /// </summary>
        /// <param name="value">The value to mutate.</param>
        /// <param name="context">Describes the <paramref name="value" /> being mutated and provides services and context for mutation.</param>
        /// <returns>The resulting mutated value in the specified numeric format.</returns>
        protected override object MutateValue(object value, IMutationContext context)
        {
            if (value is string valueAsString)
            {
                if (String.IsNullOrWhiteSpace(valueAsString))
                {
                    return(null);
                }

                string rgxMod = "";

                if (PreserveFloatingPoint)
                {
                    rgxMod = ".";

                    int search = valueAsString.LastIndexOf('.');

                    if (search > -1)
                    {
                        valueAsString = valueAsString.Substring(0, search).Replace(".", "") + valueAsString.Substring(search);
                    }
                }

                if (PreserveSign)
                {
                    rgxMod += "+-";
                }

                valueAsString = Regex.Replace(valueAsString, $"[^0-9{rgxMod}]", "");

                if (PreserveSign && valueAsString.Length > 0)
                {
                    valueAsString = valueAsString[0] + Regex.Replace(valueAsString.Substring(1), "[+-]", "");

                    if (valueAsString.Length == 1 && valueAsString.IndexOfAny(new[] { '+', '-' }) == 0)
                    {
                        return(null);
                    }
                }

                return(String.IsNullOrEmpty(valueAsString) ? null : valueAsString);
            }

            return(value);
        }
Ejemplo n.º 13
0
        /// <summary>
        ///		Implements the mutation logic for this <see cref="TrimAttribute" />.
        /// </summary>
        /// <param name="value">The value to mutate.</param>
        /// <param name="context">Describes the <paramref name="value" /> being mutated and provides services and context for mutation.</param>
        /// <returns>The string that remains after all occurrences of the characters in the the <see cref="Characters" /> array are removed from the start and/or end of the specified string. If the <see cref="Characters" /> array is null or an empty array, white-space characters are removed instead.</returns>
        protected override object MutateValue(object value, IMutationContext context)
        {
            if (value is string valueAsString)
            {
                if (Direction.IsSingleFlag())
                {
                    if (Direction.HasFlag(TrimOptions.FromStart))
                    {
                        return(valueAsString.TrimStart(Characters));
                    }
                    else if (Direction.HasFlag(TrimOptions.FromEnd))
                    {
                        return(valueAsString.TrimEnd(Characters));
                    }
                }

                return(valueAsString.Trim(Characters));
            }

            return(value);
        }
 /// <summary>
 ///		Implements the mutation logic for this <see cref="ToNullStringAttribute" />.
 /// </summary>
 /// <param name="value">The value to mutate.</param>
 /// <param name="context">Describes the <paramref name="value" /> being mutated and provides services and context for mutation.</param>
 /// <returns><c>null</c> when the <paramref name="value" /> is empty or whitespace, otherwise the specified <paramref name="value" />.</returns>
 protected override object MutateValue(object value, IMutationContext context)
 => value is string valueAsString && String.IsNullOrWhiteSpace(valueAsString)
Ejemplo n.º 15
0
 protected override object MutateValue(object value, IMutationContext context)
 {
     return(context.Items["Yes"]);
 }
 protected override object MutateValue(object value, IMutationContext context)
 => value;
Ejemplo n.º 17
0
 /// <summary>
 ///		A protected method to override and implement mutation logic.
 /// </summary>
 /// <param name="value">The value to mutate.</param>
 /// <param name="context">Describes the <paramref name="value" /> being mutated and provides services and context for mutation.</param>
 /// <returns>The resulting mutated value.</returns>
 protected abstract object MutateValue(object value, IMutationContext context);
 /// <summary>
 ///		Gets the default value from an associated <see cref="DefaultValueAttribute" />.
 /// </summary>
 /// <param name="context">Describes the value being mutated and provides services and context for mutation.</param>
 /// <returns>The value of an associated <see cref="DefaultValueAttribute" />, otherwise <c>null</c>.</returns>
 private object AttributeValue(IMutationContext context)
 => context?.Attributes.OfType <DefaultValueAttribute>().FirstOrDefault()?.Value;
 /// <summary>
 ///		Retrieves the type level attributes for the given type.
 /// </summary>
 /// <param name="context">The context that describes the type.</param>
 /// <returns>The collection of attributes.</returns>
 internal IEnumerable <Attribute> GetTypeAttributes(IMutationContext context)
 => GetTypeStoreItem(context.ObjectInstance.GetType()).Attributes;
        /// <summary>
        ///		Mutates the given value according to this <see cref="MutationAttribute" />.
        /// </summary>
        /// <param name="value">The value to mutate.</param>
        /// <param name="defaultValue">The value to be used instead of the type's default value.</param>
        /// <param name="context">Describes the <paramref name="value" /> being mutated and provides services and context for mutation.</param>
        /// <returns>The resulting mutated value.</returns>
        /// <exception cref="ArgumentNullException">When <paramref name="context" /> is required and <c>null</c>.</exception>
        public object Mutate(object value, object defaultValue, IMutationContext context = null)
        {
            _defaultValue = defaultValue;

            return(base.Mutate(value, context));
        }