DataTable: Add units for columns

This commit is contained in:
2023-11-15 18:29:14 +01:00
parent 486655d071
commit b096163ed3
7 changed files with 125 additions and 92 deletions

View File

@ -18,15 +18,16 @@ namespace Elwig.Models.Dtos {
public IEnumerable<Type?> ColumnFlatTypes { get; private set; }
public IEnumerable<int> ColumnSpans { get; private set; }
public IEnumerable<int?> ColumnWidths { get; private set; }
public IEnumerable<string?[]> ColumnUnits { get; private set; }
private readonly PropertyInfo[] _properties;
private readonly FieldInfo[] _fields;
private readonly (string, PropertyInfo?, FieldInfo?)[] _map;
public DataTable(string name, string fullName, IEnumerable<T> rows, IEnumerable<(string, string, int?)>? colNames = null) {
public DataTable(string name, string fullName, IEnumerable<T> rows, IEnumerable<(string, string, string?, int?)>? colNames = null) {
_fields = typeof(T).GetFields();
_properties = typeof(T).GetProperties();
colNames ??= _properties.Select(p => p.Name).Union(_fields.Select(f => f.Name)).Select(i => (i, i, (int?)null)).ToList();
colNames ??= _properties.Select(p => p.Name).Union(_fields.Select(f => f.Name)).Select(i => (i, i, (string?)null, (int?)null)).ToList();
_map = colNames.Select(n => (n.Item2, _properties.FirstOrDefault(p => p?.Name == n.Item1, null), _fields.FirstOrDefault(f => f?.Name == n.Item1, null))).ToArray();
Name = name;
FullName = fullName;
@ -42,27 +43,28 @@ namespace Elwig.Models.Dtos {
return type != null && type.IsValueType && type.Name.StartsWith("ValueTuple") ? type.GetFields().Length :
type != null && elType != null && type.IsArray && elType.IsValueType && elType.Name.StartsWith("ValueTuple") ? elType.GetFields().Length : 1;
}).ToList();
ColumnWidths = colNames.Select(c => c.Item3).ToList();
ColumnWidths = colNames.Select(c => c.Item4).ToList();
ColumnUnits = colNames.Select(c => c.Item3?.Split("|").Select(p => p.Length == 0 ? null : p).ToArray() ?? Array.Empty<string?>()).ToList();
}
public DataTable(string name, string fullName, IEnumerable<T> rows, IEnumerable<(string, string)>? colNames = null) :
this(name, fullName, rows, colNames?.Select(c => (c.Item1, c.Item2, (int?)null))) {
public DataTable(string name, string fullName, IEnumerable<T> rows, IEnumerable<(string, string, string?)>? colNames = null) :
this(name, fullName, rows, colNames?.Select(c => (c.Item1, c.Item2, c.Item3, (int?)null))) {
}
public DataTable(string name, IEnumerable<T> rows, IEnumerable<(string, string)>? colNames = null) :
public DataTable(string name, IEnumerable<T> rows, IEnumerable<(string, string, string?)>? colNames = null) :
this(name, name, rows, colNames) {
}
public DataTable(string name, IEnumerable<T> rows, IEnumerable<(string, string, int?)>? colNames = null) :
public DataTable(string name, IEnumerable<T> rows, IEnumerable<(string, string, string?, int?)>? colNames = null) :
this(name, name, rows, colNames) {
}
public DataTable(string name, IEnumerable<T> rows, IEnumerable<(string, string, int)>? colNames = null) :
public DataTable(string name, IEnumerable<T> rows, IEnumerable<(string, string, string?, int)>? colNames = null) :
this(name, name, rows, colNames) {
}
public DataTable(string name, string fullName, IEnumerable<T> rows, IEnumerable<(string, string, int)>? colNames = null) :
this(name, fullName, rows, colNames?.Select(c => (c.Item1, c.Item2, (int?)c.Item3))) {
public DataTable(string name, string fullName, IEnumerable<T> rows, IEnumerable<(string, string, string?, int)>? colNames = null) :
this(name, fullName, rows, colNames?.Select(c => (c.Item1, c.Item2, c.Item3, (int?)c.Item4))) {
}
protected IEnumerable<(string, object?)> GetNamedRowData(T row) {