Compare commits
No commits in common. "7368ac816b67e4b0651842e9fd4fb25f90723767" and "52ed9495297390ecf13cd313774c905a7557f4da" have entirely different histories.
7368ac816b
...
52ed949529
@ -1,19 +1,7 @@
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text;
|
||||
|
||||
namespace NathanMcRae;
|
||||
|
||||
public class Tsv<T> where T : SaneTsv.TsvRecord
|
||||
{
|
||||
public virtual List<T> Records { get; set; }
|
||||
}
|
||||
|
||||
public class CommentedTsv<T>: Tsv<T> where T : SaneTsv.TsvRecord
|
||||
{
|
||||
public override List<T> Records { get; set; }
|
||||
public string FileComment { get; set; } = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sane Tab-Separated Values
|
||||
/// </summary>
|
||||
@ -46,6 +34,8 @@ public class SaneTsv
|
||||
// TODO: We need to be able to update all these in tandem somehow
|
||||
public string[] ColumnNames { get; protected set; }
|
||||
public Type[] ColumnTypes { get; protected set; }
|
||||
public List<SaneTsvRecord> Records { get; protected set; }
|
||||
public string FileComment { get; protected set; } = null;
|
||||
protected static bool? _littleEndian = null;
|
||||
public static bool LittleEndian
|
||||
{
|
||||
@ -59,55 +49,28 @@ public class SaneTsv
|
||||
}
|
||||
}
|
||||
|
||||
public static Tsv<T> ParseSimpleTsv<T>(byte[] inputBuffer) where T : TsvRecord, new()
|
||||
public static SaneTsv ParseSimpleTsv(byte[] inputBuffer)
|
||||
{
|
||||
return Parse<T>(inputBuffer, FormatType.SIMPLE_TSV);
|
||||
return Parse(inputBuffer, FormatType.SIMPLE_TSV);
|
||||
}
|
||||
|
||||
public static Tsv<T> ParseTypedTsv<T>(byte[] inputBuffer) where T : TsvRecord, new()
|
||||
public static SaneTsv ParseTypedTsv(byte[] inputBuffer)
|
||||
{
|
||||
return Parse<T>(inputBuffer, FormatType.TYPED_TSV);
|
||||
return Parse(inputBuffer, FormatType.TYPED_TSV);
|
||||
}
|
||||
|
||||
public static CommentedTsv<T> ParseCommentedTsv<T>(byte[] inputBuffer) where T : CommentedTsvRecord, new()
|
||||
public static SaneTsv ParseCommentedTsv(byte[] inputBuffer)
|
||||
{
|
||||
// TODO: add the file comment?
|
||||
return (CommentedTsv<T>)Parse<T>(inputBuffer, FormatType.COMMENTED_TSV);
|
||||
return Parse(inputBuffer, FormatType.COMMENTED_TSV);
|
||||
}
|
||||
|
||||
// TODO: Have parsing errors include line / column #
|
||||
protected static Tsv<T> Parse<T>(byte[] inputBuffer, FormatType format) where T : TsvRecord, new()
|
||||
protected static SaneTsv Parse(byte[] inputBuffer, FormatType format)
|
||||
{
|
||||
Tsv<T> parsed;
|
||||
if (format == FormatType.COMMENTED_TSV)
|
||||
{
|
||||
parsed = new CommentedTsv<T>();
|
||||
}
|
||||
else
|
||||
{
|
||||
parsed = new Tsv<T>();
|
||||
}
|
||||
parsed.Records = new List<T>();
|
||||
|
||||
var headerTypes = new List<Type>();
|
||||
var headerNames = new List<string>();
|
||||
var headerPropertyInfos = new List<PropertyInfo>();
|
||||
int columnCount = 0;
|
||||
|
||||
foreach (PropertyInfo property in typeof(T).GetProperties())
|
||||
{
|
||||
TypedTsvColumnAttribute attribute = (TypedTsvColumnAttribute)Attribute.GetCustomAttribute(property, typeof(TypedTsvColumnAttribute));
|
||||
if (attribute == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
headerNames.Add(attribute.ColumnName ?? property.Name);
|
||||
headerTypes.Add(attribute.ColumnType ?? GetColumnFromType(property.PropertyType));
|
||||
headerPropertyInfos.Add(property);
|
||||
// TODO: Check that the property type and given column type are compatible
|
||||
columnCount++;
|
||||
}
|
||||
var parsed = new SaneTsv();
|
||||
parsed.ColumnNames = new string[] { };
|
||||
parsed.ColumnTypes = new Type[] { };
|
||||
parsed.Records = new List<SaneTsvRecord>();
|
||||
|
||||
var fieldBytes = new List<byte>();
|
||||
var fields = new List<byte[]>();
|
||||
@ -166,6 +129,9 @@ public class SaneTsv
|
||||
|
||||
numFields = fields.Count;
|
||||
|
||||
parsed.ColumnNames = new string[numFields];
|
||||
parsed.ColumnTypes = new Type[numFields];
|
||||
|
||||
int numTypesBlank = 0;
|
||||
|
||||
for (int j = 0; j < fields.Count; j++)
|
||||
@ -247,32 +213,16 @@ public class SaneTsv
|
||||
}
|
||||
|
||||
// TODO: Check column name uniqueness
|
||||
// TODO: Allow lax parsing (only worry about parsing columns that are given in the specifying type
|
||||
|
||||
if (headerNames[j] != columnName)
|
||||
{
|
||||
throw new Exception($"Column {j} has name {columnName}, but expected {headerNames[j]}");
|
||||
}
|
||||
|
||||
if (headerTypes[j] != type)
|
||||
{
|
||||
throw new Exception($"Column {j} has type {type}, but expected {headerTypes[j]}");
|
||||
}
|
||||
parsed.ColumnNames[j] = columnName;
|
||||
parsed.ColumnTypes[j] = type;
|
||||
}
|
||||
|
||||
if (currentComment.Length > 0)
|
||||
{
|
||||
if (parsed is CommentedTsv<T> commentedParsed)
|
||||
{
|
||||
commentedParsed.FileComment = currentComment.ToString();
|
||||
currentComment.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Found a file comment, but parser wasn't expecting a comment");
|
||||
}
|
||||
parsed.FileComment = currentComment.ToString();
|
||||
currentComment.Clear();
|
||||
}
|
||||
|
||||
|
||||
fields.Clear();
|
||||
}
|
||||
@ -288,7 +238,7 @@ public class SaneTsv
|
||||
comment = currentComment.ToString();
|
||||
currentComment.Clear();
|
||||
}
|
||||
parsed.Records.Add(ParseCurrentRecord<T>(headerTypes.ToArray(), headerPropertyInfos.ToArray(), fields, comment, line));
|
||||
parsed.Records.Add(new SaneTsvRecord(parsed, ParseCurrentRecord(parsed, fields, line), comment, line));
|
||||
fields.Clear();
|
||||
}
|
||||
|
||||
@ -348,46 +298,28 @@ public class SaneTsv
|
||||
comment = currentComment.ToString();
|
||||
currentComment.Clear();
|
||||
}
|
||||
parsed.Records.Add(ParseCurrentRecord<T>(headerTypes.ToArray(), headerPropertyInfos.ToArray(), fields, comment, line));
|
||||
parsed.Records.Add(new SaneTsvRecord(parsed, ParseCurrentRecord(parsed, fields, line), comment, line));
|
||||
fields.Clear();
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
protected static T ParseCurrentCommentedRecord<T> (Type[] columnTypes, PropertyInfo[] properties, List<byte[]> fields, string comment, int line) where T : CommentedTsvRecord, new()
|
||||
/// <summary>
|
||||
/// Note: this modifies 'parsed'
|
||||
/// </summary>
|
||||
protected static object[] ParseCurrentRecord(SaneTsv parsed, List<byte[]> fields, int line)
|
||||
{
|
||||
return (T)ParseCurrentRecord<T>(columnTypes, properties, fields, comment, line);
|
||||
}
|
||||
|
||||
protected static T ParseCurrentRecord<T>(Type[] columnTypes, PropertyInfo[] properties, List<byte[]> fields, string comment, int line) where T : TsvRecord, new()
|
||||
{
|
||||
T record = new T();
|
||||
|
||||
if (record is CommentedTsvRecord commentedRecord)
|
||||
{
|
||||
commentedRecord.Comment = comment;
|
||||
}
|
||||
else if (comment != null)
|
||||
{
|
||||
throw new Exception($"Found comment for line {line}, but format does not support comments");
|
||||
}
|
||||
|
||||
record.Line = line;
|
||||
|
||||
var parsedFields = new object[fields.Count];
|
||||
for (int j = 0; j < fields.Count; j++)
|
||||
{
|
||||
// All other types require the content to be UTF-8. Binary fields can ignore that.
|
||||
if (columnTypes[j] == typeof(BinaryType))
|
||||
if (parsed.ColumnTypes[j] == typeof(BinaryType))
|
||||
{
|
||||
// TODO: Use faster method for property setting
|
||||
// e.g. https://blog.marcgravell.com/2012/01/playing-with-your-member.html
|
||||
// or https://stackoverflow.com/questions/1027980/improving-performance-reflection-what-alternatives-should-i-consider
|
||||
// or https://stackoverflow.com/questions/12767091/why-are-propertyinfo-setvalue-and-getvalue-so-slow
|
||||
properties[j].SetValue(record, fields[j]);
|
||||
parsedFields[j] = fields[j];
|
||||
continue;
|
||||
}
|
||||
else if (columnTypes[j] == typeof(Float32LEType))
|
||||
else if (parsed.ColumnTypes[j] == typeof(Float32LEType))
|
||||
{
|
||||
byte[] floatBytes;
|
||||
if (!LittleEndian)
|
||||
@ -402,11 +334,11 @@ public class SaneTsv
|
||||
{
|
||||
floatBytes = fields[j];
|
||||
}
|
||||
properties[j].SetValue(record, BitConverter.ToSingle(floatBytes, 0));
|
||||
parsedFields[j] = BitConverter.ToSingle(floatBytes, 0);
|
||||
|
||||
continue;
|
||||
}
|
||||
else if (columnTypes[j] == typeof(Float64LEType))
|
||||
else if (parsed.ColumnTypes[j] == typeof(Float64LEType))
|
||||
{
|
||||
byte[] floatBytes;
|
||||
if (!LittleEndian)
|
||||
@ -421,7 +353,7 @@ public class SaneTsv
|
||||
{
|
||||
floatBytes = fields[j];
|
||||
}
|
||||
properties[j].SetValue(record, BitConverter.ToDouble(floatBytes, 0));
|
||||
parsedFields[j] = BitConverter.ToDouble(floatBytes, 0);
|
||||
|
||||
continue;
|
||||
}
|
||||
@ -438,11 +370,11 @@ public class SaneTsv
|
||||
|
||||
// TODO: Add checking for numeric types format
|
||||
|
||||
if (columnTypes[j] == typeof(StringType))
|
||||
if (parsed.ColumnTypes[j] == typeof(StringType))
|
||||
{
|
||||
properties[j].SetValue(record, fieldString);
|
||||
parsedFields[j] = fieldString;
|
||||
}
|
||||
else if (columnTypes[j] == typeof(BooleanType))
|
||||
else if (parsed.ColumnTypes[j] == typeof(BooleanType))
|
||||
{
|
||||
bool parsedBool;
|
||||
if (fieldString == "TRUE")
|
||||
@ -458,9 +390,9 @@ public class SaneTsv
|
||||
throw new Exception($"Field {j} on line {line} is not valid boolean. Must be 'TRUE' or 'FALSE' exactly");
|
||||
}
|
||||
|
||||
properties[j].SetValue(record, parsedBool);
|
||||
parsedFields[j] = parsedBool;
|
||||
}
|
||||
else if (columnTypes[j] == typeof(Float32Type))
|
||||
else if (parsed.ColumnTypes[j] == typeof(Float32Type))
|
||||
{
|
||||
float parsedFloat;
|
||||
if (!float.TryParse(fieldString, out parsedFloat))
|
||||
@ -479,9 +411,9 @@ public class SaneTsv
|
||||
}
|
||||
}
|
||||
|
||||
properties[j].SetValue(record, parsedFloat);
|
||||
parsedFields[j] = parsedFloat;
|
||||
}
|
||||
else if (columnTypes[j] == typeof(Float64Type))
|
||||
else if (parsed.ColumnTypes[j] == typeof(Float64Type))
|
||||
{
|
||||
double parsedDouble;
|
||||
if (!double.TryParse(fieldString, out parsedDouble))
|
||||
@ -500,51 +432,51 @@ public class SaneTsv
|
||||
}
|
||||
}
|
||||
|
||||
properties[j].SetValue(record, parsedDouble);
|
||||
parsedFields[j] = parsedDouble;
|
||||
}
|
||||
else if (columnTypes[j] == typeof(UInt32Type))
|
||||
else if (parsed.ColumnTypes[j] == typeof(UInt32Type))
|
||||
{
|
||||
if (!UInt32.TryParse(fieldString, out UInt32 parsedUInt32))
|
||||
{
|
||||
throw new Exception($"Field {j} on line {line} is not valid UInt32");
|
||||
}
|
||||
|
||||
properties[j].SetValue(record, parsedUInt32);
|
||||
parsedFields[j] = parsedUInt32;
|
||||
}
|
||||
else if (columnTypes[j] == typeof(UInt64Type))
|
||||
else if (parsed.ColumnTypes[j] == typeof(UInt64Type))
|
||||
{
|
||||
if (!UInt64.TryParse(fieldString, out UInt64 parsedUInt64))
|
||||
{
|
||||
throw new Exception($"Field {j} on line {line} is not valid UInt64");
|
||||
}
|
||||
|
||||
properties[j].SetValue(record, parsedUInt64);
|
||||
parsedFields[j] = parsedUInt64;
|
||||
}
|
||||
else if (columnTypes[j] == typeof(Int32Type))
|
||||
else if (parsed.ColumnTypes[j] == typeof(Int32Type))
|
||||
{
|
||||
if (!Int32.TryParse(fieldString, out Int32 parsedInt32))
|
||||
{
|
||||
throw new Exception($"Field {j} on line {line} is not valid Int32");
|
||||
}
|
||||
|
||||
properties[j].SetValue(record, parsedInt32);
|
||||
parsedFields[j] = parsedInt32;
|
||||
}
|
||||
else if (columnTypes[j] == typeof(Int64Type))
|
||||
else if (parsed.ColumnTypes[j] == typeof(Int64Type))
|
||||
{
|
||||
if (!Int64.TryParse(fieldString, out Int64 parsedInt64))
|
||||
{
|
||||
throw new Exception($"Field {j} on line {line} is not valid Int64");
|
||||
}
|
||||
|
||||
properties[j].SetValue(record, parsedInt64);
|
||||
parsedFields[j] = parsedInt64;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Unexpected type {columnTypes[j]}");
|
||||
throw new Exception($"Unexpected type {parsed.ColumnTypes[j]}");
|
||||
}
|
||||
}
|
||||
|
||||
return record;
|
||||
return parsedFields;
|
||||
}
|
||||
|
||||
public static byte[] SerializeSimpleTsv(IList<string> header, IList<IList<string>> data)
|
||||
@ -740,49 +672,13 @@ public class SaneTsv
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] SerializeSimpleTsv<T>(IList<T> data) where T : TsvRecord
|
||||
{
|
||||
return SerializeTsv<T>(data, FormatType.TYPED_TSV);
|
||||
}
|
||||
|
||||
public static byte[] SerializeTypedTsv<T>(IList<T> data) where T : TsvRecord
|
||||
{
|
||||
return SerializeTsv<T>(data, FormatType.TYPED_TSV);
|
||||
}
|
||||
|
||||
public static byte[] SerializeCommentedTsv<T>(IList<T> data, string fileComment) where T : CommentedTsvRecord
|
||||
{
|
||||
return SerializeTsv<T>(data, FormatType.COMMENTED_TSV);
|
||||
}
|
||||
|
||||
protected static byte[] SerializeTsv<T>(IList<T> data, FormatType tsvFormat) where T : TsvRecord
|
||||
public static byte[] SerializeTypedTsv(IList<Type> headerTypes, IList<string> headerNames, IList<IList<object>> data)
|
||||
{
|
||||
var bytes = new List<byte>();
|
||||
|
||||
var headerTypes = new List<Type>();
|
||||
var headerNames = new List<string>();
|
||||
var headerPropertyInfos = new List<PropertyInfo>();
|
||||
int columnCount = 0;
|
||||
|
||||
foreach (PropertyInfo property in typeof(T).GetProperties())
|
||||
if (headerNames.Count != headerTypes.Count)
|
||||
{
|
||||
TsvColumnAttribute attribute = (TsvColumnAttribute)Attribute.GetCustomAttribute(property, typeof(TsvColumnAttribute));
|
||||
if (attribute == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string headerName = attribute.ColumnName ?? property.Name;
|
||||
headerNames.Add(headerName);
|
||||
Type headerType = attribute.ColumnType ?? GetColumnFromType(property.PropertyType);
|
||||
if (tsvFormat == FormatType.SIMPLE_TSV && headerType != typeof(StringType))
|
||||
{
|
||||
throw new Exception($"Serializing Simple TSV requires all columns be of type string, but column '{headerName}' has type '{headerType}'");
|
||||
}
|
||||
headerTypes.Add(headerType);
|
||||
headerPropertyInfos.Add(property);
|
||||
// TODO: Check that the property type and given column type are compatible
|
||||
columnCount++;
|
||||
throw new ArgumentException($"headerTypes length ({headerTypes.Count}) does not match headerNames length ({headerNames.Count})");
|
||||
}
|
||||
|
||||
// Serialize header
|
||||
@ -849,10 +745,8 @@ public class SaneTsv
|
||||
// Serialize data
|
||||
for (int i = 0; i < data.Count; i++)
|
||||
{
|
||||
for (int j = 0; j < columnCount; j++)
|
||||
for (int j = 0; j < data[i].Count; j++)
|
||||
{
|
||||
object datum = headerPropertyInfos[j].GetValue(data[i]);
|
||||
|
||||
try
|
||||
{
|
||||
byte[] fieldEncoded = null;
|
||||
@ -861,16 +755,16 @@ public class SaneTsv
|
||||
|
||||
if (headerTypes[j] == typeof(StringType))
|
||||
{
|
||||
fieldEncoded = Encoding.UTF8.GetBytes((string)datum);
|
||||
fieldEncoded = Encoding.UTF8.GetBytes((string)data[i][j]);
|
||||
}
|
||||
else if (headerTypes[j] == typeof(BooleanType))
|
||||
{
|
||||
bytes.AddRange((bool)datum ? TrueEncoded : FalseEncoded);
|
||||
bytes.AddRange((bool)data[i][j] ? TrueEncoded : FalseEncoded);
|
||||
skipEscaping = true;
|
||||
}
|
||||
else if (headerTypes[j] == typeof(Float32Type))
|
||||
{
|
||||
if (datum is float f)
|
||||
if (data[i][j] is float f)
|
||||
{
|
||||
if (float.IsNegativeInfinity(f))
|
||||
{
|
||||
@ -883,7 +777,7 @@ public class SaneTsv
|
||||
else
|
||||
{
|
||||
// See https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#round-trip-format-specifier-r
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((float)datum).ToString("G9")));
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((float)data[i][j]).ToString("G9")));
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -896,11 +790,11 @@ public class SaneTsv
|
||||
{
|
||||
if (LittleEndian)
|
||||
{
|
||||
fieldEncoded = BitConverter.GetBytes((float)datum);
|
||||
fieldEncoded = BitConverter.GetBytes((float)data[i][j]);
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] floatBytes = BitConverter.GetBytes((float)datum);
|
||||
byte[] floatBytes = BitConverter.GetBytes((float)data[i][j]);
|
||||
fieldEncoded = new byte[sizeof(float)];
|
||||
for (int k = 0; k < sizeof(float); k++)
|
||||
{
|
||||
@ -910,7 +804,7 @@ public class SaneTsv
|
||||
}
|
||||
else if (headerTypes[j] == typeof(Float64Type))
|
||||
{
|
||||
if (datum is double d)
|
||||
if (data[i][j] is double d)
|
||||
{
|
||||
if (double.IsNegativeInfinity(d))
|
||||
{
|
||||
@ -936,11 +830,11 @@ public class SaneTsv
|
||||
{
|
||||
if (LittleEndian)
|
||||
{
|
||||
fieldEncoded = BitConverter.GetBytes((double)datum);
|
||||
fieldEncoded = BitConverter.GetBytes((double)data[i][j]);
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] doubleBytes = BitConverter.GetBytes((double)datum);
|
||||
byte[] doubleBytes = BitConverter.GetBytes((double)data[i][j]);
|
||||
fieldEncoded = new byte[sizeof(double)];
|
||||
for (int k = 0; k < sizeof(double); k++)
|
||||
{
|
||||
@ -950,27 +844,27 @@ public class SaneTsv
|
||||
}
|
||||
else if (headerTypes[j] == typeof(UInt32Type))
|
||||
{
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((UInt32)datum).ToString()));
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((UInt32)(int)data[i][j]).ToString()));
|
||||
skipEscaping = true;
|
||||
}
|
||||
else if (headerTypes[j] == typeof(UInt64Type))
|
||||
{
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((UInt64)datum).ToString()));
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((UInt64)(int)data[i][j]).ToString()));
|
||||
skipEscaping = true;
|
||||
}
|
||||
else if (headerTypes[j] == typeof(Int32Type))
|
||||
{
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((Int32)datum).ToString()));
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((Int32)(int)data[i][j]).ToString()));
|
||||
skipEscaping = true;
|
||||
}
|
||||
else if (headerTypes[j] == typeof(Int64Type))
|
||||
{
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((Int64)datum).ToString()));
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((Int64)(int)data[i][j]).ToString()));
|
||||
skipEscaping = true;
|
||||
}
|
||||
else if (headerTypes[j] == typeof(BinaryType))
|
||||
{
|
||||
fieldEncoded = (byte[])datum;
|
||||
fieldEncoded = (byte[])data[i][j];
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -1008,7 +902,7 @@ public class SaneTsv
|
||||
}
|
||||
}
|
||||
|
||||
if (j < columnCount - 1)
|
||||
if (j < data[i].Count - 1)
|
||||
{
|
||||
bytes.Add((byte)'\t');
|
||||
}
|
||||
@ -1027,97 +921,25 @@ public class SaneTsv
|
||||
return bytes.ToArray();
|
||||
}
|
||||
|
||||
public class SimpleTsvRecord
|
||||
public SaneTsvRecord this[int i] => Records[i];
|
||||
|
||||
public class SaneTsvRecord
|
||||
{
|
||||
public string[] ColumnNames { get; }
|
||||
public SaneTsv Parent { get; }
|
||||
public string Comment { get; }
|
||||
public string[] Fields { get; }
|
||||
public int? Line { get; }
|
||||
public object[] Fields { get; }
|
||||
public int Line { get; }
|
||||
|
||||
public string this[string columnName] => Fields[Array.IndexOf(ColumnNames, columnName)];
|
||||
public string this[int columnIndex] => Fields[columnIndex];
|
||||
public object this[string columnName] => Fields[Array.IndexOf(Parent.ColumnNames, columnName)];
|
||||
|
||||
public SimpleTsvRecord(string[] columnNames, string[] fields, string comment, int line)
|
||||
public object this[int columnIndex] => Fields[columnIndex];
|
||||
|
||||
public SaneTsvRecord(SaneTsv parent, object[] fields, string comment, int line)
|
||||
{
|
||||
ColumnNames = columnNames;
|
||||
Parent = parent;
|
||||
Fields = fields;
|
||||
Comment = comment;
|
||||
Line = line;
|
||||
}
|
||||
}
|
||||
|
||||
public class TsvRecord
|
||||
{
|
||||
public int? Line { get; set; }
|
||||
public TsvRecord(int? line)
|
||||
{
|
||||
Line = line;
|
||||
}
|
||||
|
||||
public TsvRecord() { }
|
||||
}
|
||||
|
||||
public class CommentedTsvRecord : TsvRecord
|
||||
{
|
||||
public string Comment { get; set; }
|
||||
|
||||
public CommentedTsvRecord(string comment, int? line)
|
||||
{
|
||||
Comment = comment;
|
||||
Line = line;
|
||||
}
|
||||
|
||||
public CommentedTsvRecord() { }
|
||||
}
|
||||
|
||||
public class TestRecord : CommentedTsvRecord
|
||||
{
|
||||
[TypedTsvColumn("my-column")]
|
||||
public string MyColumn { get; set; }
|
||||
}
|
||||
|
||||
// TODO: Add column ordering
|
||||
public class TsvColumnAttribute : Attribute
|
||||
{
|
||||
public string ColumnName { get; }
|
||||
public virtual Type ColumnType { get; }
|
||||
|
||||
public TsvColumnAttribute()
|
||||
{
|
||||
ColumnType = typeof(StringType);
|
||||
}
|
||||
public TsvColumnAttribute(string columnName)
|
||||
{
|
||||
ColumnType = typeof(StringType);
|
||||
ColumnName = columnName;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add column ordering
|
||||
public class TypedTsvColumnAttribute : TsvColumnAttribute
|
||||
{
|
||||
public override Type ColumnType { get; }
|
||||
|
||||
public TypedTsvColumnAttribute() { }
|
||||
|
||||
public TypedTsvColumnAttribute(string columnName) : base(columnName) { }
|
||||
|
||||
public TypedTsvColumnAttribute(string columnName, Type columnType) : base(columnName)
|
||||
{
|
||||
if (columnType.BaseType != typeof(ColumnType))
|
||||
{
|
||||
throw new Exception("Column type must inherit from SaneTsv.ColumnType");
|
||||
}
|
||||
ColumnType = columnType;
|
||||
}
|
||||
|
||||
public TypedTsvColumnAttribute(Type columnType)
|
||||
{
|
||||
if (columnType.BaseType != typeof(ColumnType))
|
||||
{
|
||||
throw new Exception("Column type must inherit from SaneTsv.ColumnType");
|
||||
}
|
||||
ColumnType = columnType;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,299 +1,145 @@
|
||||
using NathanMcRae;
|
||||
using System.Text;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
public class TestRecord : SaneTsv.TsvRecord
|
||||
string testName = "Bool test";
|
||||
string testString1 = "column1:ty\\#pe:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
SaneTsv parsed = SaneTsv.ParseTypedTsv(Encoding.UTF8.GetBytes(testString1));
|
||||
if (parsed.Records[0]["column1:ty#pe"] is bool result && result)
|
||||
{
|
||||
[SaneTsv.TypedTsvColumn("string-test")]
|
||||
public string StringTest { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("bool-test")]
|
||||
public bool BoolTest { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("float32-test")]
|
||||
public float Float32Test { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("float32-le-test", typeof(SaneTsv.Float32LEType))]
|
||||
public float Float32LETest { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("float64-test")]
|
||||
public double Float64Test { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("float64-le-test", typeof(SaneTsv.Float64LEType))]
|
||||
public double Float64LETest { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("uint32-test")]
|
||||
public UInt32 UInt32Test { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("uint64-test")]
|
||||
public UInt64 UInt64Test { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("int32-test")]
|
||||
public Int32 Int32Test { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("int64-test")]
|
||||
public Int64 Int64Test { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("binary-test")]
|
||||
public byte[] BinaryTest { get; set; }
|
||||
|
||||
public TestRecord(string stringTest, bool boolTest, float float32Test, float float32LETest, double float64Test, double float64LETest, UInt32 uInt32Test, UInt64 uInt64Test, Int32 int32Test, Int64 int64Test, byte[] binaryTest)
|
||||
{
|
||||
StringTest = stringTest;
|
||||
BoolTest = boolTest;
|
||||
Float32Test = float32Test;
|
||||
Float32LETest = float32LETest;
|
||||
Float64Test = float64Test;
|
||||
Float64LETest = float64LETest;
|
||||
UInt32Test = uInt32Test;
|
||||
UInt64Test = uInt64Test;
|
||||
Int32Test = int32Test;
|
||||
Int64Test = int64Test;
|
||||
BinaryTest = binaryTest;
|
||||
}
|
||||
|
||||
public TestRecord() { }
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
|
||||
public class BoolTestRecord : SaneTsv.CommentedTsvRecord
|
||||
else
|
||||
{
|
||||
[SaneTsv.TypedTsvColumn("column1:ty#pe")]
|
||||
public bool Column1 { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn]
|
||||
public byte[] column2 { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("columnthree\nyep")]
|
||||
public string Column3 { get; set; }
|
||||
}
|
||||
|
||||
public class BoolTestRecord2 : SaneTsv.CommentedTsvRecord
|
||||
{
|
||||
[SaneTsv.TypedTsvColumn("column1:type")]
|
||||
public bool Column1 { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn]
|
||||
public byte[] column2 { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("columnthree\nyep")]
|
||||
public string Column3 { get; set; }
|
||||
}
|
||||
|
||||
public class SerdeTestRecord : SaneTsv.CommentedTsvRecord
|
||||
{
|
||||
[SaneTsv.TypedTsvColumn("column1")]
|
||||
public bool Column1 { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn]
|
||||
public byte[] column2 { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("columnthree\nyep")]
|
||||
public string Column3 { get; set; }
|
||||
}
|
||||
|
||||
public class FloatTestRecord : SaneTsv.CommentedTsvRecord
|
||||
{
|
||||
[SaneTsv.TypedTsvColumn("somefloat")]
|
||||
public double SomeFloat { get; set; }
|
||||
|
||||
[SaneTsv.TypedTsvColumn("binfloat", typeof(SaneTsv.Float64LEType))]
|
||||
public double BinFloat { get; set; }
|
||||
}
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
{
|
||||
string testName = "Bool test";
|
||||
string testString1 = "column1:ty\\#pe:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
Tsv<BoolTestRecord> parsed = SaneTsv.ParseTypedTsv<BoolTestRecord>(Encoding.UTF8.GetBytes(testString1));
|
||||
if (parsed.Records[0].Column1)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Bad bool test";
|
||||
try
|
||||
{
|
||||
string testString1 = "column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\nTUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
Tsv<BoolTestRecord> parsed = SaneTsv.ParseTypedTsv<BoolTestRecord>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Comment test";
|
||||
string testString1 = "#This is a file comment\n" +
|
||||
"#One more file comment line\n" +
|
||||
"column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\n#This is a comment" +
|
||||
"\n#Another comment line" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
CommentedTsv<BoolTestRecord2> parsed = SaneTsv.ParseCommentedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
}
|
||||
|
||||
//{
|
||||
// string testName = "Serde test";
|
||||
// string testString1 = "column1\tcolumn2\tcolumnthree\\nyep" +
|
||||
// "\nTRUE\tvalue\\\\twoo\tvaluetrhee" +
|
||||
// "\nFALSE\tnother\tno\\ther";
|
||||
|
||||
// Tsv<SerdeTestRecord> parsed = SaneTsv.ParseSimpleTsv<SerdeTestRecord>(Encoding.UTF8.GetBytes(testString1));
|
||||
// string serialized = Encoding.UTF8.GetString(SaneTsv.SerializeSimpleTsv(parsed.ColumnNames, parsed.Records.Select(r => r.Fields.Select(f => f.ToString()).ToArray()).ToArray()));
|
||||
|
||||
// if (testString1 == serialized)
|
||||
// {
|
||||
// Console.WriteLine($"Passed {testName}");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Console.WriteLine($"Failed {testName}");
|
||||
// }
|
||||
//}
|
||||
|
||||
{
|
||||
string testName = "Float binary test";
|
||||
var bytes = new List<byte>();
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes("somefloat:float64\tbinfloat:float64-le" +
|
||||
"\n1.5\t")); bytes.AddRange(BitConverter.GetBytes(1.5));
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes("\n-8.0000005E-14\t")); bytes.AddRange(BitConverter.GetBytes(-8.0000005E-14));
|
||||
|
||||
Tsv<FloatTestRecord> parsed = SaneTsv.ParseTypedTsv<FloatTestRecord>(bytes.ToArray());
|
||||
if (parsed.Records[0].BinFloat == parsed.Records[0].SomeFloat)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Serde test";
|
||||
|
||||
TestRecord[] data =
|
||||
{
|
||||
new TestRecord("test", true, 44.5f, 44.5f, -88e-3, -88e-3, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3 }),
|
||||
new TestRecord("test2", false, 44.5000005f, 44.5000005f, -88e-30, -88e-30, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3, 4 }),
|
||||
new TestRecord("test2", false, float.NaN, float.NaN, double.NaN, double.NaN, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3, 4 }),
|
||||
new TestRecord("test2", false, float.NegativeInfinity, float.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3, 4 }),
|
||||
new TestRecord("test2", false, float.PositiveInfinity, float.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3, 4 }),
|
||||
};
|
||||
|
||||
byte[] serialized = SaneTsv.SerializeTypedTsv(data);
|
||||
|
||||
Tsv<TestRecord> parsed = SaneTsv.ParseTypedTsv<TestRecord>(serialized);
|
||||
|
||||
if ((float)parsed.Records[1].Float32Test == 44.5000005f)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Trying to parse a not commented record as a Commented TSV test";
|
||||
|
||||
// These should not compile:
|
||||
|
||||
//byte[] serialized = SaneTsv.SerializeCommentedTsv(data);
|
||||
// Gives this error: error CS7036: There is no argument given that corresponds to the required parameter 'fileComment' of 'SaneTsv.SerializeCommentedTsv<T>(IList<T>, string)'
|
||||
|
||||
//Tsv<TestRecord> parsed = SaneTsv.ParseCommentedTsv<TestRecord>(serialized);
|
||||
// Gives this error: error CS0311: The type 'Program.TestRecord' cannot be used as type parameter 'T' in the generic type or method 'SaneTsv.ParseCommentedTsv<T>(byte[])'. There is no implicit reference conversion from 'Program.TestRecord' to 'NathanMcRae.SaneTsv.CommentedTsvRecord'.
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Try to parsed a Commented TSV as a Simple TSV";
|
||||
|
||||
string testString1 = "#This is a file comment\n" +
|
||||
"#One more file comment line\n" +
|
||||
"column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\n#This is a comment" +
|
||||
"\n#Another comment line" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord2> parsed = SaneTsv.ParseSimpleTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Try to parsed a Commented TSV as a Typed TSV";
|
||||
|
||||
string testString1 = "#This is a file comment\n" +
|
||||
"#One more file comment line\n" +
|
||||
"column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\n#This is a comment" +
|
||||
"\n#Another comment line" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord2> parsed = SaneTsv.ParseTypedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Try to parsed a Typed TSV as a Simple TSV";
|
||||
|
||||
string testString1 =
|
||||
"column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord2> parsed = SaneTsv.ParseSimpleTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("Done with tests");
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Bad bool test";
|
||||
try
|
||||
{
|
||||
string testString1 = "column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\nTUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
SaneTsv parsed = SaneTsv.ParseTypedTsv(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Comment test";
|
||||
string testString1 = "#This is a file comment\n" +
|
||||
"#One more file comment line\n" +
|
||||
"column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\n#This is a comment" +
|
||||
"\n#Another comment line" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
SaneTsv parsed = SaneTsv.ParseCommentedTsv(Encoding.UTF8.GetBytes(testString1));
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Serde test";
|
||||
string testString1 = "column1\tcolumn2\tcolumnthree\\nyep" +
|
||||
"\nTRUE\tvalue\\\\twoo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
SaneTsv parsed = SaneTsv.ParseSimpleTsv(Encoding.UTF8.GetBytes(testString1));
|
||||
string serialized = Encoding.UTF8.GetString(SaneTsv.SerializeSimpleTsv(parsed.ColumnNames, parsed.Records.Select(r => r.Fields.Select(f => f.ToString()).ToArray()).ToArray()));
|
||||
|
||||
if (testString1 == serialized)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Float binary test";
|
||||
var bytes = new List<byte>();
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes("somefloat:float64\tbinfloat:float64-le" +
|
||||
"\n1.5\t")); bytes.AddRange(BitConverter.GetBytes(1.5));
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes("\n-8.0000005E-14\t")); bytes.AddRange(BitConverter.GetBytes(-8.0000005E-14));
|
||||
|
||||
SaneTsv parsed = SaneTsv.ParseTypedTsv(bytes.ToArray());
|
||||
if ((double)parsed.Records[0]["binfloat"] == (double)parsed.Records[0]["somefloat"])
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("Done with tests");
|
||||
|
||||
{
|
||||
string testName = "Serde test";
|
||||
string[] headerNames =
|
||||
{
|
||||
"string-test",
|
||||
"bool-test",
|
||||
"float32-test",
|
||||
"float32-le-test",
|
||||
"float64-test",
|
||||
"float64-le-test",
|
||||
"uint32-test",
|
||||
"uint64-test",
|
||||
"int32-test",
|
||||
"int64-test",
|
||||
"binary-test",
|
||||
};
|
||||
|
||||
Type[] headerTypes =
|
||||
{
|
||||
typeof(SaneTsv.StringType),
|
||||
typeof(SaneTsv.BooleanType),
|
||||
typeof(SaneTsv.Float32Type),
|
||||
typeof(SaneTsv.Float32LEType),
|
||||
typeof(SaneTsv.Float64Type),
|
||||
typeof(SaneTsv.Float64LEType),
|
||||
typeof(SaneTsv.UInt32Type),
|
||||
typeof(SaneTsv.UInt64Type),
|
||||
typeof(SaneTsv.Int32Type),
|
||||
typeof(SaneTsv.Int64Type),
|
||||
typeof(SaneTsv.BinaryType),
|
||||
};
|
||||
|
||||
object[][] data =
|
||||
{
|
||||
new object[] { "test", true, 44.5f, 44.5f, -88e-3, -88e-3, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3 } },
|
||||
new object[] { "test2", false, 44.5000005f, 44.5000005f, -88e-30, -88e-30, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3, 4 } },
|
||||
new object[] { "test2", false, float.NaN, float.NaN, double.NaN, double.NaN, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3, 4 } },
|
||||
new object[] { "test2", false, float.NegativeInfinity, float.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3, 4 } },
|
||||
new object[] { "test2", false, float.PositiveInfinity, float.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3, 4 } },
|
||||
};
|
||||
|
||||
byte[] serialized = SaneTsv.SerializeTypedTsv(headerTypes, headerNames, data);
|
||||
|
||||
SaneTsv parsed = SaneTsv.ParseTypedTsv(serialized);
|
||||
|
||||
if ((float)parsed.Records[1]["float32-test"] == 44.5000005)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
}
|
||||
}
|
||||
// TODO: Check qNaN, sNaN, +inf, -inf values for float types
|
||||
|
Loading…
Reference in New Issue
Block a user