-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
src/DSE.Open.Values/AgeInMonths.cs
Outdated
/// <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; |
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.
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.
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.
I think I was aiming to follow the pattern here: https://learn.microsoft.com/en-us/dotnet/api/system.timespan?view=net-8.0#properties and https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/TimeSpan.cs,170
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.
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.
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)) |
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.
I've gone with the strictest parsing available. Do we want to allow whitespace?
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.
Yes - I think some leniency can be helpful
I didn't do |
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.
Some things to think about.
src/DSE.Open.Values/AgeInMonths.cs
Outdated
/// <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; |
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.
I think I was aiming to follow the pattern here: https://learn.microsoft.com/en-us/dotnet/api/system.timespan?view=net-8.0#properties and https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/TimeSpan.cs,170
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)) |
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.
Yes - I think some leniency can be helpful
What is the distinction between |
The validation that the value is not an obviously incorrect age. |
An analogy: AlphaCode is to AsciiString is to byte array what AgeInMonths is to Months is to int. |
Closes #2