-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…e on nested classes
- Loading branch information
Showing
4 changed files
with
259 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/EPPlus/LoadFunctions/NestedColumnsSortorderScanner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/************************************************************************************************* | ||
Required Notice: Copyright (C) EPPlus Software AB. | ||
This software is licensed under PolyForm Noncommercial License 1.0.0 | ||
and may only be used for noncommercial purposes | ||
https://polyformproject.org/licenses/noncommercial/1.0.0/ | ||
A commercial license to use this software can be purchased at https://epplussoftware.com | ||
************************************************************************************************* | ||
Date Author Change | ||
************************************************************************************************* | ||
10/21/2023 EPPlus Software AB EPPlus 7.0.2 | ||
*************************************************************************************************/ | ||
using OfficeOpenXml.Attributes; | ||
using OfficeOpenXml.Core.CellStore; | ||
using OfficeOpenXml.Utils; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace OfficeOpenXml.LoadFunctions | ||
{ | ||
internal class NestedColumnsSortorderScanner | ||
{ | ||
public NestedColumnsSortorderScanner(Type outerType, BindingFlags bindingFlags) | ||
{ | ||
_sortOrder = new List<string>(); | ||
_bindingFlags = bindingFlags; | ||
ReadTypes(outerType); | ||
} | ||
|
||
private readonly List<string> _sortOrder = new List<string>(); | ||
private readonly BindingFlags _bindingFlags; | ||
|
||
private Type GetMemberType(MemberInfo member, string path) | ||
{ | ||
if (member.MemberType == MemberTypes.Property) | ||
{ | ||
return ((PropertyInfo)member).PropertyType; | ||
} | ||
else if (member.MemberType == MemberTypes.Field) | ||
{ | ||
return ((FieldInfo)member).FieldType; | ||
} | ||
else if (member.MemberType == MemberTypes.Method) | ||
{ | ||
return ((MethodInfo)member).ReturnType; | ||
} | ||
else | ||
{ | ||
throw new InvalidCastException($"Member {path} must be either Property, Field or Method but is {member.MemberType} which is not supported."); | ||
} | ||
} | ||
|
||
private string GetMemberName(MemberInfo member) | ||
{ | ||
if (member.MemberType == MemberTypes.Property) | ||
{ | ||
return ((PropertyInfo)member).Name; | ||
} | ||
else if (member.MemberType == MemberTypes.Field) | ||
{ | ||
return ((FieldInfo)member).Name; | ||
} | ||
else if (member.MemberType == MemberTypes.Method) | ||
{ | ||
return ((MethodInfo)member).Name; | ||
} | ||
else | ||
{ | ||
throw new InvalidCastException($"Member {member.Name} must be either Property, Field or Method but is {member.MemberType} which is not supported."); | ||
} | ||
} | ||
|
||
private void ReadTypes(Type type, string path = null) | ||
{ | ||
if (type.HasPropertyOfType<EPPlusTableColumnSortOrderAttribute>()) | ||
{ | ||
var sortOrderAttribute = type.GetFirstAttributeOfType<EPPlusTableColumnSortOrderAttribute>(); | ||
if (_sortOrder.Count == 0) | ||
{ | ||
_sortOrder.AddRange(sortOrderAttribute.Properties); | ||
} | ||
else | ||
{ | ||
var pathIndex = _sortOrder.IndexOf(path); | ||
var offset = 1; | ||
foreach (var prop in sortOrderAttribute.Properties) | ||
{ | ||
var fullPath = $"{path}.{prop}"; | ||
_sortOrder.Insert(pathIndex + offset++, fullPath); | ||
} | ||
//_sortOrder.Remove(path); | ||
} | ||
foreach (var member in type.GetProperties(_bindingFlags)) | ||
{ | ||
if (member.MemberType != MemberTypes.Property) continue; | ||
var memberName = GetMemberName(member); | ||
var memberPath = string.IsNullOrEmpty(path) ? member.Name : $"{path}.{memberName}"; | ||
var isNested = member.HasPropertyOfType<EpplusNestedTableColumnAttribute>(); | ||
if (isNested) | ||
{ | ||
var memberType = GetMemberType(member, memberPath); | ||
ReadTypes(memberType, memberPath); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
public List<string> GetSortOrder() | ||
{ | ||
return _sortOrder; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/EPPlusTest/LoadFunctions/LoadFromCollectionSortOrderAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using OfficeOpenXml; | ||
using OfficeOpenXml.Attributes; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace EPPlusTest.LoadFunctions | ||
{ | ||
[TestClass] | ||
public class LoadFromCollectionSortOrderAttributeTests | ||
{ | ||
[EPPlusTableColumnSortOrder(Properties = new string[] { nameof(Name), nameof(Obj), nameof(Id) })] | ||
[EpplusTable] | ||
public class Outer | ||
{ | ||
[EpplusTableColumn] | ||
public int Id { get; set; } | ||
|
||
[EpplusNestedTableColumn] | ||
public Inner Obj { get; set; } | ||
|
||
[EpplusTableColumn] | ||
public string Name { get; set; } | ||
} | ||
|
||
[EPPlusTableColumnSortOrder(Properties = new string[] { nameof(Name), nameof(Id) })] | ||
public class Inner | ||
{ | ||
[EpplusTableColumn] | ||
public int Id { get; set; } | ||
|
||
[EpplusTableColumn] | ||
public string Name { get; set; } | ||
} | ||
|
||
[EPPlusTableColumnSortOrder(Properties = new string[] { nameof(Name), nameof(Obj), nameof(Id) })] | ||
[EpplusTable] | ||
public class Outer2 | ||
{ | ||
[EpplusTableColumn] | ||
public int Id { get; set; } | ||
|
||
[EpplusNestedTableColumn] | ||
public Inner2 Obj { get; set; } | ||
|
||
[EpplusTableColumn] | ||
public string Name { get; set; } | ||
} | ||
|
||
[EPPlusTableColumnSortOrder(Properties = new string[] { nameof(Name), nameof(Id), nameof(Email) })] | ||
public class Inner2 | ||
{ | ||
[EpplusTableColumn] | ||
public int Id { get; set; } | ||
|
||
[EpplusTableColumn] | ||
public string Name { get; set; } | ||
|
||
[EpplusNestedTableColumn] | ||
public InnerEmail Email { get; set; } | ||
} | ||
|
||
[EPPlusTableColumnSortOrder(Properties = new string[] { nameof(Email), nameof(Id) })] | ||
public class InnerEmail | ||
{ | ||
[EpplusTableColumn] | ||
public int Id { get; set; } | ||
|
||
[EpplusTableColumn] | ||
public string Email { get; set; } | ||
} | ||
|
||
[TestMethod] | ||
public void SortBySortorderAttribute1() | ||
{ | ||
var outer = new Outer | ||
{ | ||
Id = 1, | ||
Obj = new Inner | ||
{ | ||
Id = 2, | ||
Name = "Inner" | ||
}, | ||
Name = "Outer" | ||
}; | ||
var items = new List<Outer> { outer }; | ||
using (var package = new ExcelPackage()) | ||
{ | ||
var sheet = package.Workbook.Worksheets.Add("Sheet 1"); | ||
sheet.Cells["A1"].LoadFromCollection(items); | ||
|
||
Assert.AreEqual("Outer", sheet.Cells["A1"].Value); | ||
Assert.AreEqual("Inner", sheet.Cells["B1"].Value); | ||
Assert.AreEqual(2, sheet.Cells["C1"].Value); | ||
Assert.AreEqual(1, sheet.Cells["D1"].Value); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void SortBySortorderAttribute2() | ||
{ | ||
var outer = new Outer2 | ||
{ | ||
Id = 1, | ||
Obj = new Inner2 | ||
{ | ||
Id = 2, | ||
Name = "Inner", | ||
Email = new InnerEmail | ||
{ | ||
Id = 3, | ||
Email = "[email protected]" | ||
} | ||
}, | ||
Name = "Outer" | ||
}; | ||
var items = new List<Outer2> { outer }; | ||
using (var package = new ExcelPackage()) | ||
{ | ||
var sheet = package.Workbook.Worksheets.Add("Sheet 1"); | ||
sheet.Cells["A1"].LoadFromCollection(items); | ||
|
||
Assert.AreEqual("Outer", sheet.Cells["A1"].Value); | ||
Assert.AreEqual("Inner", sheet.Cells["B1"].Value); | ||
Assert.AreEqual(2, sheet.Cells["C1"].Value); | ||
Assert.AreEqual("[email protected]", sheet.Cells["D1"].Value); | ||
Assert.AreEqual(3, sheet.Cells["E1"].Value); | ||
Assert.AreEqual(1, sheet.Cells["F1"].Value); | ||
} | ||
} | ||
} | ||
} |