Skip to content

Commit c46d064

Browse files
generateuiRuud Poutsmaangularsen
authored
Deprecate QuantityType to better support custom quantities (#864)
* obsolete QuantityType instead of removing it straight away * Pass QuantityType as argument with default value, instead of looking up in Quantity. Co-authored-by: Ruud Poutsma <[email protected]> Co-authored-by: Andreas Gullberg Larsen <[email protected]>
1 parent 4993a2a commit c46d064

File tree

225 files changed

+2098
-482
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

225 files changed

+2098
-482
lines changed

CodeGen/Generators/UnitsNetGen/IQuantityTestClassGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed under MIT No Attribution, see LICENSE file at the root.
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

44
using System;

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private void GenerateStaticConstructor()
117117
");
118118

119119
Writer.WL($@"
120-
Info = new QuantityInfo<{_unitEnumName}>(QuantityType.{_quantity.Name},
120+
Info = new QuantityInfo<{_unitEnumName}>(""{_quantity.Name}"",
121121
new UnitInfo<{_unitEnumName}>[] {{");
122122

123123
foreach (var unit in _quantity.Units)
@@ -147,10 +147,10 @@ private void GenerateStaticConstructor()
147147
}
148148
}
149149

150-
Writer.WL(@"
151-
},
152-
BaseUnit, Zero, BaseDimensions);
153-
}
150+
Writer.WL($@"
151+
}},
152+
BaseUnit, Zero, BaseDimensions, QuantityType.{_quantity.Name});
153+
}}
154154
");
155155
}
156156

@@ -236,6 +236,7 @@ private void GenerateStaticProperties()
236236
/// <summary>
237237
/// The <see cref=""QuantityType"" /> of this quantity.
238238
/// </summary>
239+
[Obsolete(""QuantityType will be removed in the future. Use Info property instead."")]
239240
public static QuantityType QuantityType {{ get; }} = QuantityType.{_quantity.Name};
240241
241242
/// <summary>
@@ -792,7 +793,7 @@ public bool Equals({_quantity.Name} other, double tolerance, ComparisonType comp
792793
/// <returns>A hash code for the current {_quantity.Name}.</returns>
793794
public override int GetHashCode()
794795
{{
795-
return new {{ QuantityType, Value, Unit }}.GetHashCode();
796+
return new {{ Info.Name, Value, Unit }}.GetHashCode();
796797
}}
797798
798799
#endregion
@@ -1106,6 +1107,8 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider)
11061107
return Unit;
11071108
else if(conversionType == typeof(QuantityType))
11081109
return {_quantity.Name}.QuantityType;
1110+
else if(conversionType == typeof(QuantityInfo))
1111+
return {_quantity.Name}.Info;
11091112
else if(conversionType == typeof(BaseDimensions))
11101113
return {_quantity.Name}.BaseDimensions;
11111114
else

CodeGen/Generators/UnitsNetGen/QuantityTypeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using CodeGen.JsonTypes;
1+
using CodeGen.JsonTypes;
22

