-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathNumberExtensionsGenerator.cs
More file actions
60 lines (48 loc) · 1.6 KB
/
NumberExtensionsGenerator.cs
File metadata and controls
60 lines (48 loc) · 1.6 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
using System;
using CodeGen.JsonTypes;
namespace CodeGen.Generators.UnitsNetGen
{
internal class NumberExtensionsGenerator : GeneratorBase
{
private readonly Unit[] _units;
private readonly string _quantityName;
public NumberExtensionsGenerator(Quantity quantity)
{
if (quantity is null)
throw new ArgumentNullException(nameof(quantity));
_units = quantity.Units;
_quantityName = quantity.Name;
}
public override string Generate()
{
Writer.WL(GeneratedFileHeader);
Writer.WL(
$@"
using System;
#nullable enable
namespace UnitsNet.NumberExtensions.NumberTo{_quantityName}
{{
/// <summary>
/// A number to {_quantityName} Extensions
/// </summary>
public static class NumberTo{_quantityName}Extensions
{{");
foreach (var unit in _units)
{
Writer.WL(2, $@"
/// <inheritdoc cref=""{_quantityName}{{T}}.From{unit.PluralName}(T)"" />");
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit.ObsoleteText));
Writer.WL(2, $@"public static {_quantityName}<double> {unit.PluralName}<T>(this T value) =>
{_quantityName}<double>.From{unit.PluralName}(Convert.ToDouble(value));
");
}
Writer.WL(1, @"}
}");
return Writer.ToString();
}
private static string? GetObsoleteAttributeOrNull(string obsoleteText) =>
string.IsNullOrWhiteSpace(obsoleteText) ?
null :
$"[System.Obsolete(\"{obsoleteText}\")]";
}
}