Skip to content

Commit ca5a620

Browse files
Various adjustments
- Added explicit IEnumerator implementation to MappingCellEnumerator for clarity - Straightened recursive implementation of MappingCellEnumerator's MoveNext into a loop - Expanded one of the tests - Changed some checks to pattern matching - Some other minor style changes
1 parent c99ed28 commit ca5a620

File tree

6 files changed

+335
-330
lines changed

6 files changed

+335
-330
lines changed

src/MiniExcel.Core/Helpers/MappingMetadataExtractor.cs

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ namespace MiniExcelLib.Core.Helpers;
66
/// </summary>
77
internal static class MappingMetadataExtractor
88
{
9+
private static readonly MethodInfo? CreateTypedSetterMethod = typeof(ConversionHelper)
10+
.GetMethods(BindingFlags.Public | BindingFlags.Static)
11+
.FirstOrDefault(m => m is { Name: nameof(ConversionHelper.CreateTypedPropertySetter), IsGenericMethodDefinition: true });
12+
13+
private static readonly Action<object, object?> DefaultNestedPropertySetter = (_, _) => { };
14+
915
/// <summary>
1016
/// Extracts nested mapping information from a compiled mapping object.
1117
/// This method minimizes reflection by extracting properties once at compile time.
@@ -33,48 +39,47 @@ internal static class MappingMetadataExtractor
3339
nestedInfo.Properties = propertyList;
3440

3541
var collectionsProperty = nestedMappingType.GetProperty("Collections");
36-
if (collectionsProperty?.GetValue(nestedMapping) is IEnumerable collectionMappings)
42+
if (collectionsProperty?.GetValue(nestedMapping) is not IEnumerable collectionMappings)
43+
return nestedInfo;
44+
45+
var nestedCollections = new Dictionary<string, NestedCollectionInfo>(StringComparer.Ordinal);
46+
foreach (var collection in collectionMappings)
3747
{
38-
var nestedCollections = new Dictionary<string, NestedCollectionInfo>(StringComparer.Ordinal);
48+
if (collection is not CompiledCollectionMapping compiledCollection)
49+
continue;
3950

40-
foreach (var collection in collectionMappings)
51+
var nestedItemType = compiledCollection.ItemType ?? typeof(object);
52+
var collectionInfo = new NestedCollectionInfo
4153
{
42-
if (collection is not CompiledCollectionMapping compiledCollection)
43-
continue;
44-
45-
var nestedItemType = compiledCollection.ItemType ?? typeof(object);
46-
var collectionInfo = new NestedCollectionInfo
47-
{
48-
PropertyName = compiledCollection.PropertyName,
49-
StartColumn = compiledCollection.StartCellColumn,
50-
StartRow = compiledCollection.StartCellRow,
51-
Layout = compiledCollection.Layout,
52-
RowSpacing = compiledCollection.RowSpacing,
53-
ItemType = nestedItemType,
54-
Getter = compiledCollection.Getter,
55-
Setter = compiledCollection.Setter,
56-
ListFactory = () => CollectionAccessor.CreateTypedList(nestedItemType),
57-
ItemFactory = CollectionAccessor.CreateItemFactory(nestedItemType)
58-
};
59-
60-
if (compiledCollection.Registry is not null && nestedItemType != typeof(object))
54+
PropertyName = compiledCollection.PropertyName,
55+
StartColumn = compiledCollection.StartCellColumn,
56+
StartRow = compiledCollection.StartCellRow,
57+
Layout = compiledCollection.Layout,
58+
RowSpacing = compiledCollection.RowSpacing,
59+
ItemType = nestedItemType,
60+
Getter = compiledCollection.Getter,
61+
Setter = compiledCollection.Setter,
62+
ListFactory = () => CollectionAccessor.CreateTypedList(nestedItemType),
63+
ItemFactory = CollectionAccessor.CreateItemFactory(nestedItemType)
64+
};
65+
66+
if (compiledCollection.Registry is not null && nestedItemType != typeof(object))
67+
{
68+
var childMapping = compiledCollection.Registry.GetCompiledMapping(nestedItemType);
69+
if (childMapping is not null)
6170
{
62-
var childMapping = compiledCollection.Registry.GetCompiledMapping(nestedItemType);
63-
if (childMapping is not null)
64-
{
65-
collectionInfo.NestedMapping = ExtractNestedMappingInfo(childMapping, nestedItemType);
66-
}
71+
collectionInfo.NestedMapping = ExtractNestedMappingInfo(childMapping, nestedItemType);
6772
}
68-
69-
nestedCollections[collectionInfo.PropertyName] = collectionInfo;
7073
}
7174

72-
if (nestedCollections.Count > 0)
73-
{
74-
nestedInfo.Collections = nestedCollections;
75-
}
75+
nestedCollections[collectionInfo.PropertyName] = collectionInfo;
7676
}
77-
77+
78+
if (nestedCollections.Count > 0)
79+
{
80+
nestedInfo.Collections = nestedCollections;
81+
}
82+
7883
return nestedInfo;
7984
}
8085

@@ -83,10 +88,6 @@ internal static class MappingMetadataExtractor
8388
/// </summary>
8489
/// <param name="properties">The collection of property mappings</param>
8590
/// <returns>A list of nested property information</returns>
86-
private static readonly MethodInfo? CreateTypedSetterMethod = typeof(ConversionHelper)
87-
.GetMethods(BindingFlags.Public | BindingFlags.Static)
88-
.FirstOrDefault(m => m.Name == nameof(ConversionHelper.CreateTypedPropertySetter) && m.IsGenericMethodDefinition);
89-
9091
private static List<NestedPropertyInfo> ExtractPropertyList(IEnumerable properties, Type itemType)
9192
{
9293
var propertyList = new List<NestedPropertyInfo>();
@@ -119,7 +120,7 @@ private static List<NestedPropertyInfo> ExtractPropertyList(IEnumerable properti
119120
}
120121
}
121122

122-
setter ??= (_, _) => { };
123+
setter ??= DefaultNestedPropertySetter;
123124

124125
if (name is not null && getter is not null)
125126
{
@@ -145,7 +146,7 @@ private static List<NestedPropertyInfo> ExtractPropertyList(IEnumerable properti
145146
try
146147
{
147148
var generic = CreateTypedSetterMethod.MakeGenericMethod(itemType);
148-
return generic.Invoke(null, new object[] { propertyInfo }) as Action<object, object?>;
149+
return generic.Invoke(null, [propertyInfo]) as Action<object, object?>;
149150
}
150151
catch
151152
{
@@ -179,7 +180,9 @@ private static List<NestedPropertyInfo> ExtractPropertyList(IEnumerable properti
179180

180181
private static bool IsSimpleType(Type type)
181182
{
182-
return type == typeof(string) || type.IsValueType || type.IsPrimitive;
183+
return type == typeof(string) ||
184+
type.IsValueType ||
185+
type.IsPrimitive;
183186
}
184187

185188
/// <summary>

0 commit comments

Comments
 (0)