3 Commits e9e0b11161 ... 0e06e19a97

Author SHA1 Message Date
  Piotr Czajkowski 0e06e19a97 Sugar 4 months ago
  Piotr Czajkowski 2a6bd983cf Clearer? 4 months ago
  Piotr Czajkowski b09f0dd78d Cosmetics 4 months ago

+ 2 - 2
ExcelORM/ExcelORM/Attributes/ColumnAttribute.cs

@@ -1,4 +1,4 @@
-namespace ExcelORM
+namespace ExcelORM.Attributes
 {
     [AttributeUsage(AttributeTargets.Property)]
     public class ColumnAttribute : Attribute
@@ -7,7 +7,7 @@
 
         public ColumnAttribute(string name)
         {
-            Names = new string[] { name };
+            Names = new[] { name };
         }
 
         public ColumnAttribute(string[] names)

+ 1 - 1
ExcelORM/ExcelORM/ExcelORM.csproj

@@ -4,7 +4,7 @@
         <TargetFramework>net7.0</TargetFramework>
         <ImplicitUsings>enable</ImplicitUsings>
         <Nullable>enable</Nullable>
-        <Version>1.0.1</Version>
+        <Version>1.0.2</Version>
         <PackageProjectUrl>https://git.liox.eu/pczajkowski/ExcelORM</PackageProjectUrl>
         <RepositoryUrl>https://github.com/pczajkowski/ExcelORM</RepositoryUrl>
         <GeneratePackageOnBuild>true</GeneratePackageOnBuild>

+ 1 - 0
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -1,4 +1,5 @@
 using ClosedXML.Excel;
+using ExcelORM.Attributes;
 
 namespace ExcelORM;
 

+ 10 - 8
ExcelORM/ExcelORM/Mapping.cs

@@ -1,4 +1,5 @@
 using ClosedXML.Excel;
+using ExcelORM.Attributes;
 
 namespace ExcelORM
 {
@@ -15,19 +16,20 @@ namespace ExcelORM
             var properties = typeof(T).GetProperties();
             foreach (var property in properties)
             {
-                var position = property.GetCustomAttributes(typeof(ColumnAttribute), false).FirstOrDefault() is ColumnAttribute { Names.Length: > 0 } columnAttribute
-                    ?
-                    headerCells.FirstOrDefault(x => !x.Value.IsBlank && Array.Exists(columnAttribute.Names, y => y.Equals(x.Value.ToString(), StringComparison.InvariantCultureIgnoreCase)))?.Address.ColumnNumber
-                    : headerCells.FirstOrDefault(x => !x.Value.IsBlank && x.Value.ToString().Equals(property.Name, StringComparison.InvariantCultureIgnoreCase))?.Address.ColumnNumber;
+                int? position;
+
+                if (property.GetCustomAttributes(typeof(ColumnAttribute), false).FirstOrDefault() is ColumnAttribute { Names.Length: > 0 } attribute)
+                    position = headerCells.FirstOrDefault(x => !x.Value.IsBlank && Array.Exists(attribute.Names,
+                            y => y.Equals(x.Value.ToString(), StringComparison.InvariantCultureIgnoreCase)))?.Address
+                        .ColumnNumber;
+                else
+                    position = headerCells.FirstOrDefault(x => !x.Value.IsBlank && property.Name.Equals(x.Value.ToString(), StringComparison.InvariantCultureIgnoreCase))?.Address.ColumnNumber;
 
                 if (position == null) continue;
                 map.Add(new Mapping { PropertyName = property.Name, Position = position });
             }
 
-            if (map.Count == properties.Length)
-                return map;
-
-            return null;
+            return map.Count == properties.Length ? map : null;
         }
     }
 }

+ 1 - 1
ExcelORM/ExcelORMTests/Test.cs

@@ -1,4 +1,4 @@
-using ExcelORM;
+using ExcelORM.Attributes;
 
 namespace ExcelORMTests;