Fix unit name serialization
We just store the unit name as specified and serialize that (instead of using the name as given in UnitsNet)
This commit is contained in:
parent
78574d1872
commit
2b9bcd1a6d
14
SaneTsv.cs
14
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>(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
|
||||
{
|
||||
|
@ -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<UnitTest> parsed = SaneTsv.ParseExtraTsv<UnitTest>(Encoding.UTF8.GetBytes(testString1));
|
||||
string serialized = Encoding.UTF8.GetString(SaneTsv.SerializeExtraTsv<UnitTest>(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<UnitTest>(records));
|
||||
CommentedTsv<UnitTest> parsed = SaneTsv.ParseExtraTsv<UnitTest>(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}");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user