Browse Source

Able to read additional types

Piotr Czajkowski 1 week ago
parent
commit
ac7cb1381b

+ 28 - 3
ExcelORM/ExcelORM/TypeExtensions.cs

@@ -6,15 +6,40 @@ namespace ExcelORM;
 
 
 public static class TypeExtensions
 public static class TypeExtensions
 {
 {
+    private static object? GetAdditionalTypeFromText(XLCellValue value, PropertyInfo? property = null)
+    {
+        if (property == null) return value.GetText();
+        
+        if (property.PropertyType == typeof(Guid?))
+        {
+            if (Guid.TryParse(value.GetText(), out var guid))
+                return guid;
+
+            return null;
+        }
+
+        if (Nullable.GetUnderlyingType(property.PropertyType) != null)
+        {
+            var genericType = property.PropertyType.GetGenericArguments().FirstOrDefault();
+            if (genericType == null) return null;
+            
+            if (genericType.IsEnum)
+                return Enum.TryParse(genericType, value.GetText(), true, out var enumValue)
+                    ? enumValue : null;
+        }
+        
+        return value.GetText(); 
+    }
+    
     // Borrowed from https://github.com/ClosedXML/ClosedXML/blob/develop/ClosedXML/Excel/XLCellValue.cs#L361
     // Borrowed from https://github.com/ClosedXML/ClosedXML/blob/develop/ClosedXML/Excel/XLCellValue.cs#L361
-    public static object? ToObject(this XLCellValue value)
+    public static object? ToObject(this XLCellValue value, PropertyInfo? property = null)
     {
     {
         return value.Type switch
         return value.Type switch
         {
         {
             XLDataType.Blank => null,
             XLDataType.Blank => null,
             XLDataType.Boolean => value.GetBoolean(),
             XLDataType.Boolean => value.GetBoolean(),
             XLDataType.Number => value.GetNumber(),
             XLDataType.Number => value.GetNumber(),
-            XLDataType.Text => value.GetText(),
+            XLDataType.Text => GetAdditionalTypeFromText(value, property),
             XLDataType.Error => value.GetError(),
             XLDataType.Error => value.GetError(),
             XLDataType.DateTime => value.GetDateTime(),
             XLDataType.DateTime => value.GetDateTime(),
             XLDataType.TimeSpan => value.GetTimeSpan(),
             XLDataType.TimeSpan => value.GetTimeSpan(),
@@ -38,7 +63,7 @@ public static class TypeExtensions
 
 
     public static void SetPropertyValue<T>(this T currentObject, PropertyInfo property, XLCellValue value)
     public static void SetPropertyValue<T>(this T currentObject, PropertyInfo property, XLCellValue value)
     {
     {
-        var valueToSet = value.ToObject();
+        var valueToSet = value.ToObject(property);
         if (valueToSet == null) return;
         if (valueToSet == null) return;
 
 
         try
         try

+ 3 - 0
ExcelORM/ExcelORMTests/ExcelORMTests.csproj

@@ -59,6 +59,9 @@
       <None Update="testFiles\forAppendWithRubbish.xlsx">
       <None Update="testFiles\forAppendWithRubbish.xlsx">
         <CopyToOutputDirectory>Always</CopyToOutputDirectory>
         <CopyToOutputDirectory>Always</CopyToOutputDirectory>
       </None>
       </None>
+      <None Update="testFiles\additionalTypes.xlsx">
+        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+      </None>
     </ItemGroup>
     </ItemGroup>
 
 
     <ItemGroup>
     <ItemGroup>

+ 9 - 0
ExcelORM/ExcelORMTests/ReaderTests.cs

@@ -12,6 +12,7 @@ public class ReaderTests
     private const string DifferentTypesFile = "testFiles/differentTypes.xlsx";
     private const string DifferentTypesFile = "testFiles/differentTypes.xlsx";
     private const string WithFormulaFile = "testFiles/withFormula.xlsx";
     private const string WithFormulaFile = "testFiles/withFormula.xlsx";
     private const string BadDate = "testFiles/badDate.xlsx";
     private const string BadDate = "testFiles/badDate.xlsx";
+    private const string AdditionalTypes = "testFiles/additionalTypes.xlsx";
     
     
     [Fact]
     [Fact]
     public void Read()
     public void Read()
@@ -118,4 +119,12 @@ public class ReaderTests
         var exception = Assert.Throws<ArgumentException>(() => reader.Read<TestTypes>().ToArray());
         var exception = Assert.Throws<ArgumentException>(() => reader.Read<TestTypes>().ToArray());
         Assert.Contains("Location", exception.Message);
         Assert.Contains("Location", exception.Message);
     }
     }
+    
+    [Fact]
+    public void ReadAdditionalTypes()
+    {
+        using var reader = new ExcelReader(AdditionalTypes);
+        var results = reader.Read<TestAdditionalTypes>().ToArray();
+        Assert.NotEmpty(results);
+    }
 }
 }

+ 7 - 0
ExcelORM/ExcelORMTests/TestAdditionalTypes.cs

@@ -0,0 +1,7 @@
+namespace ExcelORMTests;
+
+public record TestAdditionalTypes
+{
+    public TestEnum? MyEnum {get; set;}
+    public Guid? MyGuid {get; set;}
+}

+ 7 - 0
ExcelORM/ExcelORMTests/TestEnum.cs

@@ -0,0 +1,7 @@
+namespace ExcelORMTests;
+
+public enum TestEnum
+{
+    First,
+    Second,
+}

BIN
ExcelORM/ExcelORMTests/testFiles/additionalTypes.xlsx