| | | 1 | | using CsvHelper.Configuration; |
| | | 2 | | using EHonda.KicktippAi.Core; |
| | | 3 | | |
| | | 4 | | namespace ContextProviders.Kicktipp.Csv; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Extension methods for creating <see cref="DocumentContext"/> from CSV data. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class CsvDocumentContextExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Creates a <see cref="DocumentContext"/> from a collection of records using the specified ClassMap. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// <para> |
| | | 16 | | /// This method requires both <typeparamref name="T"/> and <typeparamref name="TMap"/> to be specified |
| | | 17 | | /// explicitly at call sites, even though <typeparamref name="T"/> could theoretically be inferred from |
| | | 18 | | /// the <paramref name="records"/> parameter. |
| | | 19 | | /// </para> |
| | | 20 | | /// <para> |
| | | 21 | | /// C# 14 extension blocks would allow defining this as a method where only <typeparamref name="TMap"/> |
| | | 22 | | /// needs to be specified. However, extension block methods with non-inferable type parameters (i.e., |
| | | 23 | | /// type parameters that only appear in constraints, not in the parameter list) are not supported in |
| | | 24 | | /// the current preview. Since <typeparamref name="TMap"/> only appears in the constraint |
| | | 25 | | /// <c>where TMap : ClassMap<T>, new()</c> and not as a method parameter, this pattern cannot |
| | | 26 | | /// be used. |
| | | 27 | | /// </para> |
| | | 28 | | /// </remarks> |
| | | 29 | | /// <typeparam name="T">The type of records to write.</typeparam> |
| | | 30 | | /// <typeparam name="TMap">The ClassMap defining the CSV schema.</typeparam> |
| | | 31 | | /// <param name="records">The collection of records to write.</param> |
| | | 32 | | /// <param name="fileName">The file name for the document (without .csv extension).</param> |
| | | 33 | | /// <returns>A DocumentContext containing the CSV content.</returns> |
| | | 34 | | public static DocumentContext ToCsvDocumentContext<T, TMap>(this IEnumerable<T> records, string fileName) |
| | | 35 | | where T : class |
| | | 36 | | where TMap : ClassMap<T>, new() |
| | | 37 | | { |
| | 1 | 38 | | var csvContent = records.WriteToCsv<T, TMap>(); |
| | 1 | 39 | | return new DocumentContext(Name: $"{fileName}.csv", Content: csvContent); |
| | | 40 | | } |
| | | 41 | | } |