Skip to content

Commit 1e5381c

Browse files
Add IValueQuantity<TValue> interface to expose the value type (#1207)
Ref #1205 - Add `IValueQuantity<TValue>` extending `IQuantity` to expose double/decimal types instead of `QuantityValue` - Add `IQuantity<TUnitType, out TValueType>`, extending `IValueQuantity<TValue>` with value specific `As(TUnitType)` method - Mark `IDecimalQuantity` obsolete, replaced by above --------- Co-authored-by: Andreas Gullberg Larsen <[email protected]>
1 parent 464ca72 commit 1e5381c

File tree

248 files changed

+1392
-157
lines changed

Some content is hidden

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

248 files changed

+1392
-157
lines changed

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,20 +263,10 @@ private void GenerateProperties()
263263
/// The numeric value this quantity was constructed with.
264264
/// </summary>
265265
public {_valueType} Value => _value;
266-
");
267266
268-
Writer.WL(@"
269267
/// <inheritdoc />
270268
QuantityValue IQuantity.Value => _value;
271-
");
272-
// Need to provide explicit interface implementation for decimal quantities like Information
273-
if (_quantity.ValueType == "decimal")
274-
Writer.WL(@"
275-
/// <inheritdoc cref=""IDecimalQuantity.Value""/>
276-
decimal IDecimalQuantity.Value => _value;
277-
");
278269
279-
Writer.WL($@"
280270
Enum IQuantity.Unit => Unit;
281271
282272
/// <inheritdoc />
@@ -959,6 +949,15 @@ double IQuantity.As(Enum unit)
959949
return (double)As(typedUnit);
960950
}}
961951
952+
/// <inheritdoc />
953+
{_quantity.ValueType} IValueQuantity<{_quantity.ValueType}>.As(Enum unit)
954+
{{
955+
if (!(unit is {_unitEnumName} typedUnit))
956+
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));
957+
958+
return As(typedUnit);
959+
}}
960+
962961
/// <summary>
963962
/// Converts this {_quantity.Name} to another {_quantity.Name} with the unit representation <paramref name=""unit"" />.
964963
/// </summary>

CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void DefaultCtor_ReturnsQuantityWithZeroValueAndBaseUnit()
164164
var quantity = new {_quantity.Name}();
165165
Assert.Equal(0, quantity.Value);");
166166
if (_quantity.ValueType == "decimal") Writer.WL(@"
167-
Assert.Equal(0m, ((IDecimalQuantity)quantity).Value);");
167+
Assert.Equal(0m, ((IValueQuantity<decimal>)quantity).Value);");
168168
Writer.WL($@"
169169
Assert.Equal({_baseUnitFullName}, quantity.Unit);
170170
}}
@@ -278,7 +278,7 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
278278
279279
if (SupportsSIUnitSystem)
280280
{{
281-
var value = (double) AsWithSIUnitSystem();
281+
var value = Convert.ToDouble(AsWithSIUnitSystem());
282282
Assert.Equal(1, value);
283283
}}
284284
else

