< Summary - SonghayCore

Information
Class: Songhay.Text.CsvExporter<T>
Assembly: SonghayCore
File(s): /home/rasx/sourceRoot/SonghayCore/SonghayCore/Text/CsvExporter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 65
Coverable lines: 65
Total lines: 130
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 34
Branch coverage: 0%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%10%
.ctor(...)0%40%
get_Columns()100%10%
get_Rows()100%10%
Export()100%10%
Export(...)0%180%
ExportToFile(...)100%10%
ExportToBytes()100%10%
MakeCsvText(...)0%120%

File(s)

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Text/CsvExporter.cs

#LineLine coverage
 1namespace Songhay.Text;
 2
 3/// <summary>
 4/// Transforms and exports the specified class to CSV format.
 5/// </summary>
 6/// <typeparam name="T">the class to export</typeparam>
 7/// <remarks>
 8/// Based on http://stackoverflow.com/questions/2422212/simple-c-sharp-csv-excel-export-class
 9/// </remarks>
 10public class CsvExporter<T> where T : class
 11{
 12    /// <summary>
 13    /// Initializes a new instance of the <see cref="CsvExporter{T}"/> class.
 14    /// </summary>
 15    /// <param name="rows">The rows.</param>
 16    public CsvExporter(IEnumerable<T>? rows)
 017        : this(rows, null)
 018    {
 019    }
 20
 21    /// <summary>
 22    /// Initializes a new instance of the <see cref="CsvExporter{T}"/> class.
 23    /// </summary>
 24    /// <param name="rows">The rows.</param>
 25    /// <param name="columns">The columns.</param>
 026    public CsvExporter(IEnumerable<T>? rows, IEnumerable<string>? columns)
 027    {
 028        Rows = rows ?? Enumerable.Empty<T>();
 029        Columns = columns ?? Enumerable.Empty<string>();
 030    }
 31
 32    /// <summary>
 33    /// Gets the columns.
 34    /// </summary>
 035    public IEnumerable<string> Columns { get; private set; }
 36
 37    /// <summary>
 38    /// Gets the rows.
 39    /// </summary>
 040    public IEnumerable<T> Rows { get; private set; }
 41
 42    /// <summary>
 43    /// Exports this instance.
 44    /// </summary>
 45    public string Export()
 046    {
 047        return Export(true);
 048    }
 49
 50    /// <summary>
 51    /// Exports the specified include header line.
 52    /// </summary>
 53    /// <param name="includeHeaderLine">if set to <c>true</c> [include header line].</param>
 54    public string Export(bool includeHeaderLine)
 055    {
 056        var sb = new StringBuilder();
 057        IList<PropertyInfo> propertyInfoList = typeof(T).GetProperties();
 58
 059        if (includeHeaderLine)
 060        {
 061            if (Columns.Any())
 062            {
 063                foreach (var propertyName in Columns)
 064                {
 065                    sb.Append(propertyName).Append(',');
 066                }
 067            }
 68            else
 069            {
 070                foreach (var propertyInfo in propertyInfoList)
 071                {
 072                    sb.Append(propertyInfo.Name).Append(',');
 073                }
 074            }
 75
 076            sb.Remove(sb.Length - 1, 1).AppendLine();
 077        }
 78
 079        foreach (T obj in Rows)
 080        {
 081            if (Columns != null)
 082            {
 083                foreach (var propertyName in Columns)
 084                {
 085                    var propertyInfo = propertyInfoList.FirstOrDefault(i => i.Name == propertyName);
 086                    if (propertyInfo != null) sb.Append(MakeCsvText(propertyInfo.GetValue(obj, null))).Append(',');
 087                }
 088            }
 89            else
 090            {
 091                foreach (var propertyInfo in propertyInfoList)
 092                {
 093                    sb.Append(MakeCsvText(propertyInfo.GetValue(obj, null))).Append(',');
 094                }
 095            }
 96
 097            sb.Remove(sb.Length - 1, 1).AppendLine();
 098        }
 99
 0100        return sb.ToString();
 0101    }
 102
 103    /// <summary>
 104    /// Exports to file.
 105    /// </summary>
 106    /// <param name="path">The path.</param>
 0107    public void ExportToFile(string? path) => File.WriteAllText(path.ToReferenceTypeValueOrThrow(), Export());
 108
 109    /// <summary>
 110    /// Exports to bytes.
 111    /// </summary>
 0112    public byte[] ExportToBytes() => Encoding.UTF8.GetBytes(Export());
 113
 114    static string MakeCsvText(object? value)
 0115    {
 0116        if (value == null) return string.Empty;
 117
 0118        if (value is DateTime time)
 0119        {
 0120            return time.ToString(time.TimeOfDay.TotalSeconds == 0 ? "yyyy-MM-dd" : "yyyy-MM-dd HH:mm:ss");
 121        }
 122
 0123        string output = value.ToString()!;
 124
 0125        if (output.Contains(',') || output.Contains('"'))
 0126            output = $"\"{output.Replace("\"", "\"\"")}\"";
 127
 0128        return output;
 0129    }
 130}