diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs index bba9e532c..5f7ece0a0 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs @@ -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(this IBlueRow r, string col_name, T err_value = default(T)) where T : struct { return BlueDataUtility.get_row_enum_value(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 diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowEnumerator.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowEnumerator.cs index c4fababc5..16394e500 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowEnumerator.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowEnumerator.cs @@ -7,7 +7,7 @@ namespace BCG /// Used for enumerating an IEnumerable (ex ICollection). Furthermore, this is the /// default Enueramtor used when enumerating asn IBlueRow /// - 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;} diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowKeyCollection.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowKeyCollection.cs new file mode 100644 index 000000000..cb0f50683 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowKeyCollection.cs @@ -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; + } + } +} diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowReflection.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowReflection.cs new file mode 100644 index 000000000..7e2f06cb8 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowReflection.cs @@ -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 row, string? tableName = null) => + new BlueRowReflection(row); + + class BlueRowReflection : 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())) + .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); + } +} diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowValueCollection.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowValueCollection.cs new file mode 100644 index 000000000..95a004c1d --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowValueCollection.cs @@ -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]; + } + } +} diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableColumnAttribute.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableColumnAttribute.cs new file mode 100644 index 000000000..02ca3092e --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableColumnAttribute.cs @@ -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; +} diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableNameAttribute.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableNameAttribute.cs new file mode 100644 index 000000000..4dd81c8d7 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableNameAttribute.cs @@ -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; } +} diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/IBlueRow.cs b/src/DataStreamSuite/DataStreamCore/BlueData/IBlueRow.cs index 1225e5c4e..99941e41a 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/IBlueRow.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/IBlueRow.cs @@ -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 /// 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 {