Skip to content

Commit

Permalink
Include the non-prefixed BaseUnits in the BaseUnitPrefixes (#1492)
Browse files Browse the repository at this point in the history
Include the non-prefixed `BaseUnits` in the `BaseUnitPrefixes`:
- the `BaseUnitPrefixes` now also handles the case where prefixing an
already prefixed base unit results in a non-prefixed base unit
- regenerating the quantities resulted in a number of new `BaseUnits` to
become available (as well as some which got their scales reduced)
  • Loading branch information
lipchev authored Jan 19, 2025
1 parent 7556b7c commit 428b58b
Show file tree
Hide file tree
Showing 24 changed files with 44 additions and 32 deletions.
9 changes: 5 additions & 4 deletions CodeGen/Helpers/PrefixBuilder/BaseUnitPrefix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
namespace CodeGen.Helpers.PrefixBuilder;

/// <summary>
/// Represents a unique key that combines a base unit and a prefix.
/// Represents a unique combination of a base unit and an optional prefix, used to identify a prefixed unit.
/// </summary>
/// <param name="BaseUnit">
/// The base unit associated with the prefix. For example, "Gram".
/// The base unit associated with the prefix, such as "Gram".
/// </param>
/// <param name="Prefix">
/// The prefix applied to the base unit. For example, <see cref="JsonTypes.Prefix.Kilo" />.
/// The prefix applied to the base unit, such as <see cref="JsonTypes.Prefix.Kilo" />.
/// <para>If the prefix exponent value is 0, this parameter will be <c>null</c>.</para>
/// </param>
internal readonly record struct BaseUnitPrefix(string BaseUnit, Prefix Prefix);
internal readonly record struct BaseUnitPrefix(string BaseUnit, Prefix? Prefix);
17 changes: 14 additions & 3 deletions CodeGen/Helpers/PrefixBuilder/BaseUnitPrefixes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public static BaseUnitPrefixes FromBaseUnits(IEnumerable<Unit> baseUnits)
{
var unitName = baseUnit.SingularName;
prefixedStringFactors[unitName] = new PrefixScaleFactor(unitName, 0);
baseUnitPrefixConversions[new BaseUnitPrefix(unitName, null)] = unitName;
foreach (Prefix prefix in baseUnit.Prefixes)
{
var prefixedUnitName = prefix + unitName.ToCamelCase();
Expand Down Expand Up @@ -123,10 +124,20 @@ internal bool TryGetMatchingPrefix(string unitName, int exponent, Prefix prefix,
{
var (quotient, remainder) = int.DivRem(prefixFactor, exponent);
// Ensure the prefix factor is divisible by the exponent without a remainder and that there is a valid prefix matching the target scale
if (remainder == 0 && TryGetPrefixWithScale(targetPrefixFactor.ScaleFactor + quotient, out Prefix calculatedPrefix))
if (remainder == 0)
{
matchingPrefix = new BaseUnitPrefix(targetPrefixFactor.BaseUnit, calculatedPrefix);
return true;
if (targetPrefixFactor.ScaleFactor + quotient == 0)
{
// when the resulting exponent is 0: return the non-prefixed BaseUnit
matchingPrefix = new BaseUnitPrefix(targetPrefixFactor.BaseUnit, null);
return true;
}

if (TryGetPrefixWithScale(targetPrefixFactor.ScaleFactor + quotient, out Prefix calculatedPrefix))
{
matchingPrefix = new BaseUnitPrefix(targetPrefixFactor.BaseUnit, calculatedPrefix);
return true;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion UnitsNet/GeneratedCode/Quantities/ElectricAdmittance.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.

2 changes: 1 addition & 1 deletion UnitsNet/GeneratedCode/Quantities/ElectricCapacitance.g.cs

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

2 changes: 1 addition & 1 deletion UnitsNet/GeneratedCode/Quantities/ElectricConductance.g.cs

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

2 changes: 1 addition & 1 deletion UnitsNet/GeneratedCode/Quantities/ElectricImpedance.g.cs

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

2 changes: 1 addition & 1 deletion UnitsNet/GeneratedCode/Quantities/ElectricInductance.g.cs

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

2 changes: 1 addition & 1 deletion UnitsNet/GeneratedCode/Quantities/ElectricPotential.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.

2 changes: 1 addition & 1 deletion UnitsNet/GeneratedCode/Quantities/ElectricReactance.g.cs

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

Loading

0 comments on commit 428b58b

Please sign in to comment.