-
Notifications
You must be signed in to change notification settings - Fork 403
🎉Add new NumberExtensionsCS14 nuget package
#1620
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
angularsen
merged 18 commits into
angularsen:master
from
Ted-Jin-Lab:number-extensions-cs14
Sep 21, 2025
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0a404e8
🎉Add new `NumberExtensionsCS14` nuget pakcage to make `5.Meters` work…
Ted-Jin-Lab fb2c9f1
🐛langversion fix.
Ted-Jin-Lab 28224d6
Merge branch 'master' into number-extensions-cs14
angularsen 9546427
Fix CI/CD pipeline for new projects and C# 14 support
angularsen 59e165e
Fix PackageId and update title/description
angularsen ac06e03
Merge branch 'master' into number-extensions-cs14
angularsen a88e09f
Add C# 14 extension members documentation to README
angularsen ef3fdbe
Fix CI and add global.json to specify .NET 10 SDK preview
angularsen fcd388e
Merge branch 'master' into number-extensions-cs14
angularsen af9d18a
Fix CI/CD pipeline to install .NET 9 SDK
angularsen e2e4af5
Merge branch 'master' into number-extensions-cs14
angularsen 943cb99
Fix CS14 project compile error with ILLink analyzer
angularsen c5ba1f9
Fix Azure Pipelines to install .NET 8 SDK
angularsen 499ad55
Fix global.json to allow SDK fallback for CI pipeline
angularsen 58b3050
Update global.json for flexible SDK selection
angularsen e7fd329
Add permissions for claude in pr/ci workflows
angularsen ee2f64b
Revert "Add permissions for claude in pr/ci workflows"
angularsen 8ff09ab
Merge branch 'master' into number-extensions-cs14
angularsen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
CodeGen/Generators/UnitsNetGen/NumberExtensionsCS14Generator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| using System; | ||
| using CodeGen.JsonTypes; | ||
|
|
||
| namespace CodeGen.Generators.UnitsNetGen | ||
| { | ||
| internal class NumberExtensionsCS14Generator(Quantity quantity) : GeneratorBase | ||
| { | ||
| private readonly Quantity _quantity = quantity ?? throw new ArgumentNullException(nameof(quantity)); | ||
| private readonly Unit[] _units = quantity.Units; | ||
| private readonly string _quantityName = quantity.Name; | ||
|
|
||
| public string Generate() | ||
| { | ||
| Writer.WL(GeneratedFileHeader); | ||
|
|
||
| Writer.WL( | ||
| $@" | ||
| using System; | ||
|
|
||
| #if NET7_0_OR_GREATER | ||
| using System.Numerics; | ||
| #endif | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace UnitsNet.NumberExtensions.NumberTo{_quantityName} | ||
| {{ | ||
| /// <summary> | ||
| /// A number to {_quantityName} Extensions | ||
| /// </summary>"); | ||
|
|
||
| Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity)); | ||
| Writer.WL(@$" | ||
| public static class NumberTo{_quantityName}Extensions | ||
| {{"); | ||
|
|
||
| Writer.WL(@" | ||
| #pragma warning disable CS1591 | ||
| extension<T>(T value) | ||
| #pragma warning restore CS1591 | ||
| where T : notnull | ||
| #if NET7_0_OR_GREATER | ||
| , INumber<T> | ||
| #else | ||
| , IConvertible | ||
| #endif | ||
| {"); | ||
|
|
||
|
|
||
| foreach (var unit in _units) | ||
| { | ||
| if (unit.SkipConversionGeneration) | ||
| continue; | ||
|
|
||
| Writer.WL(3, $@" | ||
| /// <inheritdoc cref=""{_quantityName}.From{unit.PluralName}(double)"" />"); | ||
|
|
||
| // Include obsolete text from the quantity per extension method, to make it visible when the class is not explicitly referenced in code. | ||
| Writer.WLIfText(3, GetObsoleteAttributeOrNull(unit.ObsoleteText ?? _quantity.ObsoleteText)); | ||
|
|
||
| Writer.WL(3, $@"public {_quantityName} {unit.PluralName} | ||
| #if NET7_0_OR_GREATER | ||
| => {_quantityName}.From{unit.PluralName}(double.CreateChecked(value)); | ||
| #else | ||
| => {_quantityName}.From{unit.PluralName}(value.ToDouble(null)); | ||
| #endif | ||
| "); | ||
| } | ||
|
|
||
| Writer.WL(2, @"} | ||
| } | ||
| }"); | ||
| return Writer.ToString(); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="GetObsoleteAttributeOrNull(string)"/> | ||
| private static string? GetObsoleteAttributeOrNull(Quantity quantity) => GetObsoleteAttributeOrNull(quantity.ObsoleteText); | ||
|
|
||
| private static string? GetObsoleteAttributeOrNull(string? obsoleteText) => | ||
| string.IsNullOrWhiteSpace(obsoleteText) ? null : $"[Obsolete(\"{obsoleteText}\")]"; | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
CodeGen/Generators/UnitsNetGen/NumberExtensionsCS14TestClassGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| using System; | ||
| using CodeGen.JsonTypes; | ||
|
|
||
| namespace CodeGen.Generators.UnitsNetGen | ||
| { | ||
| internal class NumberExtensionsCS14TestClassGenerator : GeneratorBase | ||
| { | ||
| private readonly Unit[] _units; | ||
| private readonly string _quantityName; | ||
|
|
||
| public NumberExtensionsCS14TestClassGenerator(Quantity quantity) | ||
| { | ||
| if (quantity is null) throw new ArgumentNullException(nameof(quantity)); | ||
|
|
||
| _units = quantity.Units; | ||
| _quantityName = quantity.Name; | ||
| } | ||
|
|
||
| public string Generate() | ||
| { | ||
| Writer.WL(GeneratedFileHeader); | ||
|
|
||
| Writer.WL( | ||
| $@" | ||
| using UnitsNet.NumberExtensions.NumberTo{_quantityName}; | ||
| using Xunit; | ||
|
|
||
| namespace UnitsNet.Tests | ||
| {{ | ||
| public class NumberTo{_quantityName}ExtensionsTests | ||
| {{"); | ||
|
|
||
| foreach (var unit in _units) | ||
| { | ||
| if (unit.SkipConversionGeneration) | ||
| continue; | ||
|
|
||
| Writer.WL(2, $@" | ||
| [Fact]"); | ||
|
|
||
| Writer.WL(2, $@"public void NumberTo{unit.PluralName}Test() => | ||
| Assert.Equal({_quantityName}.From{unit.PluralName}(2), 2.{unit.PluralName}); | ||
| "); | ||
| } | ||
|
|
||
| Writer.WL(1, @"} | ||
| }"); | ||
| return Writer.ToString(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...sions.CS14.Tests/GeneratedCode/NumberToAbsorbedDoseOfIonizingRadiationExtensionsTest.g.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CS14 projects are NOT included in the build-functions.psm1 file. This means:
Build Script Issues Found
The new CS14 projects are missing from the build pipeline:
This means:
Here's the additional feedback to add to the PR review:
🔴 Build Pipeline Issue
The new CS14 projects are missing from the build pipeline configuration:
File:
Build/build-functions.psm1There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could merge this earlier if we get the CI pipeline to build and run tests with .NET 10 SDK preview.
I pushed an attempt to fix this, let's see if the pipeline works now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude happily fixed these issues, so I pushed a fix for the build scripts also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, thanks. I didn't check the Package Id, description and the build script. Sorry about that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, that's what reviews are for 👍