示例#1
0
        private IEnumerable <IValidationError> MapRow <TModel>(
            Row row,
            MappingConfig mappingConfig,
            PropertyInfo property,
            TModel obj) where TModel : new()
        {
            var columnMapping = mappingConfig.GetColumnMappingByPropertyName(property.Name);

            if (columnMapping.IsNull())
            {
                return(new List <IValidationError>());
            }

            var cell = row.GetCellByColumnIndex(columnMapping.ColumnIndex);

            if (cell == null)
            {
                return(HandleEmptyCell(columnMapping, row.RowIndex, property.Name)
                       .OnValue(error => new List <IValidationError> {
                    error
                })
                       .OnEmpty(() => new List <IValidationError>())
                       .Unwrap());
            }

            var validationErrors = new List <IValidationError>();

            _valueMapper
            .Map(cell.Value.ToString(), property.PropertyType, row.RowIndex, columnMapping)
            .OnSuccess(value =>
            {
                if (value.ToString().IsNotNullOrEmpty())
                {
                    property.SetValue(obj, value);
                }
            })
            .OnFailure(validationError => { validationErrors.Add(validationError); });

            return(validationErrors);
        }