Add gen purpose blue row/table utils for data streams

* Add BlueRowReflection<T> so ViewStream implementations can use
  strong row types without needing to implement IBlueRow
* Add default implementations for IDictionary members in IBlueRow to make
  implementing IBlueRow less tedious
This commit is contained in:
John Colvin committed 2025-11-03 16:13:16 -05:00
1 parent cc80785d8a
commit 97efe8396e
8 files changed
+248 -2

No files matched your search

@@ -90,6 +90,41 @@ namespace BCG {
public static DateTime get_datetime(this IBlueRow r, int col_no, DateTime err_value = default(DateTime)) {
return BlueDataUtility.get_row_datetime_value(r, col_no, err_value);
}
public static double? GetDouble(this IBlueRow r, int colNo) =>
GetDouble(r[colNo]);
public static double? GetDouble(this IBlueRow r, string colName) =>
GetDouble(r[colName]);
public static double? GetDouble(object obj) {
if (obj is double d) {
return d;
}
if (obj is int i32) {
return i32;
}
if (obj is long i64) {
return i64;
}
if (obj is string s) {
return double.TryParse(s, out d) ? d : null;
}
if (obj is uint u32) {
return u32;
}
if (obj is ulong u64) {
return u64;
}
if (obj is short i16) {
return i16;
}
if (obj is ushort u16) {
return u16;
}
return null;
}
public static T get_enum<T>(this IBlueRow r, string col_name, T err_value = default(T)) where T : struct {
return BlueDataUtility.get_row_enum_value<T>(r, col_name, err_value);
}
@@ -2314,6 +2349,32 @@ namespace BCG {
// }
// }
public static TimeSpan? GetTimeSpan(this IBlueRow r, int col_no) =>
GetTimeSpan(r[col_no]);
public static TimeSpan? GetTimeSpan(this IBlueRow r, string col_name) =>
GetTimeSpan(r[col_name]);
public static TimeSpan? GetTimeSpan(object obj) {
if (obj is TimeSpan ts) {
return ts;
}
var str = obj as string ?? obj?.ToString();
if (str is null) {
return null;
}
if (TimeSpan.TryParse(str, out ts)) {
return ts;
}
if (str.StartsWith("PT")) {
try {
// Parse ISO 8601 duration
return System.Xml.XmlConvert.ToTimeSpan(str);
} catch {
}
}
return null;
}
#endregion
#region CSV methods
@@ -7,7 +7,7 @@ namespace BCG
/// Used for enumerating an IEnumerable (ex ICollection). Furthermore, this is the
/// default Enueramtor used when enumerating asn IBlueRow
/// </summary>
public class BlueRowEnumerator : IEnumerator
public class BlueRowEnumerator : IDictionaryEnumerator
{
protected int myPos = -1;
protected int myRowSize = -1;
@@ -18,6 +18,11 @@ namespace BCG
myRow = row;
myRowSize = row.schema.columnNames.Length;
}
public DictionaryEntry Entry =>
new(myRow.schema.columnInfo[myPos].colName, myRow[myPos]);
public object Key => myRow.schema.columnInfo[myPos].colName;
public object Value => myRow[myPos];
#region IEnumerator members
public void Reset() {myPos = 0;}
@@ -0,0 +1,30 @@
#nullable enable
namespace BCG;
using System;
using System.Collections;
public class BlueRowKeyCollection : ICollection {
readonly ColumnInfo[] columns;
public BlueRowKeyCollection(ColumnInfo[] columns) =>
this.columns = columns;
public int Count => columns.Length;
public bool IsSynchronized => false;
public object SyncRoot => this;
public void CopyTo(Array array, int index) {
for (int i = 0; i < columns.Length; i++) {
array.SetValue(columns[i].colName, i + index);
}
}
public IEnumerator GetEnumerator() {
for (int i = 0; i < columns.Length; i++) {
yield return columns[i].colName;
}
}
}
@@ -0,0 +1,44 @@
#nullable enable
namespace Bts.DataStreamCore;
using BCG;
using System.Linq;
using System.Reflection;
public static class BlueRowReflectionFactory {
public static IBlueRow Create<T>(T row, string? tableName = null) =>
new BlueRowReflection<T>(row);
class BlueRowReflection<T> : IBlueRow {
public static readonly ColumnInfo[] ColumnInfos;
public static readonly PropertyInfo[] ColumnProps;
readonly T row;
readonly string? tableName;
static BlueRowReflection() {
var colTups = typeof(T).GetProperties(BindingFlags.Instance)
.Select(p => (Prop: p, Attr: p.GetCustomAttribute<BlueTableColumnAttribute>()))
.Where(p => p.Attr is not null)
.ToList();
ColumnInfos = new ColumnInfo[colTups.Count];
ColumnProps = new PropertyInfo[colTups.Count];
for (int i = 0; i < colTups.Count; i++) {
var (prop, attr) = colTups[i];
ColumnInfos[i] = new(attr?.Name ?? prop.Name, i, prop.PropertyType);
ColumnProps[i] = prop;
}
}
public BlueRowReflection(T row, string? tableName = null) {
this.row = row;
this.tableName = tableName;
}
public object? this[int index] => ColumnProps[index].GetValue(row);
public IBlueTableSchema schema =>
new BlueTableSchema(ColumnInfos, tableName ?? typeof(T).Name);
}
}
@@ -0,0 +1,32 @@
#nullable enable
namespace BCG;
using System;
using System.Collections;
public class BlueRowValueCollection : ICollection {
readonly IBlueRow row;
public BlueRowValueCollection(IBlueRow row) =>
this.row = row;
public int Count => row.schema.columnInfo.Length;
public bool IsSynchronized => false;
public object SyncRoot => this;
public void CopyTo(Array array, int index) {
var length = row.schema.columnInfo.Length;
for (int i = 0; i < length; i++) {
array.SetValue(row[i], i + index);
}
}
public IEnumerator GetEnumerator() {
var length = row.schema.columnInfo.Length;
for (int i = 0; i < length; i++) {
yield return row[i];
}
}
}
@@ -0,0 +1,17 @@
#nullable enable
namespace Bts.DataStreamCore;
using System;
[AttributeUsage(AttributeTargets.Property)]
public class BlueTableColumnAttribute : Attribute {
readonly int index;
public BlueTableColumnAttribute(string? name = null, int index = -1) {
Name = name;
this.index = index;
}
public string? Name { get; }
public int? Index => index >= 0 ? index : null;
}
@@ -0,0 +1,12 @@
#nullable enable
namespace Bts.DataStreamCore;
using System;
[AttributeUsage(AttributeTargets.Class)]
public class BlueTableNameAttribute : Attribute {
public BlueTableNameAttribute(string name) =>
Name = name;
public string Name { get; }
}
@@ -1,6 +1,8 @@
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace BCG
{
@@ -23,7 +25,50 @@ namespace BCG
/// half a million type checkings
/// </summary>
object? this[int index] {get;}
object? this[string colName] {get;}
object? this[string colName] {
get {
var cols = schema.columnInfo;
for (int i = 0; i < cols.Length; i++) {
if (cols[i].colName == colName) {
return this[i];
}
}
throw new KeyNotFoundException();
}
}
object? IDictionary.this[object key] {
get => key is string s
? this[s]
: throw new ArgumentException($"Key must be of type string (actually {key?.GetType()})", nameof(key));
set => throw new NotImplementedException();
}
ICollection IDictionary.Keys => new BlueRowKeyCollection(schema.columnInfo);
ICollection IDictionary.Values => new BlueRowValueCollection(this);
int ICollection.Count => schema.columnInfo.Length;
bool IDictionary.IsFixedSize => true;
bool IDictionary.IsReadOnly => true;
object ICollection.SyncRoot => this;
bool ICollection.IsSynchronized => false;
bool IDictionary.Contains(object key) {
if (key is string s) {
return schema.columnNames.Contains(s);
}
return false;
}
void ICollection.CopyTo(Array array, int index) {
var length = schema.columnInfo.Length;
for (int i = 0; i < length; i++) {
array.SetValue(this[i], i + index);
}
}
void IDictionary.Add(object key, object? value) => throw new NotImplementedException();
void IDictionary.Clear() => throw new NotImplementedException();
IDictionaryEnumerator IDictionary.GetEnumerator() => new BlueRowEnumerator(this);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
void IDictionary.Remove(object key) => throw new NotSupportedException();
}
namespace Extensions
{