Skip to content

Commit

Permalink
Add support for .Net 6 BCL DateOnly and TimeOnly OData#22
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrdxr committed Oct 28, 2021
1 parent b2d07a6 commit ca676f2
Show file tree
Hide file tree
Showing 20 changed files with 3,228 additions and 141 deletions.
4 changes: 3 additions & 1 deletion build/azure-pipelines-rolling.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pr:
- master

pool:
vmImage: 'windows-latest'
# vmImage: 'windows-latest'
# Temporary until 6.0 becomes the latest
vmImage: 'windows-2022
variables:
BuildPlatform: 'Any CPU'
Expand Down
15 changes: 11 additions & 4 deletions src/Microsoft.OData.ModelBuilder/Commons/EdmLibHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,17 @@ internal static class EdmLibHelpers
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(Date?), GetPrimitiveType(EdmPrimitiveTypeKind.Date)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(TimeOfDay), GetPrimitiveType(EdmPrimitiveTypeKind.TimeOfDay)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(TimeOfDay?), GetPrimitiveType(EdmPrimitiveTypeKind.TimeOfDay)),

// Keep the Binary and XElement in the end, since there are not the default mappings for Edm.Binary and Edm.String.
#if NET6_0_OR_GREATER
// Keep DateOnly and TimeOnly after Date and TimeOfDay, since they are not the default mappings for Edm.Date and Edm.TimeOfDay.
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(DateOnly), GetPrimitiveType(EdmPrimitiveTypeKind.Date)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(DateOnly?), GetPrimitiveType(EdmPrimitiveTypeKind.Date)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(TimeOnly), GetPrimitiveType(EdmPrimitiveTypeKind.TimeOfDay)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(TimeOnly?), GetPrimitiveType(EdmPrimitiveTypeKind.TimeOfDay)),
#endif

// Keep the Binary and XElement in the end, since they are not the default mappings for Edm.Binary and Edm.String.
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(XElement), GetPrimitiveType(EdmPrimitiveTypeKind.String)),
// new KeyValuePair<Type, IEdmPrimitiveType>(typeof(Binary), GetPrimitiveType(EdmPrimitiveTypeKind.Binary)),
// new KeyValuePair<Type, IEdmPrimitiveType>(typeof(Binary), GetPrimitiveType(EdmPrimitiveTypeKind.Binary)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(ushort), GetPrimitiveType(EdmPrimitiveTypeKind.Int32)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(ushort?), GetPrimitiveType(EdmPrimitiveTypeKind.Int32)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(uint), GetPrimitiveType(EdmPrimitiveTypeKind.Int64)),
Expand Down Expand Up @@ -126,7 +133,7 @@ public static Type GetClrType(this IEdmModel edmModel, IEdmTypeReference edmType
return edmModel.GetClrType(edmTypeReference, DefaultAssemblyResolver.Default);
}

