Skip to content

Commit 037f2c5

Browse files
committed
Sort inferred relations to keep generated operators in a consistent order
1 parent d95776b commit 037f2c5

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

CodeGen/Generators/QuantityRelationsParser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public static void ParseAndApplyRelations(string rootDir, Quantity[] quantities)
6060
.Select(r => r with { RightQuantity = timeSpanQuantity })
6161
.ToList());
6262

63+
// Sort all relations to keep generated operators in a consistent order.
64+
relations.Sort();
65+
6366
foreach (var quantity in quantities)
6467
{
6568
var quantityRelations = new List<QuantityRelation>();

CodeGen/JsonTypes/QuantityRelation.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

4+
using System;
5+
46
namespace CodeGen.JsonTypes
57
{
6-
internal record QuantityRelation
8+
internal record QuantityRelation : IComparable<QuantityRelation>
79
{
810
public string Operator = null!;
911

@@ -15,5 +17,18 @@ internal record QuantityRelation
1517

1618
public Quantity ResultQuantity = null!;
1719
public Unit ResultUnit = null!;
20+
21+
private string SortString => ResultQuantity.Name
22+
+ ResultUnit.SingularName
23+
+ LeftQuantity.Name
24+
+ LeftUnit.SingularName
25+
+ Operator
26+
+ RightQuantity.Name
27+
+ RightUnit.SingularName;
28+
29+
public int CompareTo(QuantityRelation? other)
30+
{
31+
return string.Compare(SortString, other?.SortString, StringComparison.Ordinal);
32+
}
1833
}
1934
}

0 commit comments

Comments
 (0)