-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
684 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
50 changes: 50 additions & 0 deletions
50
src/EFCore/Storage/ValueConversion/DateOnlyToStringConverter.cs
This file contains 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,50 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
/// <summary> | ||
/// Converts <see cref="DateOnly" /> to and from strings. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
public class DateOnlyToStringConverter : StringDateOnlyConverter<DateOnly, string> | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of this converter. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
public DateOnlyToStringConverter() | ||
: this(null) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new instance of this converter. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
/// <param name="mappingHints"> | ||
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate | ||
/// facets for the converted data. | ||
/// </param> | ||
public DateOnlyToStringConverter(ConverterMappingHints? mappingHints) | ||
: base( | ||
ToString(), | ||
ToDateOnly(), | ||
DefaultHints.With(mappingHints)) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// A <see cref="ValueConverterInfo" /> for the default use of this converter. | ||
/// </summary> | ||
public static ValueConverterInfo DefaultInfo { get; } | ||
= new(typeof(DateOnly), typeof(string), i => new DateOnlyToStringConverter(i.MappingHints), DefaultHints); | ||
} |
56 changes: 56 additions & 0 deletions
56
src/EFCore/Storage/ValueConversion/Internal/StringDateOnlyConverter.cs
This file contains 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,56 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public class StringDateOnlyConverter<TModel, TProvider> : ValueConverter<TModel, TProvider> | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
// ReSharper disable once StaticMemberInGenericType | ||
protected static readonly ConverterMappingHints DefaultHints = new(size: 10); | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public StringDateOnlyConverter( | ||
Expression<Func<TModel, TProvider>> convertToProviderExpression, | ||
Expression<Func<TProvider, TModel>> convertFromProviderExpression, | ||
ConverterMappingHints? mappingHints = null) | ||
: base(convertToProviderExpression, convertFromProviderExpression, mappingHints) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
protected static new Expression<Func<DateOnly, string>> ToString() | ||
=> v => v.ToString(@"yyyy\-MM\-dd"); | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
protected static Expression<Func<string, DateOnly>> ToDateOnly() | ||
=> v => DateOnly.Parse(v, CultureInfo.InvariantCulture, DateTimeStyles.None); | ||
} |
58 changes: 58 additions & 0 deletions
58
src/EFCore/Storage/ValueConversion/Internal/StringTimeOnlyConverter.cs
This file contains 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,58 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public class StringTimeOnlyConverter<TModel, TProvider> : ValueConverter<TModel, TProvider> | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
// ReSharper disable once StaticMemberInGenericType | ||
protected static readonly ConverterMappingHints DefaultHints = new(size: 48); | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public StringTimeOnlyConverter( | ||
Expression<Func<TModel, TProvider>> convertToProviderExpression, | ||
Expression<Func<TProvider, TModel>> convertFromProviderExpression, | ||
ConverterMappingHints? mappingHints = null) | ||
: base(convertToProviderExpression, convertFromProviderExpression, mappingHints) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
protected static new Expression<Func<TimeOnly, string>> ToString() | ||
=> v => v.Ticks % 10000000 == 0 | ||
? string.Format(CultureInfo.InvariantCulture, @"{0:HH\:mm\:ss}", v) | ||
: v.ToString("o"); | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
protected static Expression<Func<string, TimeOnly>> ToTimeOnly() | ||
=> v => TimeOnly.Parse(v, CultureInfo.InvariantCulture, DateTimeStyles.None); | ||
} |
50 changes: 50 additions & 0 deletions
50
src/EFCore/Storage/ValueConversion/StringToDateOnlyConverter.cs
This file contains 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,50 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
/// <summary> | ||
/// Converts strings to and from <see cref="DateOnly" /> values. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
public class StringToDateOnlyConverter : StringDateOnlyConverter<string, DateOnly> | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of this converter. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
public StringToDateOnlyConverter() | ||
: this(null) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new instance of this converter. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
/// <param name="mappingHints"> | ||
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate | ||
/// facets for the converted data. | ||
/// </param> | ||
public StringToDateOnlyConverter(ConverterMappingHints? mappingHints) | ||
: base( | ||
ToDateOnly(), | ||
ToString(), | ||
DefaultHints.With(mappingHints)) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// A <see cref="ValueConverterInfo" /> for the default use of this converter. | ||
/// </summary> | ||
public static ValueConverterInfo DefaultInfo { get; } | ||
= new(typeof(string), typeof(DateOnly), i => new StringToDateTimeConverter(i.MappingHints), DefaultHints); | ||
} |
50 changes: 50 additions & 0 deletions
50
src/EFCore/Storage/ValueConversion/StringToTimeOnlyConverter.cs
This file contains 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,50 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
/// <summary> | ||
/// Converts strings to and from <see cref="TimeOnly" /> values. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
public class StringToTimeOnlyConverter : StringTimeOnlyConverter<string, TimeOnly> | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of this converter. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
public StringToTimeOnlyConverter() | ||
: this(null) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new instance of this converter. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
/// <param name="mappingHints"> | ||
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate | ||
/// facets for the converted data. | ||
/// </param> | ||
public StringToTimeOnlyConverter(ConverterMappingHints? mappingHints) | ||
: base( | ||
ToTimeOnly(), | ||
ToString(), | ||
DefaultHints.With(mappingHints)) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// A <see cref="ValueConverterInfo" /> for the default use of this converter. | ||
/// </summary> | ||
public static ValueConverterInfo DefaultInfo { get; } | ||
= new(typeof(string), typeof(TimeOnly), i => new StringToTimeOnlyConverter(i.MappingHints), DefaultHints); | ||
} |
50 changes: 50 additions & 0 deletions
50
src/EFCore/Storage/ValueConversion/TimeOnlyToStringConverter.cs
This file contains 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,50 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
/// <summary> | ||
/// Converts <see cref="TimeOnly" /> to and from strings. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
public class TimeOnlyToStringConverter : StringTimeOnlyConverter<TimeOnly, string> | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of this converter. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
public TimeOnlyToStringConverter() | ||
: this(null) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new instance of this converter. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
/// <param name="mappingHints"> | ||
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate | ||
/// facets for the converted data. | ||
/// </param> | ||
public TimeOnlyToStringConverter(ConverterMappingHints? mappingHints) | ||
: base( | ||
ToString(), | ||
ToTimeOnly(), | ||
DefaultHints.With(mappingHints)) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// A <see cref="ValueConverterInfo" /> for the default use of this converter. | ||
/// </summary> | ||
public static ValueConverterInfo DefaultInfo { get; } | ||
= new(typeof(TimeOnly), typeof(string), i => new TimeOnlyToStringConverter(i.MappingHints), DefaultHints); | ||
} |
45 changes: 45 additions & 0 deletions
45
src/EFCore/Storage/ValueConversion/TimeOnlyToTicksConverter.cs
This file contains 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,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
/// <summary> | ||
/// Converts <see cref="TimeOnly" /> to and <see cref="TimeOnly.Ticks" />. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
public class TimeOnlyToTicksConverter : ValueConverter<TimeOnly, long> | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of this converter. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
public TimeOnlyToTicksConverter() | ||
: this(null) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new instance of this converter. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples. | ||
/// </remarks> | ||
/// <param name="mappingHints"> | ||
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate | ||
/// facets for the converted data. | ||
/// </param> | ||
public TimeOnlyToTicksConverter(ConverterMappingHints? mappingHints) | ||
: base(v => v.Ticks, v => new TimeOnly(v), mappingHints) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// A <see cref="ValueConverterInfo" /> for the default use of this converter. | ||
/// </summary> | ||
public static ValueConverterInfo DefaultInfo { get; } | ||
= new(typeof(TimeOnly), typeof(long), i => new TimeOnlyToTicksConverter(i.MappingHints)); | ||
} |
Oops, something went wrong.