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

Implement direct conversion functions #1227

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
17 changes: 16 additions & 1 deletion CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,22 @@ private bool TryToUnit({_quantity.Name}Unit unit, [NotNullWhen(true)] out {_quan
}}

{_quantity.Name}? convertedOrNull = (Unit, unit) switch
{{
{{");

foreach(Unit unit in _quantity.Units)
{
foreach(var conversion in unit.Conversions)
{
var func2 = conversion.Formula.Replace("{x}", "_value");
Writer.WL($@"
({_quantity.Name}Unit.{unit.SingularName}, {_unitEnumName}.{conversion.Target}) => new {_quantity.Name}({func2}, {_unitEnumName}.{conversion.Target}),");
}
}

if(_quantity.Units.Any(unit => unit.Conversions.Any()))
Writer.WL();

Writer.WL($@"
// {_quantity.Name}Unit -> BaseUnit");

foreach (Unit unit in _quantity.Units)
Expand Down
19 changes: 19 additions & 0 deletions CodeGen/JsonTypes/Conversions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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;

namespace CodeGen.JsonTypes
{
internal class Conversions
{
// 0649 Field is never assigned to
#pragma warning disable 0649

public string Target = null!;
public string Formula = null!;

// 0649 Field is never assigned to
#pragma warning restore 0649
}
}
1 change: 1 addition & 0 deletions CodeGen/JsonTypes/Unit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal class Unit
#pragma warning disable 0649

public BaseUnits? BaseUnits;
public Conversions[] Conversions = Array.Empty<Conversions>();
public string FromBaseToUnitFunc = null!;
public string FromUnitToBaseFunc = null!;
public Localization[] Localization = Array.Empty<Localization>();
Expand Down
6 changes: 6 additions & 0 deletions Common/UnitDefinitions/Mass.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@
"BaseUnits": {
"M": "Ounce"
},
"Conversions": [
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DirectConversions maybe, to be more explicit that this is an optional thing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@angularsen I think I would actually make it mandatory. I may take in stages, but maybe the first stage is the convert the current FromUnitToBaseFunc/FromBaseToUnitFunc to the Conversions?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh gotcha, yes I suppose we could refactor the existing definitions to this.

The upside is more explicit naming, the old naming is kinda confusing. Also gathering all conversion functions, both direct and via base, in one place might be better.

The downside is that it is maybe not as obvious that the conversions need to go via a "base" unit, but we could maybe fail codegen if any unit has missing conversions from/to base unit?

{
"Target": "Pound",
"Formula": "{x} / 16.0"
}
],
"FromUnitToBaseFunc": "{x} * 0.028349523125",
"FromBaseToUnitFunc": "{x} / 0.028349523125",
"XmlDocSummary": "The international avoirdupois ounce (abbreviated oz) is defined as exactly 28.349523125 g under the international yard and pound agreement of 1959, signed by the United States and countries of the Commonwealth of Nations. 16 oz make up an avoirdupois pound.",
Expand Down
2 changes: 2 additions & 0 deletions UnitsNet/GeneratedCode/Quantities/Mass.g.cs

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