-
Notifications
You must be signed in to change notification settings - Fork 186
Support DateOnly and TimeOnly #450
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,30 @@ public static bool IsDateTime(Type clrType) | |
| return Type.GetTypeCode(underlyingTypeOrSelf) == TypeCode.DateTime; | ||
| } | ||
|
|
||
| #if NET6_0 | ||
| /// <summary> | ||
| /// Determine if a type is a <see cref="DateOnly"/>. | ||
| /// </summary> | ||
| /// <param name="clrType">The type to test.</param> | ||
| /// <returns>True if the type is a DateOnly; false otherwise.</returns> | ||
| public static bool IsDateOnly(this Type clrType) | ||
| { | ||
| Type underlyingTypeOrSelf = GetUnderlyingTypeOrSelf(clrType); | ||
| return underlyingTypeOrSelf == typeof(DateOnly); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Determine if a type is a <see cref="TimeOnly"/>. | ||
| /// </summary> | ||
| /// <param name="clrType">The type to test.</param> | ||
| /// <returns>True if the type is a TimeOnly; false otherwise.</returns> | ||
| public static bool IsTimeOnly(this Type clrType) | ||
| { | ||
| Type underlyingTypeOrSelf = GetUnderlyingTypeOrSelf(clrType); | ||
| return underlyingTypeOrSelf == typeof(TimeOnly); | ||
| } | ||
| #endif | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to leave an extra blank line after this. #Resolved |
||
|
|
||
| /// <summary> | ||
| /// Determine if a type is a TimeSpan. | ||
| /// </summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,6 +111,13 @@ static DefaultODataTypeMapper() | |
| BuildTypeMapping<char>(EdmPrimitiveTypeKind.String, isStandard: false); | ||
| BuildTypeMapping<DateTime?>(EdmPrimitiveTypeKind.DateTimeOffset, isStandard: false); | ||
| BuildTypeMapping<DateTime>(EdmPrimitiveTypeKind.DateTimeOffset, isStandard: false); | ||
|
|
||
| #if NET6_0 | ||
| BuildTypeMapping<DateOnly?>(EdmPrimitiveTypeKind.Date, isStandard: false); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume there is no behavior difference in the order we add these mappings, but I'd still suggest adding the nullable versions prior to the non-nullable ones for consistency: all other types in this method are following that pattern. FAKE EDIT: Actually, scratch that: the order does seem to matter, as per the comment at the top of the method: // Do not change the order for the nullable or non-nullable. Put nullable ahead of non-nullable.
// By design: non-nullable will overwrite the item1.I'd even go as far as to suggest a small method to encapsulate this, something like: public void BuildStructTypeMapping<T>(EdmPrimitiveTypeKind edmPrimitiveTypeKind, bool isStandard)
where T : struct
{
BuildTypeMapping<T?>(edmPrimitiveTypeKind, isStandard);
BuildTypeMapping<T>(edmPrimitiveTypeKind, isStandard);
}Using this for all
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind sharing with me a contribution for this? I'd love to take and merge it for you.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For sure. I'll wait until you merge this and then refactor the whole block so as to avoid you having to rebase it. For now, just make sure the order is as per the comment and we should be good.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you are right for the order. I forget what i said. OMG |
||
| BuildTypeMapping<DateOnly>(EdmPrimitiveTypeKind.Date, isStandard: false); | ||
| BuildTypeMapping<TimeOnly?>(EdmPrimitiveTypeKind.TimeOfDay, isStandard: false); | ||
| BuildTypeMapping<TimeOnly>(EdmPrimitiveTypeKind.TimeOfDay, isStandard: false); | ||
| #endif | ||
| } | ||
| #endregion | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,6 +137,26 @@ public static object ConvertPrimitiveValue(object value, Type type, TimeZoneInfo | |
|
|
||
| throw new ValidationException(Error.Format(SRResources.PropertyMustBeBoolean)); | ||
| } | ||
| #if NET6_0 | ||
| else if (type == typeof(DateOnly)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice if this could be simplified using a switch expression instead of all the I understand it is a bit unrelated though, so up to you. #WontFix
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion, i'd like to leave for other PR. |
||
| { | ||
| if (value is Date dt) | ||
| { | ||
| return new DateOnly(dt.Year, dt.Month, dt.Day); | ||
| } | ||
|
|
||
| throw new ValidationException(Error.Format(SRResources.PropertyMustBeDateTimeOffsetOrDate)); | ||
| } | ||
| else if (type == typeof(TimeOnly)) | ||
| { | ||
| if (value is TimeOfDay tod) | ||
| { | ||
| return new TimeOnly(tod.Hours, tod.Minutes, tod.Seconds, (int)tod.Milliseconds); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, It's ODL problem and now i have to cast it. |
||
| } | ||
|
|
||
| throw new ValidationException(Error.Format(SRResources.PropertyMustBeTimeOfDay)); | ||
| } | ||
| #endif | ||
| else | ||
| { | ||
| if (TypeHelper.TryGetInstance(type, value, out var result)) | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.
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.
Have you considered moving NET6-specific members into a partial file? That might make it easier to manage in the future. Something like
TypeHelper.Net6.csfor example. #WontFixThere 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.
Since there's only two new methods added, i'd like to create a new file until we have more .NET 6 specify methods added. Thanks for your suggestion.