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

Convert AgeInMonths to a source generated value #117

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

rabuckley
Copy link
Contributor

Closes #2

@rabuckley rabuckley requested a review from frankbuckley June 17, 2024 09:20
@rabuckley rabuckley self-assigned this Jun 17, 2024
src/DSE.Open.Values/AgeInMonths.cs Show resolved Hide resolved
src/DSE.Open.Values/AgeInMonths.cs Show resolved Hide resolved
Comment on lines 50 to 36
/// <summary>
/// The number of complete years represented by the value.
/// The total number of months represented by the value.
/// </summary>
public int Years => TotalMonths / 12;
public int TotalMonths { get; }

/// <summary>
/// The number of months represented by the value in addition to the <see cref="Years"/>.
/// The total number of years and months represented by the value.
/// </summary>
public int Months => TotalMonths % 12;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

From an API use perspective, it felt like Months should be returning TotalMonths, so to make it clearer that it is the remainder of dividing by Years I went with a single method returning a tuple.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For me, it makes less sense here. Years is not a part of months in the same way that seconds is a part of a TimeSpan.

That said, I don't much mind.

src/DSE.Open.Values/AgeInMonths.cs Show resolved Hide resolved
src/DSE.Open.Values/AgeInMonths.cs Outdated Show resolved Hide resolved
Comment on lines +129 to +130
if (!int.TryParse(s[ranges[0]], NumberStyles.None, NumberFormatInfo.InvariantInfo, out var years) ||
!int.TryParse(s[ranges[1]], NumberStyles.None, NumberFormatInfo.InvariantInfo, out var months))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've gone with the strictest parsing available. Do we want to allow whitespace?

Copy link
Member

Choose a reason for hiding this comment

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

Yes - I think some leniency can be helpful

Copy link

codecov bot commented Jun 17, 2024

Codecov Report

Attention: Patch coverage is 28.30189% with 190 lines in your changes missing coverage. Please review.

Project coverage is 48.4%. Comparing base (fc5d8bf) to head (69bfff3).
Report is 8 commits behind head on main.

Files Patch % Lines
....Values.Generators.ValueTypesGenerator/Months.g.cs 21.9% 62 Missing and 2 partials ⚠️
src/DSE.Open.Values/AgeInMonths.cs 43.0% 45 Missing and 8 partials ⚠️
...es.Generators.ValueTypesGenerator/AgeInMonths.g.cs 16.1% 48 Missing and 4 partials ⚠️
src/DSE.Open.Values/Months.cs 30.0% 7 Missing ⚠️
...n.Values.Generators/ValueTypesGenerator.Emitter.cs 0.0% 4 Missing ⚠️
...nerators.ValueTypesGenerator/AlphaNumericCode.g.cs 0.0% 2 Missing ⚠️
...Values.Generators.ValueTypesGenerator/Percent.g.cs 0.0% 2 Missing ⚠️
...n.Values.Generators.ValueTypesGenerator/Ratio.g.cs 0.0% 2 Missing ⚠️
...rs/Extensions/MethodDeclarationSyntaxExtensions.cs 0.0% 1 Missing ⚠️
...lues.Generators.ValueTypesGenerator/AlphaCode.g.cs 50.0% 1 Missing ⚠️
... and 2 more
Additional details and impacted files
@@           Coverage Diff           @@
##            main    #117     +/-   ##
=======================================
- Coverage   48.6%   48.4%   -0.2%     
=======================================
  Files        605     606      +1     
  Lines      32491   32658    +167     
  Branches    2717    2735     +18     
=======================================
+ Hits       15800   15830     +30     
- Misses     15650   15772    +122     
- Partials    1041    1056     +15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rabuckley
Copy link
Contributor Author

I didn't do IDivisibleValue because dividing months by months doesn't make sense to me. We would want to divide by int but the interface doesn't allow for that, and it is not built into the source generator.

Copy link
Member

@frankbuckley frankbuckley left a comment

Choose a reason for hiding this comment

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

Some things to think about.

src/DSE.Open.Values/AgeInMonths.cs Show resolved Hide resolved
Comment on lines 50 to 36
/// <summary>
/// The number of complete years represented by the value.
/// The total number of months represented by the value.
/// </summary>
public int Years => TotalMonths / 12;
public int TotalMonths { get; }

/// <summary>
/// The number of months represented by the value in addition to the <see cref="Years"/>.
/// The total number of years and months represented by the value.
/// </summary>
public int Months => TotalMonths % 12;
Copy link
Member

Choose a reason for hiding this comment

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

src/DSE.Open.Values/AgeInMonths.cs Outdated Show resolved Hide resolved
Comment on lines +129 to +130
if (!int.TryParse(s[ranges[0]], NumberStyles.None, NumberFormatInfo.InvariantInfo, out var years) ||
!int.TryParse(s[ranges[1]], NumberStyles.None, NumberFormatInfo.InvariantInfo, out var months))
Copy link
Member

Choose a reason for hiding this comment

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

Yes - I think some leniency can be helpful

src/DSE.Open.Values/DateMonthOnly.cs Show resolved Hide resolved
@rabuckley
Copy link
Contributor Author

We could decide AgeInMonths is just that a record of an age at a point in time (therefore constrained to sensible, non-negative values). And then we could have a Months unit of measure type that implements IInterval<TSelf, T>.

If we did this, then the difference between two AgeInMonths value could return a Months value.

What is the distinction between AgeInMonths and Months? There are no interesting methods here that would differ. Perhaps we turn this into Months and handle ages using Months too?

@frankbuckley
Copy link
Member

What is the distinction between AgeInMonths and Months? There are no interesting methods here that would differ. Perhaps we turn this into Months and handle ages using Months too?

The validation that the value is not an obviously incorrect age.

@frankbuckley
Copy link
Member

An analogy:

AlphaCode is to AsciiString is to byte array what AgeInMonths is to Months is to int.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert AgeInMonths to source generated value type
2 participants