Repository navigation
Expand file tree
/
Copy pathCompiledMapping.cs
More file actions
324 lines (273 loc) · 13.6 KB
/
Copy pathCompiledMapping.cs
File metadata and controls
324 lines (273 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
namespace MiniExcelLib.OpenXml.FluentMapping;
internal class CompiledMapping<T>
{
public string WorksheetName { get; set; } = "Sheet1";
public IReadOnlyList<CompiledPropertyMapping> Properties { get; set; } = new List<CompiledPropertyMapping>();
public IReadOnlyList<CompiledCollectionMapping> Collections { get; set; } = new List<CompiledCollectionMapping>();
// Optimization structures
/// <summary>
/// Pre-calculated cell grid for fast mapping.
/// Indexed as [row-relative][column-relative] where indices are relative to MinRow/MinColumn
/// </summary>
public OptimizedCellHandler[,]? OptimizedCellGrid { get; set; }
/// <summary>Mapping boundaries and optimization metadata</summary>
public OptimizedMappingBoundaries? OptimizedBoundaries { get; set; }
/// <summary>
/// For reading: array of column handlers indexed by (column - MinColumn).
/// Provides O(1) lookup from column index to property setter.
/// </summary>
public OptimizedCellHandler[]? OptimizedColumnHandlers { get; set; }
/// <summary>
/// Pre-compiled collection helpers for fast collection handling
/// </summary>
public IReadOnlyList<OptimizedCollectionHelper>? OptimizedCollectionHelpers { get; set; }
/// <summary>
/// Pre-compiled nested mapping information for complex collection types.
/// Indexed by collection index to provide fast access to nested property info.
/// </summary>
public IReadOnlyDictionary<int, NestedMappingInfo>? NestedMappings { get; set; }
/// <summary>
/// Tries to get the cell handler at the specified absolute row and column position.
/// </summary>
/// <param name="absoluteRow">The absolute row number (1-based)</param>
/// <param name="absoluteCol">The absolute column number (1-based)</param>
/// <param name="handler">The handler if found, or default if not</param>
/// <returns>True if a non-empty handler was found at the position</returns>
public bool TryGetHandler(int absoluteRow, int absoluteCol, out OptimizedCellHandler handler)
{
handler = default!;
if (OptimizedCellGrid is null || OptimizedBoundaries is null)
return false;
var relRow = absoluteRow - OptimizedBoundaries.MinRow;
var relCol = absoluteCol - OptimizedBoundaries.MinColumn;
if (relRow < 0 || relRow >= OptimizedBoundaries.GridHeight ||
relCol < 0 || relCol >= OptimizedBoundaries.GridWidth)
return false;
handler = OptimizedCellGrid[relRow, relCol];
return handler.Type != CellHandlerType.Empty;
}
/// <summary>
/// Tries to extract a value from an item using the specified handler.
/// </summary>
/// <typeparam name="TItem">The type of the item</typeparam>
/// <param name="handler">The cell handler containing the value extractor</param>
/// <param name="item">The item to extract the value from</param>
/// <param name="value">The extracted value, or null if extraction failed</param>
/// <returns>True if the value was successfully extracted</returns>
public bool TryGetValue<TItem>(OptimizedCellHandler handler, TItem? item, out object? value) where TItem : class
{
value = null;
if (item is null || handler.ValueExtractor is null)
return false;
value = handler.ValueExtractor(item, 0);
return true;
}
/// <summary>
/// Tries to set a value on an item using the specified handler.
/// </summary>
/// <typeparam name="TItem">The type of the item</typeparam>
/// <param name="handler">The cell handler containing the value setter</param>
/// <param name="item">The item to set the value on</param>
/// <param name="value">The value to set</param>
/// <returns>True if the value was successfully set</returns>
public bool TrySetValue<TItem>(OptimizedCellHandler handler, TItem? item, object? value) where TItem : class
{
if (item is null || handler.ValueSetter is null)
return false;
handler.ValueSetter(item, value);
return true;
}
/// <summary>
/// Convenience method that combines TryGetHandler and TryGetValue in a single call.
/// </summary>
/// <typeparam name="TItem">The type of the item</typeparam>
/// <param name="row">The absolute row number (1-based)</param>
/// <param name="col">The absolute column number (1-based)</param>
/// <param name="item">The item to extract the value from</param>
/// <param name="value">The extracted value, or null if not found</param>
/// <returns>True if a value was successfully extracted</returns>
public bool TryGetCellValue<TItem>(int row, int col, TItem item, out object? value) where TItem : class
{
value = null;
return TryGetHandler(row, col, out var handler) &&
TryGetValue(handler, item, out value);
}
/// <summary>
/// Convenience method that combines TryGetHandler and TrySetValue in a single call.
/// </summary>
/// <typeparam name="TItem">The type of the item</typeparam>
/// <param name="row">The absolute row number (1-based)</param>
/// <param name="col">The absolute column number (1-based)</param>
/// <param name="item">The item to set the value on</param>
/// <param name="value">The value to set</param>
/// <returns>True if the value was successfully set</returns>
public bool TrySetCellValue<TItem>(int row, int col, TItem item, object? value) where TItem : class
{
return TryGetHandler(row, col, out var handler) &&
TrySetValue(handler, item, value);
}
/// <summary>
/// Tries to set a property value on an item using the compiled property mapping.
/// </summary>
/// <typeparam name="TItem">The type of the item</typeparam>
/// <param name="property">The compiled property mapping</param>
/// <param name="item">The item to set the value on</param>
/// <param name="value">The value to set</param>
/// <returns>True if the value was successfully set</returns>
public bool TrySetPropertyValue<TItem>(CompiledPropertyMapping property, TItem item, object? value) where TItem : class
{
if (property.Setter is null)
return false;
property.Setter(item, value);
return true;
}
/// <summary>
/// Tries to set a collection value on an item using the compiled collection mapping.
/// </summary>
/// <typeparam name="TItem">The type of the item</typeparam>
/// <param name="collection">The compiled collection mapping</param>
/// <param name="item">The item to set the collection on</param>
/// <param name="value">The collection value to set</param>
/// <returns>True if the collection was successfully set</returns>
public bool TrySetCollectionValue<TItem>(CompiledCollectionMapping collection, TItem item, object? value) where TItem : class
{
if (collection.Setter is null)
return false;
collection.Setter(item, value);
return true;
}
}
/// <summary>
/// Pre-compiled helpers for collection handling
/// </summary>
internal class OptimizedCollectionHelper
{
public Func<IList> Factory { get; set; } = null!;
public Func<IList, object> Finalizer { get; set; } = null!;
public Action<object, object?>? Setter { get; set; }
public bool IsArray { get; set; }
public Func<object?> DefaultItemFactory { get; set; } = null!;
public Type ItemType { get; set; } = null!;
public bool IsItemValueType { get; set; }
public bool IsItemPrimitive { get; set; }
public object? DefaultValue { get; set; }
}
internal class CompiledPropertyMapping
{
public Func<object, object> Getter { get; set; } = null!;
public string CellAddress { get; set; } = null!;
public int CellColumn { get; set; } // Pre-parsed column index
public int CellRow { get; set; } // Pre-parsed row index
public string? Format { get; set; }
public string? Formula { get; set; }
public Type PropertyType { get; set; } = null!;
public string PropertyName { get; set; } = null!;
public Action<object, object?>? Setter { get; set; }
}
internal class CompiledCollectionMapping
{
public Func<object, IEnumerable> Getter { get; set; } = null!;
public int StartCellColumn { get; set; } // Pre-parsed column index
public int StartCellRow { get; set; } // Pre-parsed row index
public CollectionLayout Layout { get; set; }
public int RowSpacing { get; set; }
public Type? ItemType { get; set; }
public string PropertyName { get; set; } = null!;
public Action<object, object?>? Setter { get; set; }
public MappingRegistry? Registry { get; set; } // For looking up nested type mappings
}
/// <summary>
/// Defines the layout direction for collections in Excel mappings.
/// </summary>
internal enum CollectionLayout
{
/// <summary>Collections expand vertically (downward in rows)</summary>
Vertical = 0,
}
/// <summary>
/// Represents the type of data a cell contains in the mapping
/// </summary>
internal enum CellHandlerType
{
/// <summary>Cell is empty/unused</summary>
Empty,
/// <summary>Cell contains a simple property value</summary>
Property,
/// <summary>Cell contains an item from a collection</summary>
CollectionItem,
/// <summary>Cell contains a formula</summary>
Formula
}
/// <summary>
/// Pre-compiled handler for a specific cell in the mapping grid.
/// Contains all information needed to extract/set values for that cell without runtime parsing.
/// </summary>
internal class OptimizedCellHandler
{
/// <summary>Type of data this cell contains</summary>
public CellHandlerType Type { get; set; } = CellHandlerType.Empty;
/// <summary>For Property/Formula: direct property getter. For CollectionItem: collection getter + indexer</summary>
public Func<object, int, object?>? ValueExtractor { get; set; }
/// <summary>For reading: direct property setter with conversion built-in</summary>
public Action<object, object?>? ValueSetter { get; set; }
/// <summary>Property name for debugging/error reporting</summary>
public string? PropertyName { get; set; }
/// <summary>For collection items: which collection this belongs to</summary>
public int CollectionIndex { get; set; } = -1;
/// <summary>For collection items: offset within collection</summary>
public int CollectionItemOffset { get; set; }
/// <summary>For formulas: the formula text</summary>
public string? Formula { get; set; }
/// <summary>For formatted values: the format string</summary>
public string? Format { get; set; }
/// <summary>For collection items: reference to the collection mapping</summary>
public CompiledCollectionMapping? CollectionMapping { get; set; }
/// <summary>For collection items: pre-compiled converter from cell value to collection item type</summary>
public Func<object?, object?>? CollectionItemConverter { get; set; }
/// <summary>
/// For multiple items scenario: which item (0, 1, 2...) this handler belongs to.
/// -1 means this handler applies to all items (unbounded collection).
/// </summary>
public int ItemIndex { get; set; }
/// <summary>
/// For collection handlers: the row where this collection stops reading (exclusive).
/// -1 means unbounded (continue until no more data).
/// </summary>
public int BoundaryRow { get; set; } = -1;
/// <summary>
/// For collection handlers: the column where this collection stops reading (exclusive).
/// -1 means unbounded (continue until no more data).
/// </summary>
public int BoundaryColumn { get; set; } = -1;
}
/// <summary>
/// Optimized mapping boundaries and metadata
/// </summary>
internal class OptimizedMappingBoundaries
{
/// <summary>Minimum row used by any mapping (1-based)</summary>
public int MinRow { get; set; } = int.MaxValue;
/// <summary>Maximum row used by any mapping (1-based)</summary>
public int MaxRow { get; set; }
/// <summary>Minimum column used by any mapping (1-based)</summary>
public int MinColumn { get; set; } = int.MaxValue;
/// <summary>Maximum column used by any mapping (1-based)</summary>
public int MaxColumn { get; set; }
/// <summary>Width of the cell grid (MaxColumn - MinColumn + 1)</summary>
public int GridWidth => MaxColumn > 0 ? MaxColumn - MinColumn + 1 : 0;
/// <summary>Height of the cell grid (MaxRow - MinRow + 1)</summary>
public int GridHeight => MaxRow > 0 ? MaxRow - MinRow + 1 : 0;
/// <summary>Whether this mapping has collections that can expand dynamically</summary>
public bool HasDynamicCollections { get; set; }
/// <summary>
/// For multiple items with collections: the height of the repeating pattern.
/// This is the distance from one item's properties to the next item's properties.
/// 0 means no repeating pattern (single item or no collections).
/// </summary>
public int PatternHeight { get; set; }
/// <summary>
/// For multiple items: whether this mapping supports multiple items with collections.
/// When true, the grid pattern repeats every PatternHeight rows.
/// </summary>
public bool IsMultiItemPattern { get; set; }
}