Skip to content
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

Include the non-prefixed BaseUnits in the BaseUnitPrefixes #1492

Merged
merged 2 commits into from
Jan 19, 2025
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
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