Skip to content

Commit

Permalink
Sort inferred relations to keep generated operators in a consistent o…
Browse files Browse the repository at this point in the history
…rder
  • Loading branch information
Muximize committed Jan 8, 2024
1 parent c7918a3 commit 4de9789
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CodeGen/Generators/QuantityRelationsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public static void ParseAndApplyRelations(string rootDir, Quantity[] quantities)
.Select(r => r with { RightQuantity = timeSpanQuantity })
.ToList());

// Sort all relations to keep generated operators in a consistent order.
relations.Sort();

foreach (var quantity in quantities)
{
var quantityRelations = new List<QuantityRelation>();
Expand Down
17 changes: 16 additions & 1 deletion CodeGen/JsonTypes/QuantityRelation.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using System;

namespace CodeGen.JsonTypes
{
internal record QuantityRelation
internal record QuantityRelation : IComparable<QuantityRelation>
{
public string Operator = null!;

Expand All @@ -15,5 +17,18 @@ internal record QuantityRelation

public Quantity ResultQuantity = null!;
public Unit ResultUnit = null!;

private string SortString => ResultQuantity.Name
+ ResultUnit.SingularName
+ LeftQuantity.Name
+ LeftUnit.SingularName
+ Operator
+ RightQuantity.Name
+ RightUnit.SingularName;

public int CompareTo(QuantityRelation? other)
{
return string.Compare(SortString, other?.SortString, StringComparison.Ordinal);
}
}
}

0 comments on commit 4de9789

Please sign in to comment.