Skip to content

Deprecate QuantityType to better support custom quantities #864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.

using System;
Expand Down
15 changes: 9 additions & 6 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void GenerateStaticConstructor()
");

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

foreach (var unit in _quantity.Units)
Expand Down Expand Up @@ -147,10 +147,10 @@ private void GenerateStaticConstructor()
}
}

Writer.WL(@"
},
BaseUnit, Zero, BaseDimensions);
}
Writer.WL($@"
}},
BaseUnit, Zero, BaseDimensions, QuantityType.{_quantity.Name});
}}
");
}

Expand Down Expand Up @@ -236,6 +236,7 @@ private void GenerateStaticProperties()
/// <summary>
/// The <see cref=""QuantityType"" /> of this quantity.
/// </summary>
[Obsolete(""QuantityType will be removed in the future. Use Info property instead."")]
public static QuantityType QuantityType {{ get; }} = QuantityType.{_quantity.Name};

/// <summary>
Expand Down Expand Up @@ -792,7 +793,7 @@ public bool Equals({_quantity.Name} other, double tolerance, ComparisonType comp
/// <returns>A hash code for the current {_quantity.Name}.</returns>
public override int GetHashCode()
{{
return new {{ QuantityType, Value, Unit }}.GetHashCode();
return new {{ Info.Name, Value, Unit }}.GetHashCode();
}}

#endregion
Expand Down Expand Up @@ -1106,6 +1107,8 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider)
return Unit;
else if(conversionType == typeof(QuantityType))
return {_quantity.Name}.QuantityType;
else if(conversionType == typeof(QuantityInfo))
return {_quantity.Name}.Info;
else if(conversionType == typeof(BaseDimensions))
return {_quantity.Name}.BaseDimensions;
else
Expand Down
2 changes: 1 addition & 1 deletion CodeGen/Generators/UnitsNetGen/QuantityTypeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CodeGen.JsonTypes;
using CodeGen.JsonTypes;

namespace CodeGen.Generators.UnitsNetGen
{
Expand Down
49 changes: 48 additions & 1 deletion CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CodeGen.Helpers;
using CodeGen.Helpers;
using CodeGen.JsonTypes;

namespace CodeGen.Generators.UnitsNetGen
Expand All @@ -21,6 +21,7 @@ public override string Generate()
using JetBrains.Annotations;
using UnitsNet.InternalHelpers;
using UnitsNet.Units;
using System.Collections.Generic;

#nullable enable

Expand All @@ -31,12 +32,34 @@ namespace UnitsNet
/// </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(QuantityType quantityType, QuantityValue value)
{
switch(quantityType)
Expand All @@ -55,6 +78,30 @@ public static IQuantity FromQuantityType(QuantityType quantityType, QuantityValu
}
}

/// <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>
Expand Down
9 changes: 8 additions & 1 deletion CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,13 @@ public void Convert_ChangeType_QuantityType_EqualsQuantityType()
Assert.Equal(QuantityType.{_quantity.Name}, Convert.ChangeType(quantity, typeof(QuantityType)));
}}

[Fact]
public void Convert_ChangeType_QuantityInfo_EqualsQuantityInfo()
{{
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(1.0);
Assert.Equal({_quantity.Name}.Info, Convert.ChangeType(quantity, typeof(QuantityInfo)));
}}

[Fact]
public void Convert_ChangeType_BaseDimensions_EqualsBaseDimensions()
{{
Expand All @@ -680,7 +687,7 @@ public void Convert_ChangeType_InvalidType_ThrowsInvalidCastException()
public void GetHashCode_Equals()
{{
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(1.0);
Assert.Equal(new {{{_quantity.Name}.QuantityType, quantity.Value, quantity.Unit}}.GetHashCode(), quantity.GetHashCode());
Assert.Equal(new {{{_quantity.Name}.Info.Name, quantity.Value, quantity.Unit}}.GetHashCode(), quantity.GetHashCode());
}}
");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CodeGen.Helpers;
using CodeGen.Helpers;
using CodeGen.JsonTypes;

namespace CodeGen.Generators.UnitsNetWrcGen
Expand Down
4 changes: 2 additions & 2 deletions UnitsNet.Tests/CustomQuantities/HowMuch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace UnitsNet.Tests.CustomQuantities
{
Expand Down Expand Up @@ -26,7 +26,7 @@ public HowMuch(double value, HowMuchUnit unit)
public QuantityType Type => QuantityType.Undefined;
public BaseDimensions Dimensions => BaseDimensions.Dimensionless;

public QuantityInfo QuantityInfo => new QuantityInfo(Type,
public QuantityInfo QuantityInfo => new QuantityInfo("HowMuch",
new UnitInfo[]
{
new UnitInfo<HowMuchUnit>(HowMuchUnit.Some, BaseUnits.Undefined),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading