-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathStaticQuantityGenerator.cs
More file actions
173 lines (156 loc) · 6.7 KB
/
StaticQuantityGenerator.cs
File metadata and controls
173 lines (156 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using CodeGen.Helpers;
using CodeGen.JsonTypes;
namespace CodeGen.Generators.UnitsNetGen
{
internal class StaticQuantityGenerator : GeneratorBase
{
private readonly Quantity[] _quantities;
public StaticQuantityGenerator(Quantity[] quantities)
{
_quantities = quantities;
}
public override string Generate()
{
Writer.WL(GeneratedFileHeader);
Writer.WL(@"
using System;
using System.Globalization;
using JetBrains.Annotations;
using UnitsNet.InternalHelpers;
using UnitsNet.Units;
using System.Collections.Generic;
#nullable enable
namespace UnitsNet
{
/// <summary>
/// Dynamically parse or construct quantities when types are only known at runtime.
/// </summary>
public static partial class Quantity
{
/// <summary>
/// All QuantityInfo instances mapped by quantity name that are present in UnitsNet by default.
/// </summary>
public static readonly IDictionary<string, QuantityInfo> ByName = new Dictionary<string, QuantityInfo>
{");
foreach (var quantity in _quantities)
Writer.WL($@"
{{ ""{quantity.Name}"", {quantity.Name}.Info }},");
Writer.WL(@"
};
// Used by the QuantityInfo .ctor to map a name to a QuantityType. Will be removed when QuantityType
// will be removed.
internal static readonly IDictionary<string, QuantityType> QuantityTypeByName = new Dictionary<string, QuantityType>
{");
foreach (var quantity in _quantities)
Writer.WL($@"
{{ ""{quantity.Name}"", QuantityType.{quantity.Name} }},");
Writer.WL(@"
};
/// <summary>
/// Dynamically constructs a quantity of the given <see cref=""QuantityType""/> with the value in the quantity's base units.
/// </summary>
/// <param name=""quantityType"">The <see cref=""QuantityType""/> of the quantity to create.</param>
/// <param name=""value"">The value to construct the quantity with.</param>
/// <returns>The created quantity.</returns>
[Obsolete(""QuantityType will be removed. Use FromQuantityInfo(QuantityInfo, QuantityValue) instead."")]
public static IQuantity FromQuantityType<T>(QuantityType quantityType, QuantityValue value)
{
switch(quantityType)
{");
foreach (var quantity in _quantities)
{
var quantityName = quantity.Name;
Writer.WL($@"
case QuantityType.{quantityName}:
return {quantityName}<T>.From(value, {quantityName}<T>.BaseUnit);");
}
Writer.WL(@"
default:
throw new ArgumentException($""{quantityType} is not a supported quantity type."");
}
}
/// <summary>
/// Dynamically constructs a quantity of the given <see cref=""QuantityInfo""/> with the value in the quantity's base units.
/// </summary>
/// <param name=""quantityInfo"">The <see cref=""QuantityInfo""/> of the quantity to create.</param>
/// <param name=""value"">The value to construct the quantity with.</param>
/// <returns>The created quantity.</returns>
public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValue value)
{
switch(quantityInfo.Name)
{");
foreach (var quantity in _quantities)
{
var quantityName = quantity.Name;
Writer.WL($@"
case ""{quantityName}"":
return {quantityName}.From(value, {quantityName}.BaseUnit);");
}
Writer.WL(@"
default:
throw new ArgumentException($""{quantityInfo.Name} is not a supported quantity."");
}
}
/// <summary>
/// Try to dynamically construct a quantity.
/// </summary>
/// <param name=""value"">Numeric value.</param>
/// <param name=""unit"">Unit enum value.</param>
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
/// <returns><c>True</c> if successful with <paramref name=""quantity""/> assigned the value, otherwise <c>false</c>.</returns>
public static bool TryFrom<T>(QuantityValue value, Enum unit, out IQuantity? quantity)
{
switch (unit)
{");
foreach (var quantity in _quantities)
{
var quantityName = quantity.Name;
var unitTypeName = $"{quantityName}Unit";
var unitValue = unitTypeName.ToCamelCase();
Writer.WL($@"
case {unitTypeName} {unitValue}:
quantity = {quantityName}<T>.From(value, {unitValue});
return true;");
}
Writer.WL(@"
default:
{
quantity = default(IQuantity);
return false;
}
}
}
/// <summary>
/// Try to dynamically parse a quantity string representation.
/// </summary>
/// <param name=""formatProvider"">The format provider to use for lookup. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
/// <param name=""quantityType"">Type of quantity, such as <see cref=""Length{T}""/>.</param>
/// <param name=""quantityString"">Quantity string representation, such as ""1.5 kg"". Must be compatible with given quantity type.</param>
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
/// <returns>The parsed quantity.</returns>
public static bool TryParse<T>(IFormatProvider? formatProvider, Type quantityType, string quantityString, out IQuantity? quantity)
{
quantity = default(IQuantity);
if (!typeof(IQuantity).Wrap().IsAssignableFrom(quantityType))
return false;
var parser = QuantityParser.Default;
switch(quantityType)
{");
foreach (var quantity in _quantities)
{
var quantityName = quantity.Name;
Writer.WL($@"
case Type _ when quantityType == typeof({quantityName}<T>):
return parser.TryParse<{quantityName}<T>, {quantityName}Unit>(quantityString, formatProvider, {quantityName}<T>.From, out quantity);");
}
Writer.WL(@"
default:
return false;
}
}
}
}");
return Writer.ToString();
}
}
}