Skip to content

Commit

Permalink
fix expression tree with value type check
Browse files Browse the repository at this point in the history
  • Loading branch information
donnytian committed Sep 22, 2023
1 parent f3bf446 commit 1199c15
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
23 changes: 15 additions & 8 deletions Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,13 @@ public static (PropertyInfo propertyInfo, string fullPath) GetPropertyInfo<T>(st
{
var memberAccess = body as MemberExpression ?? (MemberExpression)((UnaryExpression)body).Operand;

newBody = Expression.Condition(
Expression.Equal(memberAccess.Expression, Expression.Constant(null)),
Expression.Convert(Expression.Constant(null), ObjectType),
newBody);
if (!memberAccess.Expression.Type.IsValueType)
{
newBody = Expression.Condition(
Expression.Equal(memberAccess.Expression, Expression.Constant(null)),
Expression.Convert(Expression.Constant(null), ObjectType),
newBody);
}

body = memberAccess.Expression;
}
Expand Down Expand Up @@ -581,11 +584,15 @@ public static (PropertyInfo propertyInfo, string fullPath) GetPropertyInfo<T>(st
{
var memberAccess = innerBody as MemberExpression ?? (MemberExpression)((UnaryExpression)innerBody).Operand;

var assignNewObjExpression = Expression.IfThen(
Expression.Equal(memberAccess, Expression.Constant(null)),
Expression.Assign(memberAccess, Expression.New(memberAccess.Type)));
if (!memberAccess.Type.IsValueType)
{
var ifNullAssignNew = Expression.IfThen(
Expression.Equal(memberAccess, Expression.Constant(null)),
Expression.Assign(memberAccess, Expression.New(memberAccess.Type)));

statements.Add(ifNullAssignNew);
}

statements.Add(assignNewObjExpression);
innerBody = memberAccess.Expression;
}

Expand Down
4 changes: 0 additions & 4 deletions Npoi.Mapper/src/Npoi.Mapper/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -812,13 +812,11 @@ void ColumnFailed(IColumnInfo column, string message)
if (MapHelper.TryConvertType(valueObj, column, UseDefaultValueAttribute, out var result))
{
column.Attribute.GetSetterOrDefault(target)?.Invoke(target, result);
//column.Attribute.Property.SetValue(target, result, null);
}
else
{
ColumnFailed(column, "Cannot convert value to the property type!");
}
//var value = Convert.ChangeType(valueObj, column.Attribute.PropertyUnderlyingType ?? propertyType);
}
}
catch (Exception e)
Expand Down Expand Up @@ -909,8 +907,6 @@ private void Put<T>(ISheet sheet, IEnumerable<T> objects, bool overwrite)

foreach (var column in columns)
{
//var pi = column.Attribute.Property;
//var value = pi?.GetValue(o, null);
var value = column.Attribute.GetGetterOrDefault(o)?.Invoke(o);
var cell = row.GetCell(column.Attribute.Index, MissingCellPolicy.CREATE_NULL_AS_BLANK);

Expand Down
22 changes: 21 additions & 1 deletion Npoi.Mapper/test/NestedPropertyTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using Npoi.Mapper;
using NUnit.Framework;
Expand All @@ -19,6 +20,7 @@ private class CustomerBilling
{
public BillingAddress Address { get; set; }
public BillingContact Contact { get; set; }
public MyStruct Dates;
}

private class BillingAddress
Expand All @@ -31,6 +33,11 @@ private class BillingContact
public string Name { get; set; }
}

private struct MyStruct
{
public DateTimeOffset Dto { get; set; }
}

