|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using CodeGen.Helpers; |
| 4 | +using CodeGen.JsonTypes; |
| 5 | + |
| 6 | +namespace CodeGen.Generators.UnitsNetGen |
| 7 | +{ |
| 8 | + internal class UnitSystemInfoGenerator : GeneratorBase |
| 9 | + { |
| 10 | + private readonly Quantity[] _quantities; |
| 11 | + private readonly string _unitSystemName; |
| 12 | + |
| 13 | + public UnitSystemInfoGenerator(string unitSystemName, Quantity[] quantities) |
| 14 | + { |
| 15 | + _quantities = quantities; |
| 16 | + _unitSystemName = unitSystemName; |
| 17 | + } |
| 18 | + |
| 19 | + public override string Generate() |
| 20 | + { |
| 21 | + Writer.WL(GeneratedFileHeader); |
| 22 | + Writer.WL($@" |
| 23 | +// ReSharper disable once CheckNamespace |
| 24 | +namespace UnitsNet.UnitSystems |
| 25 | +{{ |
| 26 | +
|
| 27 | + public partial class {_unitSystemName} |
| 28 | + {{ |
| 29 | + /// <summary> |
| 30 | + /// Returns the list of unit mappings for the current unit system |
| 31 | + /// </summary> |
| 32 | + /// <returns>The list of unit system infos</returns> |
| 33 | + public static UnitSystemInfo[] GetDefaultSystemUnits() |
| 34 | + {{ |
| 35 | + return new UnitSystemInfo[] |
| 36 | + {{"); |
| 37 | + foreach (Quantity quantity in _quantities) |
| 38 | + { |
| 39 | + UnitSystemMapping unitSystemMapping = quantity.UnitSystems.FirstOrDefault(x => x.UnitSystem == _unitSystemName); |
| 40 | + if (unitSystemMapping == null) |
| 41 | + { |
| 42 | + var firstCommonUnitIndex = Array.FindIndex(quantity.Units, x => x.UnitSystems.Contains(_unitSystemName)); |
| 43 | + |
| 44 | + if (firstCommonUnitIndex < 0) |
| 45 | + { |
| 46 | + Writer.WL(4, "null,"); // no mapping for current quantity |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + Writer.WL(4, $@"new UnitSystemInfo(null, new UnitInfo[]{{"); |
| 51 | + Writer.WL(5, $@"{quantity.Name}.Info.UnitInfos[{firstCommonUnitIndex}],"); |
| 52 | + var startingIndex = firstCommonUnitIndex + 1; |
| 53 | + while (true) |
| 54 | + { |
| 55 | + var nextIndex = Array.FindIndex(quantity.Units, startingIndex, x => x.UnitSystems.Contains(_unitSystemName)); |
| 56 | + if (nextIndex < 0) |
| 57 | + { |
| 58 | + Writer.WL(5, @"}),"); |
| 59 | + break; |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + Writer.WL(5, $@"{quantity.Name}.Info.UnitInfos[{nextIndex}],"); |
| 64 | + startingIndex = nextIndex + 1; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + // find the unit index in the generated unit infos |
| 72 | + var defaultIndex = Array.FindIndex(quantity.Units, x => x.SingularName == unitSystemMapping.BaseUnit); |
| 73 | + |
| 74 | + Writer.WL(4, $@"new UnitSystemInfo({quantity.Name}.Info.UnitInfos[{defaultIndex}], new UnitInfo[]{{"); |
| 75 | + var startingIndex = 0; |
| 76 | + while(true) |
| 77 | + { |
| 78 | + var nextIndex = Array.FindIndex(quantity.Units, startingIndex, x => x.UnitSystems.Contains(_unitSystemName)); |
| 79 | + if (nextIndex < 0) |
| 80 | + { |
| 81 | + Writer.WL(5, @"}),"); |
| 82 | + break; |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + Writer.WL(5, $@"{quantity.Name}.Info.UnitInfos[{nextIndex}],"); |
| 87 | + startingIndex = nextIndex + 1; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Writer.WL(@" |
| 94 | + }; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +"); |
| 99 | + |
| 100 | + return Writer.ToString(); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments