From bddb43b5e1a7921a01e881d1094b141eca61e692 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 17 Feb 2018 18:04:10 -0800 Subject: [PATCH] #114 Add 'ExcelIgnore' Attribute. --- .../CompanyIgnoreIsActive.cs | 10 +++++ .../ExcelQueryFactoryTests.cs | 42 +++++++++++++++++++ .../LinqToExcel.Tests.csproj | 1 + .../Attributes/ExcelIgnoreAttribute.cs | 15 +++++++ src/LinqToExcel/LinqToExcel.csproj | 1 + src/LinqToExcel/Query/ExcelQueryExecutor.cs | 6 ++- src/LinqToExcel/Query/ExcelQueryable.cs | 4 +- 7 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/LinqToExcel.Tests/CompanyIgnoreIsActive.cs create mode 100644 src/LinqToExcel/Attributes/ExcelIgnoreAttribute.cs diff --git a/src/LinqToExcel.Tests/CompanyIgnoreIsActive.cs b/src/LinqToExcel.Tests/CompanyIgnoreIsActive.cs new file mode 100644 index 0000000..6e68a68 --- /dev/null +++ b/src/LinqToExcel.Tests/CompanyIgnoreIsActive.cs @@ -0,0 +1,10 @@ +using LinqToExcel.Attributes; + +namespace LinqToExcel.Tests +{ + class CompanyIgnoreIsActive : Company + { + [ExcelIgnore] + new public bool IsActive { get; set; } + } +} diff --git a/src/LinqToExcel.Tests/ExcelQueryFactoryTests.cs b/src/LinqToExcel.Tests/ExcelQueryFactoryTests.cs index 126bccc..e663759 100644 --- a/src/LinqToExcel.Tests/ExcelQueryFactoryTests.cs +++ b/src/LinqToExcel.Tests/ExcelQueryFactoryTests.cs @@ -168,6 +168,20 @@ public void StrictMapping_ClassStrict_with_additional_unused_worksheet_columns_d Assert.AreEqual(1, companies.Count); } + [Test] + public void StrictMapping_ClassStrict_with_ignore_attribute_doesnt_throw_exception() + { + var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory()); + excel.StrictMapping = StrictMappingType.ClassStrict; + + var companies = (from c in excel.Worksheet("More Companies") + where c.Name == "ACME" + select c).ToList(); + + Assert.AreEqual(1, companies.Count); + } + + [Test] public void StrictMapping_WorksheetStrict_throws_StrictMappingException_when_column_is_not_mapped_to_property() { @@ -194,6 +208,21 @@ public void StrictMapping_WorksheetStrict_with_additional_unused_class_propertie Assert.AreEqual(1, companies.Count); } + [Test] + public void StrictMapping_WorksheetStrict_with_ignore_attribute_throws_exception() + { + var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory()); + excel.StrictMapping = StrictMappingType.WorksheetStrict; + + var companies = (from c in excel.Worksheet("More Companies") + where c.Name == "ACME" + select c); + Assert.That(() => companies.ToList(), + Throws.TypeOf(), "'Active' column is not mapped to a property"); + + } + + [Test] public void StrictMapping_Both_throws_StrictMappingException_when_property_is_not_mapped_to_column() { @@ -230,6 +259,19 @@ public void StrictMapping_Both_with_column_mappings_doesnt_throw_exception() Assert.AreEqual(1, companies.Count); } + [Test] + public void StrictMapping_Both_with_ignore_attribute_doesnt_throw_exception() + { + var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory()); + excel.StrictMapping = StrictMappingType.Both; + + var companies = (from c in excel.Worksheet("Sheet1") + where c.Name == "ACME" + select c).ToList(); + + Assert.AreEqual(1, companies.Count); + } + [Test] public void StrictMapping_None_with_additional_worksheet_column_doesnt_throw_exception() { diff --git a/src/LinqToExcel.Tests/LinqToExcel.Tests.csproj b/src/LinqToExcel.Tests/LinqToExcel.Tests.csproj index 16eca35..66e719f 100644 --- a/src/LinqToExcel.Tests/LinqToExcel.Tests.csproj +++ b/src/LinqToExcel.Tests/LinqToExcel.Tests.csproj @@ -90,6 +90,7 @@ + diff --git a/src/LinqToExcel/Attributes/ExcelIgnoreAttribute.cs b/src/LinqToExcel/Attributes/ExcelIgnoreAttribute.cs new file mode 100644 index 0000000..2b095c4 --- /dev/null +++ b/src/LinqToExcel/Attributes/ExcelIgnoreAttribute.cs @@ -0,0 +1,15 @@ +using System; + +namespace LinqToExcel.Attributes +{ + /// + /// Ignores attribute during column map creation. Allows property to be safely ignored + /// when using StrictMappingType + /// + [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] + public sealed class ExcelIgnore : Attribute + { + public ExcelIgnore() + { } + } +} diff --git a/src/LinqToExcel/LinqToExcel.csproj b/src/LinqToExcel/LinqToExcel.csproj index 938a5a5..3fc1e86 100644 --- a/src/LinqToExcel/LinqToExcel.csproj +++ b/src/LinqToExcel/LinqToExcel.csproj @@ -82,6 +82,7 @@ + diff --git a/src/LinqToExcel/Query/ExcelQueryExecutor.cs b/src/LinqToExcel/Query/ExcelQueryExecutor.cs index 57da8fb..5e4c4aa 100644 --- a/src/LinqToExcel/Query/ExcelQueryExecutor.cs +++ b/src/LinqToExcel/Query/ExcelQueryExecutor.cs @@ -13,6 +13,7 @@ using System.Text; using LinqToExcel.Domain; using LinqToExcel.Logging; +using LinqToExcel.Attributes; namespace LinqToExcel.Query { @@ -372,7 +373,10 @@ private object TrimStringValue(object value) private void ConfirmStrictMapping(IEnumerable columns, PropertyInfo[] properties, StrictMappingType strictMappingType) { - var propertyNames = properties.Select(x => x.Name); + + var propertyNames = properties + .Where(x => (ExcelIgnore)Attribute.GetCustomAttribute(x, typeof(ExcelIgnore)) == null) + .Select(x => x.Name); if (strictMappingType == StrictMappingType.ClassStrict || strictMappingType == StrictMappingType.Both) { foreach (var propertyName in propertyNames) diff --git a/src/LinqToExcel/Query/ExcelQueryable.cs b/src/LinqToExcel/Query/ExcelQueryable.cs index 0d08f36..1e7a118 100644 --- a/src/LinqToExcel/Query/ExcelQueryable.cs +++ b/src/LinqToExcel/Query/ExcelQueryable.cs @@ -23,7 +23,9 @@ internal ExcelQueryable(ExcelQueryArgs args, ILogManagerFactory logManagerFactor foreach (var property in typeof(T).GetProperties()) { ExcelColumnAttribute att = (ExcelColumnAttribute)Attribute.GetCustomAttribute(property, typeof(ExcelColumnAttribute)); - if (att != null && !args.ColumnMappings.ContainsKey(property.Name)) + ExcelIgnore ignore = (ExcelIgnore)Attribute.GetCustomAttribute(property, typeof(ExcelIgnore)); + + if (att != null && !args.ColumnMappings.ContainsKey(property.Name) && (ignore == null)) { args.ColumnMappings.Add(property.Name, att.ColumnName); }