33
namespace CodeGen.Generators.UnitsNetGen
44
{

CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using CodeGen.Helpers;
1+
using CodeGen.Helpers;
22
using CodeGen.JsonTypes;
33

44
namespace CodeGen.Generators.UnitsNetGen
@@ -21,6 +21,7 @@ public override string Generate()
2121
using JetBrains.Annotations;
2222
using UnitsNet.InternalHelpers;
2323
using UnitsNet.Units;
24+
using System.Collections.Generic;
2425
2526
#nullable enable
2627
@@ -31,12 +32,34 @@ namespace UnitsNet
3132
/// </summary>
3233
public static partial class Quantity
3334
{
35+
/// <summary>
36+
/// All QuantityInfo instances mapped by quantity name that are present in UnitsNet by default.
37+
/// </summary>
38+
public static readonly IDictionary<string, QuantityInfo> ByName = new Dictionary<string, QuantityInfo>
39+
{");
40+
foreach (var quantity in _quantities)
41+
Writer.WL($@"
42+
{{ ""{quantity.Name}"", {quantity.Name}.Info }},");
43+
Writer.WL(@"
44+
};
45+
46+
// Used by the QuantityInfo .ctor to map a name to a QuantityType. Will be removed when QuantityType
47+
// will be removed.
48+
internal static readonly IDictionary<string, QuantityType> QuantityTypeByName = new Dictionary<string, QuantityType>
49+
{");
50+
foreach (var quantity in _quantities)
51+
Writer.WL($@"
52+
{{ ""{quantity.Name}"", QuantityType.{quantity.Name} }},");
53+
Writer.WL(@"
54+
};
55+
3456
/// <summary>
3557
/// Dynamically constructs a quantity of the given <see cref=""QuantityType""/> with the value in the quantity's base units.
3658
/// </summary>
3759
/// <param name=""quantityType"">The <see cref=""QuantityType""/> of the quantity to create.</param>
3860
/// <param name=""value"">The value to construct the quantity with.</param>
3961
/// <returns>The created quantity.</returns>
62+
[Obsolete(""QuantityType will be removed. Use FromQuantityInfo(QuantityInfo, QuantityValue) instead."")]
4063
public static IQuantity FromQuantityType(QuantityType quantityType, QuantityValue value)
4164
{
4265
switch(quantityType)
@@ -55,6 +78,30 @@ public static IQuantity FromQuantityType(QuantityType quantityType, QuantityValu
5578
}
5679
}
5780
81+
/// <summary>
82+
/// Dynamically constructs a quantity of the given <see cref=""QuantityInfo""/> with the value in the quantity's base units.
83+
/// </summary>
84+
/// <param name=""quantityInfo"">The <see cref=""QuantityInfo""/> of the quantity to create.</param>
85+
/// <param name=""value"">The value to construct the quantity with.</param>
86+
/// <returns>The created quantity.</returns>
87+
public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValue value)
88+
{
89+
switch(quantityInfo.Name)
90+
{");
91+
foreach (var quantity in _quantities)
92+
{
93+
var quantityName = quantity.Name;
94+
Writer.WL($@"
95+
case ""{quantityName}"":
96+
return {quantityName}.From(value, {quantityName}.BaseUnit);");
97+
}
98+
99+
Writer.WL(@"
100+
default:
101+
throw new ArgumentException($""{quantityInfo.Name} is not a supported quantity."");
102+
}
103+
}
104+
58105
/// <summary>
59106
/// Try to dynamically construct a quantity.
60107
/// </summary>

CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,13 @@ public void Convert_ChangeType_QuantityType_EqualsQuantityType()
662662
Assert.Equal(QuantityType.{_quantity.Name}, Convert.ChangeType(quantity, typeof(QuantityType)));
663663
}}
664664
665+
[Fact]
666+
public void Convert_ChangeType_QuantityInfo_EqualsQuantityInfo()
667+
{{
668+
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(1.0);
669+
Assert.Equal({_quantity.Name}.Info, Convert.ChangeType(quantity, typeof(QuantityInfo)));
670+
}}
671+
665672
[Fact]
666673
public void Convert_ChangeType_BaseDimensions_EqualsBaseDimensions()
667674
{{
@@ -680,7 +687,7 @@ public void Convert_ChangeType_InvalidType_ThrowsInvalidCastException()
680687
public void GetHashCode_Equals()
681688
{{
682689
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(1.0);
683-
Assert.Equal(new {{{_quantity.Name}.QuantityType, quantity.Value, quantity.Unit}}.GetHashCode(), quantity.GetHashCode());
690+
Assert.Equal(new {{{_quantity.Name}.Info.Name, quantity.Value, quantity.Unit}}.GetHashCode(), quantity.GetHashCode());
684691
}}
685692
");
686693

CodeGen/Generators/UnitsNetWrcGen/StaticQuantityGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using CodeGen.Helpers;
1+
using CodeGen.Helpers;
22
using CodeGen.JsonTypes;
33

44
namespace CodeGen.Generators.UnitsNetWrcGen

UnitsNet.Tests/CustomQuantities/HowMuch.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22

33
namespace UnitsNet.Tests.CustomQuantities
44
{
@@ -26,7 +26,7 @@ public HowMuch(double value, HowMuchUnit unit)
2626
public QuantityType Type => QuantityType.Undefined;
2727
public BaseDimensions Dimensions => BaseDimensions.Dimensionless;
2828

29-
public QuantityInfo QuantityInfo => new QuantityInfo(Type,
29+
public QuantityInfo QuantityInfo => new QuantityInfo("HowMuch",
3030
new UnitInfo[]
3131
{
3232
new UnitInfo<HowMuchUnit>(HowMuchUnit.Some, BaseUnits.Undefined),

UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AmountOfSubstanceTestsBase.g.cs

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AmplitudeRatioTestsBase.g.cs

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/ApparentEnergyTestsBase.g.cs

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/ApparentPowerTestsBase.g.cs

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AreaDensityTestsBase.g.cs

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AreaMomentOfInertiaTestsBase.g.cs

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)