public static void ShouldBeGreaterThan <T>(double minValue, string propertyName, IExcelToEnumerableOptions <T> options)
 {
     if (!options.Validations.ContainsKey(propertyName))
     {
         options.Validations[propertyName] = new List <ExcelCellValidator>();
     }
     options.Validations[propertyName].Add(ExcelCellValidatorFactory.CreateGreaterThan(minValue));
 }
示例#2
0
        private IEnumerable <PropertySetter> GetSettersForProperty <T>(PropertyInfo propertyInfo, int index,
                                                                       IExcelToEnumerableOptions <T> options)
        {
            if (propertyInfo.PropertyType != typeof(string) &&
                typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType))
            {
                if (options.CollectionConfigurations == null ||
                    !options.CollectionConfigurations.ContainsKey(propertyInfo.Name))
                {
                    return(new PropertySetter[0]);
                }

                return(GetSettersForEnumerable(propertyInfo, index, options));
            }

            var setter = GetterSetterHelpers.GetSetter(propertyInfo);
            var getter = options.UniqueProperties != null && options.UniqueProperties.Contains(propertyInfo.Name)
                ? GetterSetterHelpers.GetGetter(propertyInfo)
                : null;
            var fromCellSetter = new PropertySetter
            {
                Getter     = getter,
                Setter     = setter,
                Type       = propertyInfo.PropertyType,
                ColumnName =
                    options.CustomHeaderNames != null && options.CustomHeaderNames.ContainsKey(propertyInfo.Name)
                        ? options.CustomHeaderNames[propertyInfo.Name]
                        : propertyInfo.Name.ToLowerInvariant(),
                PropertyName          = propertyInfo.Name,
                PropertyMapping       = GetPropertyMapping(options, propertyInfo.Name, propertyInfo.PropertyType),
                RelaxedNumberMatching = options.RelaxedNumberMatching && propertyInfo.PropertyType.IsNumeric()
            };

            if (propertyInfo.Name == options.RowNumberProperty)
            {
                fromCellSetter.ColumnName = null;
            }

            if (options.NotNullProperties.Contains(propertyInfo.Name))
            {
                fromCellSetter.Validators = new List <ExcelCellValidator> {
                    ExcelCellValidatorFactory.CreateRequired()
                };
            }

            return(new[] { fromCellSetter });
        }
        internal static void AddOptionsFromAttributes(ExcelToEnumerableOptionsBuilder <T> builder, Type type)
        {
            MapClassLevelAttributes(builder, type);

            var properties = type.GetProperties().Distinct().ToArray();

            foreach (var property in properties)
            {
                var propertyAttributes = property.CustomAttributes.ToArray();
                foreach (var propertyAttribute in propertyAttributes)
                {
                    switch (propertyAttribute.AttributeType.Name)
                    {
                    case nameof(MapsToColumnNumberAttribute):
                        var columnNumber = (int)propertyAttribute.ConstructorArguments[0].Value;
                        ExcelPropertyConfiguration.MapsToColumnNumber(columnNumber, property.Name, builder._options);
                        break;

                    case nameof(MapsToColumnLetterAttribute):
                        var columnLetter = (string)propertyAttribute.ConstructorArguments[0].Value;
                        ExcelPropertyConfiguration.MapsToColumnNumber(CellRef.ColumnNameToNumber(columnLetter),
                                                                      property.Name, builder._options);
                        break;

                    case nameof(OptionalColumnAttribute):
                        ExcelPropertyConfiguration.OptionalColumn(true, property.Name, builder._options);
                        break;

                    case nameof(MapFromColumnsAttribute):
                        var columnNames =
                            ((ReadOnlyCollection <CustomAttributeTypedArgument>)propertyAttribute
                             .ConstructorArguments[0].Value).Select(x => x.Value.ToString());
                        ExcelPropertyConfiguration.MapFromColumns(columnNames, property.Name, builder._options);
                        break;

                    case nameof(MapsToColumnNamedAttribute):
                        var columnName = (string)propertyAttribute.ConstructorArguments[0].Value;
                        ExcelPropertyConfiguration.MapsToColumnNamed(columnName, property.Name, builder._options);
                        break;

                    case nameof(IgnoreColumnAttribute):
                        ExcelPropertyConfiguration.Ignore(property.Name, builder._options);
                        break;

                    case nameof(MapsToRowNumberAttribute):
                        ExcelPropertyConfiguration.MapsToRowNumber(property.Name, builder._options);
                        break;

                    case nameof(ShouldBeLessThanAttribute):
                        var maxValue = (double)propertyAttribute.ConstructorArguments[0].Value;
                        ExcelPropertyConfiguration.ShouldBeLessThan(maxValue, property.Name, builder._options);
                        break;

                    case nameof(ShouldBeGreaterThanAttribute):
                        var minValue = (double)propertyAttribute.ConstructorArguments[0].Value;
                        ExcelPropertyConfiguration.ShouldBeGreaterThan(minValue, property.Name, builder._options);
                        break;

                    case nameof(NotNullAttribute):
                        ExcelPropertyConfiguration.NotNullProperties(property.Name, builder._options);
                        break;

                    case nameof(ShouldBeOneOfAttribute):
                        // CSH 27112020 We're adding a validator directly here, rather than going via the static ExcelPropertyConfiguration because the validator we're using here using
                        // an enumerable of type object rather than type TProperty (since enforcing argument types at compile time is not possible with Attributes)
                        var objectList =
                            ((ReadOnlyCollection <CustomAttributeTypedArgument>)propertyAttribute
                             .ConstructorArguments[0].Value).Select(x => x.Value);
                        builder._options.Validations[property.Name]
                        .Add(ExcelCellValidatorFactory.CreateShouldBeOneOf(objectList));
                        break;

                    case nameof(UniqueAttribute):
                        ExcelPropertyConfiguration.Unique(property.Name, builder._options);
                        break;

                    case nameof(RequiredColumnAttribute):
                        ExcelPropertyConfiguration.RequiredColumn(true, property.Name, builder._options);
                        break;
                    }
                }
            }
        }