Skip to content

Commit

Permalink
Add some enum generic constraints (#31515)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored Aug 20, 2023
1 parent 221ea0b commit 4a976b7
Show file tree
Hide file tree
Showing 10 changed files with 7 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/EFCore/Storage/Json/JsonSignedEnumReaderWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.Json;
/// Reads and writes JSON for <see langword="enum" /> values backed by a signed integer.
/// </summary>
public sealed class JsonSignedEnumReaderWriter<TEnum> : JsonValueReaderWriter<TEnum>
where TEnum : struct, Enum
{
/// <summary>
/// The singleton instance of this stateless reader/writer.
Expand Down
1 change: 1 addition & 0 deletions src/EFCore/Storage/Json/JsonUnsignedEnumReaderWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.Json;
/// Reads and writes JSON for <see langword="enum" /> values backed by an unsigned integer.
/// </summary>
public sealed class JsonUnsignedEnumReaderWriter<TEnum> : JsonValueReaderWriter<TEnum>
where TEnum : struct, Enum
{
/// <summary>
/// The singleton instance of this stateless reader/writer.
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/Storage/Json/JsonWarningEnumReaderWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.Json;
/// happens, a warning is generated.
/// </summary>
public sealed class JsonWarningEnumReaderWriter<TEnum> : JsonValueReaderWriter<TEnum>
where TEnum : struct
where TEnum : struct, Enum
{
/// <summary>
/// The singleton instance of this stateless reader/writer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion;
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples.
/// </remarks>
public class EnumToNumberConverter<TEnum, TNumber> : ValueConverter<TEnum, TNumber>
where TEnum : struct
where TEnum : struct, Enum
where TNumber : struct
{
// ReSharper disable once StaticMemberInGenericType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion;
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples.
/// </remarks>
public class EnumToStringConverter<TEnum> : StringEnumConverter<TEnum, string, TEnum>
where TEnum : struct
where TEnum : struct, Enum
{
/// <summary>
/// Creates a new instance of this converter. This converter does not preserve order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class StringEnumConverter<TModel, TProvider, TEnum> : ValueConverter<TModel, TProvider>
where TEnum : struct
where TEnum : struct, Enum
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion;
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples.
/// </remarks>
public class StringToEnumConverter<TEnum> : StringEnumConverter<string, TEnum, TEnum>
where TEnum : struct
where TEnum : struct, Enum
{
/// <summary>
/// Creates a new instance of this converter. This converter does not preserve order.
Expand Down
20 changes: 0 additions & 20 deletions test/EFCore.Tests/Storage/EnumToNumberConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,26 +265,6 @@ public void Can_convert_ulongs_to_enums()
Assert.Equal(default, converter(0));
}

[ConditionalFact]
public void Enum_to_integer_converter_throws_for_bad_types()
{
Assert.Equal(
CoreStrings.ConverterBadType(
typeof(EnumToNumberConverter<Guid, int>).ShortDisplayName(),
"Guid",
"enum types"),
Assert.Throws<InvalidOperationException>(
() => new EnumToNumberConverter<Guid, int>()).Message);

Assert.Equal(
CoreStrings.ConverterBadType(
typeof(EnumToNumberConverter<Beatles, Guid>).ShortDisplayName(),
"Guid",
"'int', 'long', 'short', 'byte', 'uint', 'ulong', 'ushort', 'sbyte', 'double', 'float', 'decimal'"),
Assert.Throws<InvalidOperationException>(
() => new EnumToNumberConverter<Beatles, Guid>()).Message);
}

private enum Beatles
{
John = 7,
Expand Down
10 changes: 0 additions & 10 deletions test/EFCore.Tests/Storage/EnumToStringConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,6 @@ public void Can_convert_strings_to_enums_object()
Assert.Null(converter(null));
}

[ConditionalFact]
public void Enum_to_string_converter_throws_for_bad_types()
=> Assert.Equal(
CoreStrings.ConverterBadType(
typeof(StringEnumConverter<Guid, string, Guid>).ShortDisplayName(),
"Guid",
"enum types"),
Assert.Throws<InvalidOperationException>(
() => new EnumToStringConverter<Guid>()).Message);

private enum Beatles
{
John = 7,
Expand Down
10 changes: 0 additions & 10 deletions test/EFCore.Tests/Storage/StringToEnumConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,6 @@ public void Can_convert_enums_to_strings_object()
Assert.Null(converter(null));
}

[ConditionalFact]
public void String_to_enum_converter_throws_for_bad_types()
=> Assert.Equal(
CoreStrings.ConverterBadType(
typeof(StringEnumConverter<string, Guid, Guid>).ShortDisplayName(),
"Guid",
"enum types"),
Assert.Throws<InvalidOperationException>(
() => new StringToEnumConverter<Guid>()).Message);

private enum Beatles
{
John = 7,
Expand Down

0 comments on commit 4a976b7

Please sign in to comment.