Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#114 Add 'ExcelIgnore' Attribute. #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/LinqToExcel.Tests/CompanyIgnoreIsActive.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using LinqToExcel.Attributes;

namespace LinqToExcel.Tests
{
class CompanyIgnoreIsActive : Company
{
[ExcelIgnore]
new public bool IsActive { get; set; }
}
}
42 changes: 42 additions & 0 deletions src/LinqToExcel.Tests/ExcelQueryFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompanyIgnoreIsActive>("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()
{
Expand All @@ -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<CompanyIgnoreIsActive>("More Companies")
where c.Name == "ACME"
select c);
Assert.That(() => companies.ToList(),
Throws.TypeOf<StrictMappingException>(), "'Active' column is not mapped to a property");

}


[Test]
public void StrictMapping_Both_throws_StrictMappingException_when_property_is_not_mapped_to_column()
{
Expand Down Expand Up @@ -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<CompanyIgnoreIsActive>("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()
{
Expand Down
1 change: 1 addition & 0 deletions src/LinqToExcel.Tests/LinqToExcel.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Compile Include="ColumnMappings_IntegrationTests.cs" />
<Compile Include="ColumnMappings_SQLStatements_UnitTests.cs" />
<Compile Include="CompanyNullable.cs" />
<Compile Include="CompanyIgnoreIsActive.cs" />
<Compile Include="CompanyWithColumnAnnotations.cs" />
<Compile Include="CompanyWithCity.cs" />
<Compile Include="ConfiguredWorksheetName_SQLStatements_UnitTests.cs" />
Expand Down
15 changes: 15 additions & 0 deletions src/LinqToExcel/Attributes/ExcelIgnoreAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace LinqToExcel.Attributes
{
/// <summary>
/// Ignores attribute during column map creation. Allows property to be safely ignored
/// when using StrictMappingType
/// </summary>
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class ExcelIgnore : Attribute
{
public ExcelIgnore()
{ }
}
}
1 change: 1 addition & 0 deletions src/LinqToExcel/LinqToExcel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\ExcelIgnoreAttribute.cs" />
<Compile Include="Attributes\ExcelColumnAttribute.cs" />
<Compile Include="Domain\Cell.cs" />
<Compile Include="Domain\RowNoHeader.cs" />
Expand Down
6 changes: 5 additions & 1 deletion src/LinqToExcel/Query/ExcelQueryExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Text;
using LinqToExcel.Domain;
using LinqToExcel.Logging;
using LinqToExcel.Attributes;

namespace LinqToExcel.Query
{
Expand Down Expand Up @@ -372,7 +373,10 @@ private object TrimStringValue(object value)

private void ConfirmStrictMapping(IEnumerable<string> 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)
Expand Down
4 changes: 3 additions & 1 deletion src/LinqToExcel/Query/ExcelQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down