[Test]
public void ImportWithNestedPropertiesTest()
{
Expand All @@ -39,6 +46,7 @@ public void ImportWithNestedPropertiesTest()
const string contactName = "tian";
const int customerAge = 33;
const int addressId = 4321;
var date = DateTime.Now;
var workbook = GetBlankWorkbook();
var sheet = workbook.GetSheetAt(0);
var row0 = sheet.CreateRow(0);
Expand All @@ -49,23 +57,27 @@ public void ImportWithNestedPropertiesTest()
row0.CreateCell(1).SetCellValue("customer age");
row0.CreateCell(2).SetCellValue("contact name");
row0.CreateCell(3).SetCellValue("address id");
row0.CreateCell(4).SetCellValue("birth date");

row1.CreateCell(0).SetCellValue(customerName);
row1.CreateCell(1).SetCellValue(customerAge);
row1.CreateCell(2).SetCellValue(contactName);
row1.CreateCell(3).SetCellValue(addressId);
row1.CreateCell(4).SetCellValue(date);

row2.CreateCell(0).SetCellValue("");
row2.CreateCell(1).SetCellValue(customerAge.ToString());
row2.CreateCell(2).SetCellValue((string)null);
row2.CreateCell(3).SetCellValue("");
row2.CreateCell(4).SetCellValue("");

// Act
var mapper = new Mapper(workbook);
mapper.Map<Customer>(0, c => c.Name);
mapper.Map<Customer>(1, c => c.Age);
mapper.Map<Customer>(2, c => c.Billing.Contact.Name);
mapper.Map<Customer>(3, c => c.Billing.Address.AddressId);
mapper.Map<Customer>(4, c => c.Billing.Dates.Dto);

var objs = mapper.Take<Customer>().ToList();

Expand All @@ -75,11 +87,13 @@ public void ImportWithNestedPropertiesTest()
Assert.AreEqual(customerAge, objs[0].Value.Age);
Assert.AreEqual(contactName, objs[0].Value.Billing.Contact.Name);
Assert.AreEqual(addressId, objs[0].Value.Billing.Address.AddressId);
Assert.AreEqual(date.Date, objs[0].Value.Billing.Dates.Dto.Date);

Assert.AreEqual("", objs[1].Value.Name);
Assert.AreEqual(customerAge, objs[1].Value.Age);
Assert.AreEqual("", objs[1].Value.Billing.Contact.Name);
Assert.AreEqual(null, objs[1].Value.Billing.Address);
Assert.AreEqual(DateTime.MinValue, objs[1].Value.Billing.Dates.Dto.Date);
}

[Test]
Expand All @@ -93,6 +107,7 @@ public void ExportWithNestedPropertiesTest()
const int customerAge = 33;
const int addressId = 4321;
const int addressId2 = 3333;
var date = DateTime.Now;
var customer1 = new Customer
{
Age = customerAge,
Expand All @@ -101,6 +116,7 @@ public void ExportWithNestedPropertiesTest()
{
Address = new BillingAddress { AddressId = addressId },
Contact = new BillingContact { Name = contactName },
Dates = new MyStruct { Dto = date },
},
};
var customer2 = new Customer
Expand All @@ -120,6 +136,7 @@ public void ExportWithNestedPropertiesTest()
mapper.Map<Customer>(1, c => c.Age);
mapper.Map<Customer>(2, c => c.Billing.Contact.Name);
mapper.Map<Customer>(3, c => c.Billing.Address.AddressId);
mapper.Map<Customer>(4, c => c.Billing.Dates.Dto);

mapper.Save(fileName, entities, false);

Expand All @@ -134,15 +151,18 @@ public void ExportWithNestedPropertiesTest()
Assert.AreEqual(nameof(Customer.Age), row0.GetCell(1).StringCellValue);
Assert.AreEqual(nameof(Customer.Billing.Contact.Name), row0.GetCell(2).StringCellValue);
Assert.AreEqual(nameof(Customer.Billing.Address.AddressId), row0.GetCell(3).StringCellValue);
Assert.AreEqual(nameof(Customer.Billing.Dates.Dto), row0.GetCell(4).StringCellValue);

Assert.AreEqual(customer1.Name, row1.GetCell(0).StringCellValue);
Assert.AreEqual(customer1.Age, row1.GetCell(1).NumericCellValue);
Assert.AreEqual(customer1.Billing.Contact.Name, row1.GetCell(2).StringCellValue);
Assert.AreEqual(customer1.Billing.Address.AddressId, row1.GetCell(3).NumericCellValue);
Assert.AreEqual(customer1.Billing.Dates.Dto.Date, DateTimeOffset.Parse(row1.GetCell(4).StringCellValue).Date);

Assert.AreEqual(customer2.Name ?? "", row2.GetCell(0).StringCellValue);
Assert.AreEqual(customer2.Age ?? 0.0, row2.GetCell(1).NumericCellValue);
Assert.AreEqual("", row2.GetCell(2).StringCellValue);
Assert.AreEqual(customer2.Billing.Address.AddressId, row2.GetCell(3).NumericCellValue);
Assert.AreEqual(customer2.Billing.Dates.Dto.Date, DateTimeOffset.Parse(row2.GetCell(4).StringCellValue).Date);
}
}

0 comments on commit 1199c15

Please sign in to comment.