public static Type GetClrType(this IEdmModel edmModel,IEdmTypeReference edmTypeReference, IAssemblyResolver assembliesResolver)
public static Type GetClrType(this IEdmModel edmModel, IEdmTypeReference edmTypeReference, IAssemblyResolver assembliesResolver)
{
if (edmTypeReference == null)
{
Expand Down
24 changes: 24 additions & 0 deletions src/Microsoft.OData.ModelBuilder/Commons/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,30 @@ public static bool IsTimeSpan(Type clrType)
return underlyingTypeOrSelf == typeof(TimeSpan);
}

#if NET6_0_OR_GREATER
/// <summary>
/// Determine if a type is a 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(Type clrType)
{
Type underlyingTypeOrSelf = GetUnderlyingTypeOrSelf(clrType);
return underlyingTypeOrSelf == typeof(DateOnly);
}

/// <summary>
/// Determine if a type is a 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(Type clrType)
{
Type underlyingTypeOrSelf = GetUnderlyingTypeOrSelf(clrType);
return underlyingTypeOrSelf == typeof(TimeOnly);
}
#endif

/// <summary>
/// Determine if a type is an enumeration.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<RootNamespace>Microsoft.OData.ModelBuilder</RootNamespace>
<DocumentationFile>$(OutputPath)$(AssemblyName).xml</DocumentationFile>

Expand All @@ -22,9 +22,9 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.OData.Edm" Version="7.9.0" />
<PackageReference Include="Microsoft.Spatial" Version="7.9.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.6.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.9.3" />
<PackageReference Include="Microsoft.Spatial" Version="7.9.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

Expand Down
40 changes: 40 additions & 0 deletions src/Microsoft.OData.ModelBuilder/Microsoft.OData.ModelBuilder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,20 @@
<param name="clrType">The type to test.</param>
<returns>True if the type is a TimeSpan; false otherwise.</returns>
</member>
<member name="M:Microsoft.OData.ModelBuilder.TypeHelper.IsDateOnly(System.Type)">
<summary>
Determine if a type is a DateOnly.
</summary>
<param name="clrType">The type to test.</param>
<returns>True if the type is a DateOnly; false otherwise.</returns>
</member>
<member name="M:Microsoft.OData.ModelBuilder.TypeHelper.IsTimeOnly(System.Type)">
<summary>
Determine if a type is a TimeOnly
</summary>
<param name="clrType">The type to test.</param>
<returns>True if the type is a TimeOnly; false otherwise.</returns>
</member>
<member name="M:Microsoft.OData.ModelBuilder.TypeHelper.IsEnum(System.Type)">
<summary>
Determine if a type is an enumeration.
Expand Down Expand Up @@ -4279,6 +4293,11 @@
Looks up a localized string similar to The property &apos;{0}&apos; on type &apos;{1}&apos; must be a Complex property..
</summary>
</member>
<member name="P:Microsoft.OData.ModelBuilder.SRResources.MustBeDateTimeOrDateOnlyProperty">
<summary>
Looks up a localized string similar to The property &apos;{0}&apos; on type &apos;{1}&apos; must be a System.DateTime or System.DateOnly property..
</summary>
</member>
<member name="P:Microsoft.OData.ModelBuilder.SRResources.MustBeDateTimeProperty">
<summary>
Looks up a localized string similar to The property &apos;{0}&apos; on type &apos;{1}&apos; must be a System.DateTime property..
Expand All @@ -4304,6 +4323,11 @@
Looks up a localized string similar to The type &apos;{0}&apos; must be a primitive type..
</summary>
</member>
<member name="P:Microsoft.OData.ModelBuilder.SRResources.MustBeTimeSpanOrTimeOnlyProperty">
<summary>
Looks up a localized string similar to The property &apos;{0}&apos; on type &apos;{1}&apos; must be a System.TimeSpan or System.TimeOnly property..
</summary>
</member>
<member name="P:Microsoft.OData.ModelBuilder.SRResources.MustBeTimeSpanProperty">
<summary>
Looks up a localized string similar to The property &apos;{0}&apos; on type &apos;{1}&apos; must be a System.TimeSpan property..
Expand Down Expand Up @@ -5333,6 +5357,22 @@
For example, in C# <c>t => t.MyProperty</c> and in Visual Basic .NET <c>Function(t) t.MyProperty</c>.</param>
<returns>A configuration object that can be used to further configure the property.</returns>
</member>
<member name="M:Microsoft.OData.ModelBuilder.StructuralTypeConfiguration`1.Property(System.Linq.Expressions.Expression{System.Func{`0,System.Nullable{System.TimeOnly}}})">
<summary>
Adds an time-only primitive property to the EDM type.
</summary>
<param name="propertyExpression">A lambda expression representing the navigation property for the relationship.
For example, in C# <c>t => t.MyProperty</c> and in Visual Basic .NET <c>Function(t) t.MyProperty</c>.</param>
<returns>A configuration object that can be used to further configure the property.</returns>
</member>
<member name="M:Microsoft.OData.ModelBuilder.StructuralTypeConfiguration`1.Property(System.Linq.Expressions.Expression{System.Func{`0,System.TimeOnly}})">
<summary>
Adds an time-only primitive property to the EDM type.
</summary>
<param name="propertyExpression">A lambda expression representing the navigation property for the relationship.
For example, in C# <c>t => t.MyProperty</c> and in Visual Basic .NET <c>Function(t) t.MyProperty</c>.</param>
<returns>A configuration object that can be used to further configure the property.</returns>
</member>
<member name="M:Microsoft.OData.ModelBuilder.StructuralTypeConfiguration`1.Property(System.Linq.Expressions.Expression{System.Func{`0,System.Nullable{System.TimeSpan}}})">
<summary>
Adds an duration primitive property to the EDM type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ public static PrimitivePropertyConfiguration AsDate(this PrimitivePropertyConfig

if (!TypeHelper.IsDateTime(property.RelatedClrType))
{
#if NET6_0_OR_GREATER
if (!TypeHelper.IsDateOnly(property.RelatedClrType))
{
throw Error.Argument("property", SRResources.MustBeDateTimeOrDateOnlyProperty, property.PropertyInfo.Name,
property.DeclaringType.FullName);
}
#else
throw Error.Argument("property", SRResources.MustBeDateTimeProperty, property.PropertyInfo.Name,
property.DeclaringType.FullName);
#endif
}

property.TargetEdmTypeKind = EdmPrimitiveTypeKind.Date;
Expand All @@ -48,8 +56,16 @@ public static PrimitivePropertyConfiguration AsTimeOfDay(this PrimitivePropertyC

if (!TypeHelper.IsTimeSpan(property.RelatedClrType))
{
#if NET6_0_OR_GREATER
if (!TypeHelper.IsTimeOnly(property.RelatedClrType))
{
throw Error.Argument("property", SRResources.MustBeTimeSpanOrTimeOnlyProperty, property.PropertyInfo.Name,
property.DeclaringType.FullName);
}
#else
throw Error.Argument("property", SRResources.MustBeTimeSpanProperty, property.PropertyInfo.Name,
property.DeclaringType.FullName);
#endif
}

property.TargetEdmTypeKind = EdmPrimitiveTypeKind.TimeOfDay;
Expand Down
20 changes: 19 additions & 1 deletion src/Microsoft.OData.ModelBuilder/SRResources.Designer.cs

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

Loading

0 comments on commit ca676f2

Please sign in to comment.