-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Licensed under MIT No Attribution, see LICENSE file at the root. | ||
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using CodeGen.JsonTypes; | ||
using Newtonsoft.Json; | ||
|
||
namespace CodeGen.Generators | ||
{ | ||
internal static class QuantityRelationsParser | ||
{ | ||
private static List<Relation> ParseRelations(string rootDir) | ||
{ | ||
var relationsFileName = Path.Combine(rootDir, "Common/UnitRelations.json"); | ||
return JsonConvert.DeserializeObject<List<Relation>>(File.ReadAllText(relationsFileName)) ?? new List<Relation>(); | ||
} | ||
|
||
public static void ApplyRelations(string rootDir, IEnumerable<Quantity> quantities) | ||
{ | ||
var relations = ParseRelations(rootDir); | ||
var timespanRelations = new List<Relation>(); | ||
|
||
foreach (var relation in relations) | ||
{ | ||
relation.ResultQuantity = relation.Result?.Split('.')[0] ?? string.Empty; | ||
relation.LeftQuantity = relation.Left?.Split('.')[0] ?? string.Empty; | ||
relation.RightQuantity = relation.Right?.Split('.')[0] ?? string.Empty; | ||
|
||
if (relation.LeftQuantity == "Duration") | ||
{ | ||
timespanRelations.Add(relation with | ||
{ | ||
Left = relation.Left?.Replace("Duration.", "TimeSpan.Total") ?? string.Empty, | ||
LeftQuantity = "TimeSpan", | ||
}); | ||
} | ||
|
||
if (relation.RightQuantity == "Duration") | ||
{ | ||
timespanRelations.Add(relation with | ||
{ | ||
Right = relation.Right?.Replace("Duration.", "TimeSpan.Total") ?? string.Empty, | ||
RightQuantity = "TimeSpan", | ||
}); | ||
} | ||
} | ||
|
||
relations.AddRange(timespanRelations); | ||
|
||
foreach (var quantity in quantities) | ||
{ | ||
var quantityRelations = new List<Relation>(); | ||
|
||
foreach (var relation in relations) | ||
{ | ||
if (relation.LeftQuantity == quantity.Name) | ||
{ | ||
quantityRelations.Add(relation); | ||
if (relation is { Operator: "*", RightQuantity: "TimeSpan" or "double" }) | ||
{ | ||
quantityRelations.Add(relation.Swapped()); | ||
} | ||
} | ||
|
||
if (relation.RightQuantity == quantity.Name) | ||
{ | ||
if (relation.Operator == "inverse" || relation.Operator == "*" && relation.Left != relation.Right) | ||
{ | ||
quantityRelations.Add(relation.Swapped()); | ||
} | ||
|
||
if (relation is { Operator: "/", Left: "double" }) | ||
{ | ||
quantityRelations.Add(relation); | ||
} | ||
} | ||
} | ||
|
||
quantity.Relations = quantityRelations.ToArray(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Licensed under MIT No Attribution, see LICENSE file at the root. | ||
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet. | ||
|
||
namespace CodeGen.JsonTypes | ||
{ | ||
internal record Relation | ||
{ | ||
// 0649 Field is never assigned to | ||
#pragma warning disable 0649 | ||
|
||
public string Operator = null!; | ||
public string Result = null!; | ||
public string Left = null!; | ||
public string Right = null!; | ||
|
||
public string ResultQuantity = null!; | ||
public string LeftQuantity = null!; | ||
public string RightQuantity = null!; | ||
|
||
public Relation Swapped() => this with | ||
{ | ||
Left = Right, | ||
Right = Left, | ||
LeftQuantity = RightQuantity, | ||
RightQuantity = LeftQuantity | ||
}; | ||
|
||
// 0649 Field is never assigned to | ||
#pragma warning restore 0649 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters