Permalink
Browse files

Use C# 7.3 enum constraints

Fixes #2118
  • Loading branch information...
roji committed Aug 17, 2018
1 parent f9673b1 commit 2c5a680f48ee9872fb17da00baadcab93ed29e61
View
@@ -2,7 +2,7 @@ language: csharp
dist: trusty
sudo: false
mono: none
dotnet: 2.1.300
dotnet: 2.1.400
services:
- postgresql
@@ -1036,7 +1036,8 @@ public NpgsqlRawCopyStream BeginRawBinaryCopy(string copyCommand)
/// <typeparam name="TEnum">The .NET enum type to be mapped</typeparam>
[PublicAPI]
[Obsolete("Use NpgsqlConnection.TypeMapper.MapEnum() instead")]
public void MapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null) where TEnum : struct
public void MapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null)
where TEnum : Enum
=> TypeMapper.MapEnum<TEnum>(pgName, nameTranslator);
/// <summary>
@@ -1063,7 +1064,8 @@ public NpgsqlRawCopyStream BeginRawBinaryCopy(string copyCommand)
/// <typeparam name="TEnum">The .NET enum type to be mapped</typeparam>
[PublicAPI]
[Obsolete("Use NpgsqlConnection.GlobalTypeMapper.MapEnum() instead")]
public static void MapEnumGlobally<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null) where TEnum : struct
public static void MapEnumGlobally<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null)
where TEnum : Enum
=> NpgsqlConnection.GlobalTypeMapper.MapEnum<TEnum>(pgName, nameTranslator);
/// <summary>
@@ -1078,7 +1080,8 @@ public NpgsqlRawCopyStream BeginRawBinaryCopy(string copyCommand)
/// Defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>
/// </param>
[Obsolete("Use NpgsqlConnection.GlobalTypeMapper.UnmapEnum() instead")]
public static void UnmapEnumGlobally<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null) where TEnum : struct
public static void UnmapEnumGlobally<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null)
where TEnum : Enum
=> NpgsqlConnection.GlobalTypeMapper.UnmapEnum<TEnum>(pgName, nameTranslator);
#endregion
@@ -44,7 +44,7 @@ interface IEnumHandler
Type EnumType { get; }
}
class EnumHandler<TEnum> : NpgsqlSimpleTypeHandler<TEnum>, IEnumHandler where TEnum : struct
class EnumHandler<TEnum> : NpgsqlSimpleTypeHandler<TEnum>, IEnumHandler where TEnum : Enum
{
readonly Dictionary<TEnum, string> _enumToLabel;
readonly Dictionary<string, TEnum> _labelToEnum;
@@ -107,7 +107,7 @@ public interface IEnumTypeHandlerFactory
INpgsqlNameTranslator NameTranslator { get; }
}
class EnumTypeHandlerFactory<TEnum> : NpgsqlTypeHandlerFactory<TEnum>, IEnumTypeHandlerFactory where TEnum : struct
class EnumTypeHandlerFactory<TEnum> : NpgsqlTypeHandlerFactory<TEnum>, IEnumTypeHandlerFactory where TEnum : Enum
{
readonly Dictionary<TEnum, string> _enumToLabel = new Dictionary<TEnum, string>();
readonly Dictionary<string, TEnum> _labelToEnum = new Dictionary<string, TEnum>();
@@ -34,7 +34,7 @@ namespace Npgsql.TypeMapping
/// <summary>
/// A type mapper, managing how to read and write CLR values to PostgreSQL data types.
/// A type mapper exists for each connection, as well as a single global type mapper
/// (accessible via
/// (accessible via
/// </summary>
/// <remarks>
/// </remarks>
@@ -76,7 +76,7 @@ public interface INpgsqlTypeMapper
/// Defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>
/// </param>
/// <typeparam name="TEnum">The .NET enum type to be mapped</typeparam>
INpgsqlTypeMapper MapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null) where TEnum : struct;
INpgsqlTypeMapper MapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null) where TEnum : Enum;
/// <summary>
/// Removes an existing enum mapping.
@@ -89,7 +89,7 @@ public interface INpgsqlTypeMapper
/// A component which will be used to translate CLR names (e.g. SomeClass) into database names (e.g. some_class).
/// Defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>
/// </param>
bool UnmapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null) where TEnum : struct;
bool UnmapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null) where TEnum : Enum;
/// <summary>
/// Maps a CLR type to a PostgreSQL composite type.
@@ -56,10 +56,8 @@ public virtual INpgsqlTypeMapper AddMapping(NpgsqlTypeMapping mapping)
#region Enum mapping
public INpgsqlTypeMapper MapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null)
where TEnum : struct
where TEnum : Enum
{
if (!typeof(TEnum).GetTypeInfo().IsEnum)
throw new ArgumentException("An enum type must be provided");
if (pgName != null && pgName.Trim() == "")
throw new ArgumentException("pgName can't be empty", nameof(pgName));
@@ -76,10 +74,9 @@ public INpgsqlTypeMapper MapEnum<TEnum>(string pgName = null, INpgsqlNameTransla
}.Build());
}
public bool UnmapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null) where TEnum : struct
public bool UnmapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null)
where TEnum : Enum
{
if (!typeof(TEnum).GetTypeInfo().IsEnum)
throw new ArgumentException("An enum type must be provided");
if (pgName != null && pgName.Trim() == "")
throw new ArgumentException("pgName can't be empty", nameof(pgName));

0 comments on commit 2c5a680

Please sign in to comment.