From 2b9bcd1a6d677c79347e7487b43364d5215c71cd Mon Sep 17 00:00:00 2001 From: Nathan McRae Date: Fri, 15 Mar 2024 23:15:35 -0700 Subject: [PATCH] Fix unit name serialization We just store the unit name as specified and serialize that (instead of using the name as given in UnitsNet) --- SaneTsv.cs | 14 ++++++++------ SaneTsvTest/Program.cs | 38 +++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/SaneTsv.cs b/SaneTsv.cs index 8c0ce2c..562a110 100644 --- a/SaneTsv.cs +++ b/SaneTsv.cs @@ -39,11 +39,13 @@ public class SaneTsv public class Iso8601Type : ColumnType { } public class PhysicalUnitsType : ColumnType { + public string UnitString { get; } public UnitsNet.UnitInfo Units { get; } public ColumnType BaseType { get; internal set; } - public PhysicalUnitsType(UnitsNet.UnitInfo units, ColumnType baseType) + public PhysicalUnitsType(string unitString, UnitsNet.UnitInfo units, ColumnType baseType) { + UnitString = unitString; Units = units; BaseType = baseType; } @@ -306,7 +308,7 @@ public class SaneTsv string unitName = columnTypeStrings[columnTypeStrings.Length - 3]; if (UnitsNet.Quantity.TryFromUnitAbbreviation(1, unitName, out UnitsNet.IQuantity quantity)) { - type = new PhysicalUnitsType(UnitsNet.Quantity.GetUnitInfo(quantity.Unit), new Float64Type()); + type = new PhysicalUnitsType(unitName, UnitsNet.Quantity.GetUnitInfo(quantity.Unit), new Float64Type()); } else { @@ -326,7 +328,7 @@ public class SaneTsv string unitName = columnTypeStrings[columnTypeStrings.Length - 3]; if (UnitsNet.Quantity.TryFromUnitAbbreviation(1, unitName, out UnitsNet.IQuantity quantity)) { - type = new PhysicalUnitsType(UnitsNet.Quantity.GetUnitInfo(quantity.Unit), new Float64LEType()); + type = new PhysicalUnitsType(unitName, UnitsNet.Quantity.GetUnitInfo(quantity.Unit), new Float64LEType()); } else { @@ -1273,7 +1275,7 @@ public class SaneTsv string unitName = match.Groups[1].Value; string baseType = match.Groups[2].Value; - return new PhysicalUnitsType(ParseUnit(unitName), GetColumnFromString(baseType)); + return new PhysicalUnitsType(unitName, ParseUnit(unitName), GetColumnFromString(baseType)); //if (UnitsNet.Quantity.TryFromUnitAbbreviation(1, unitName, out UnitsNet.IQuantity quantity)) //{ // return new PhysicalUnitsType(UnitsNet.Quantity.GetUnitInfo(quantity.Unit), GetColumnFromString(baseType)); @@ -1336,7 +1338,7 @@ public class SaneTsv // TODO //UnitsNet.UnitInfo a = new UnitsNet.UnitInfo([d]) var a = new UnitsNet.UnitInfo(UnitsNet.Units.MassUnit.Kilogram, "kgs", new UnitsNet.BaseUnits(mass: UnitsNet.Units.MassUnit.Kilogram)); - return new PhysicalUnitsType(a, new Float64Type()); + return new PhysicalUnitsType("kg", a, new Float64Type()); } else { @@ -1396,7 +1398,7 @@ public class SaneTsv } else if (type is PhysicalUnitsType unit) { - return $"{unit.Units.Name}:{UnitsTypeText}:{GetNameFromColumn(unit.BaseType)}"; + return $"{unit.UnitString}:{UnitsTypeText}:{GetNameFromColumn(unit.BaseType)}"; } else { diff --git a/SaneTsvTest/Program.cs b/SaneTsvTest/Program.cs index e8eddec..bf249de 100644 --- a/SaneTsvTest/Program.cs +++ b/SaneTsvTest/Program.cs @@ -101,14 +101,38 @@ internal class Program : SaneTsv { string testName = "Serde unit"; - string testString1 = "# ExtraTSV V0.0.1\n" + - "id:uint32\tvalue:m/s:ph-unit:float64\n" + - "0\t1.5\n" + - "1\t5.4e3"; - CommentedTsv parsed = SaneTsv.ParseExtraTsv(Encoding.UTF8.GetBytes(testString1)); - string serialized = Encoding.UTF8.GetString(SaneTsv.SerializeExtraTsv(parsed.Records)); - if (testString1 == serialized) + UnitTest[] records = new UnitTest[] + { + new UnitTest + { + Id = 1, + Value = UnitsNet.Speed.FromMetersPerSecond(5.03), + }, + new UnitTest + { + Id = 5, + Value = UnitsNet.Speed.FromMetersPerSecond(double.NaN), + }, + new UnitTest + { + Id = 1000000, + Value = UnitsNet.Speed.FromMetersPerSecond(9859873.498), + }, + }; + + string serialized = Encoding.UTF8.GetString(SaneTsv.SerializeExtraTsv(records)); + CommentedTsv parsed = SaneTsv.ParseExtraTsv(Encoding.UTF8.GetBytes(serialized)); + + bool match = true; + for (int i = 0; i < records.Length; i++) + { + match = match && records[i].Id == parsed.Records[i].Id && + (records[i].Value.Equals(parsed.Records[i].Value, UnitsNet.Speed.FromMetersPerSecond(1E-1)) + || (double.IsNaN(records[i].Value.Value) && double.IsNaN(parsed.Records[i].Value.Value))); + } + + if (match) { Console.WriteLine($"Passed {testName}"); }