Common/UnitDefinitions/Power.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
{
1313
"SingularName": "Watt",
1414
"PluralName": "Watts",
15+
"BaseUnits": {
16+
"L": "Meter",
17+
"M": "Kilogram",
18+
"T": "Second"
19+
},
1520
"FromUnitToBaseFunc": "{x}",
1621
"FromBaseToUnitFunc": "{x}",
1722
"Prefixes": [ "Femto", "Pico", "Nano", "Micro", "Milli", "Deci", "Deca", "Kilo", "Mega", "Giga", "Tera", "Peta" ],

UnitsNet.Serialization.JsonNet/AbbreviatedUnitsConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public override void WriteJson(JsonWriter writer, IQuantity? quantity, JsonSeria
7979

8080
// write the 'Value' using the actual type
8181
writer.WritePropertyName(ValueProperty);
82-
if (quantity is IDecimalQuantity decimalQuantity)
82+
if (quantity is IValueQuantity<decimal> decimalQuantity)
8383
{
8484
// cannot use `writer.WriteValue(decimalQuantity.Value)`: there is a hidden EnsureDecimalPlace(..) method call inside it that converts '123' to '123.0'
8585
writer.WriteRawValue(decimalQuantity.Value.ToString(CultureInfo.InvariantCulture));
@@ -177,7 +177,7 @@ protected string GetQuantityType(IQuantity quantity)
177177
{
178178
value = default;
179179
}
180-
else if (quantityInfo.Zero is IDecimalQuantity)
180+
else if (quantityInfo.Zero is IValueQuantity<decimal>)
181181
{
182182
value = decimal.Parse(valueToken, CultureInfo.InvariantCulture);
183183
}

UnitsNet.Serialization.JsonNet/UnitsNetBaseJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ protected ValueUnit ConvertIQuantity(IQuantity quantity)
181181
{
182182
quantity = quantity ?? throw new ArgumentNullException(nameof(quantity));
183183

184-
if (quantity is IDecimalQuantity d)
184+
if (quantity is IValueQuantity<decimal> d)
185185
{
186186
return new ExtendedValueUnit
187187
{

UnitsNet.Tests/CustomCode/IQuantityTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,48 @@ public void ToUnit_GivenSIUnitSystem_ReturnsSIQuantity()
5757
Assert.Equal(0.0508, inSI.Value);
5858
Assert.Equal(LengthUnit.Meter, inSI.Unit);
5959
}
60+
61+
62+
[Fact]
63+
public void IQuantityTUnitDouble_Value_ReturnsDouble()
64+
{
65+
IQuantity<TemperatureUnit, double> doubleQuantity = Temperature.FromDegreesCelsius(1234.5);
66+
Assert.IsType<double>(doubleQuantity.Value);
67+
}
68+
69+
[Fact]
70+
public void IQuantityTUnitDouble_AsEnum_ReturnsDouble()
71+
{
72+
IQuantity<TemperatureUnit, double> doubleQuantity = Temperature.FromDegreesCelsius(1234.5);
73+
Assert.IsType<double>(doubleQuantity.As(TemperatureUnit.Kelvin));
74+
}
75+
76+
[Fact]
77+
public void IQuantityTUnitDouble_AsUnitSystem_ReturnsDouble()
78+
{
79+
IQuantity<TemperatureUnit, double> doubleQuantity = Temperature.FromDegreesCelsius(1234.5);
80+
Assert.IsType<double>(doubleQuantity.As(UnitSystem.SI));
81+
}
82+
83+
[Fact]
84+
public void IQuantityTUnitDecimal_Value_ReturnsDecimal()
85+
{
86+
IQuantity<InformationUnit, decimal> decimalQuantity = Information.FromKilobytes(1234.5);
87+
Assert.IsType<decimal>(decimalQuantity.Value);
88+
}
89+
90+
[Fact]
91+
public void IQuantityTUnitDecimal_AsEnum_ReturnsDecimal()
92+
{
93+
IQuantity<InformationUnit, decimal> decimalQuantity = Information.FromKilobytes(1234.5);
94+
Assert.IsType<decimal>(decimalQuantity.As(InformationUnit.Byte));
95+
}
96+
97+
[Fact]
98+
public void IQuantityTUnitDecimal_AsUnitSystem_ReturnsDecimal()
99+
{
100+
IQuantity<PowerUnit, decimal> decimalQuantity = Power.FromMegawatts(1234.5);
101+
Assert.IsType<decimal>(decimalQuantity.As(UnitSystem.SI));
102+
}
60103
}
61104
}

UnitsNet.Tests/CustomCode/PowerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace UnitsNet.Tests
88
{
99
public class PowerTests : PowerTestsBase
1010
{
11-
protected override bool SupportsSIUnitSystem => false;
11+
protected override bool SupportsSIUnitSystem => true;
1212
protected override decimal FemtowattsInOneWatt => 1e15m;
1313

1414
protected override decimal GigajoulesPerHourInOneWatt => 3600e-9m;

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

Lines changed: 1 addition & 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: 1 addition & 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: 1 addition & 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: 1 addition & 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: 1 addition & 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: 1 addition & 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: 1 addition & 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: 1 addition & 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: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Lines changed: 1 addition & 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)