Skip to content

Commit 2c43e15

Browse files
Expose DefaultNameTranslator on INpgsqlTypeMapper (#2137)
1 parent ac89e36 commit 2c43e15

4 files changed

Lines changed: 61 additions & 35 deletions

File tree

src/Npgsql/TypeMapping/ConnectorTypeMapper.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#region License
2+
23
// The PostgreSQL License
34
//
45
// Copyright (C) 2018 The Npgsql Development Team
@@ -19,17 +20,15 @@
1920
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
2021
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
2122
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23+
2224
#endregion
2325

2426
using System;
2527
using System.Collections;
2628
using System.Collections.Generic;
2729
using System.Data;
28-
using System.Diagnostics;
2930
using System.Linq;
3031
using System.Reflection;
31-
using System.Text;
32-
using System.Threading.Tasks;
3332
using JetBrains.Annotations;
3433
using Npgsql.Logging;
3534
using Npgsql.PostgresTypes;
@@ -80,13 +79,12 @@ class ConnectorTypeMapper : TypeMapperBase
8079

8180
#region Construction
8281

83-
internal ConnectorTypeMapper(NpgsqlConnector connector)
82+
internal ConnectorTypeMapper(NpgsqlConnector connector): base(GlobalTypeMapper.Instance.DefaultNameTranslator)
8483
{
8584
_connector = connector;
8685
UnrecognizedTypeHandler = new UnknownTypeHandler(_connector.Connection);
8786
ClearBindings();
8887
ResetMappings();
89-
DefaultNameTranslator = GlobalTypeMapper.Instance.DefaultNameTranslator;
9088
}
9189

9290
#endregion Constructors

src/Npgsql/TypeMapping/GlobalTypeMapper.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#region License
2+
23
// The PostgreSQL License
34
//
45
// Copyright (C) 2018 The Npgsql Development Team
@@ -19,16 +20,15 @@
1920
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
2021
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
2122
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23+
2224
#endregion
2325

2426
using System;
2527
using System.Collections.Generic;
2628
using System.Data;
2729
using System.Linq;
2830
using System.Reflection;
29-
using System.Text;
3031
using System.Threading;
31-
using System.Threading.Tasks;
3232
using Npgsql.NameTranslation;
3333
using Npgsql.TypeHandling;
3434
using NpgsqlTypes;
@@ -57,11 +57,8 @@ static GlobalTypeMapper()
5757
Instance = instance;
5858
}
5959

60-
internal GlobalTypeMapper()
61-
{
62-
Mappings = new Dictionary<string, NpgsqlTypeMapping>();
63-
DefaultNameTranslator = new NpgsqlSnakeCaseNameTranslator();
64-
}
60+
internal GlobalTypeMapper() : base(new NpgsqlSnakeCaseNameTranslator())
61+
=> Mappings = new Dictionary<string, NpgsqlTypeMapping>();
6562

6663
#region Mapping management
6764

src/Npgsql/TypeMapping/INpgsqlTypeMapper.cs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#region License
2+
23
// The PostgreSQL License
34
//
45
// Copyright (C) 2018 The Npgsql Development Team
@@ -19,42 +20,53 @@
1920
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
2021
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
2122
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23+
2224
#endregion
2325

2426
using System;
2527
using System.Collections.Generic;
26-
using System.Linq;
27-
using System.Text;
28-
using System.Threading.Tasks;
28+
using JetBrains.Annotations;
2929
using Npgsql.NameTranslation;
3030
using NpgsqlTypes;
3131

32+
// ReSharper disable UnusedMember.Global
3233
namespace Npgsql.TypeMapping
3334
{
3435
/// <summary>
3536
/// A type mapper, managing how to read and write CLR values to PostgreSQL data types.
3637
/// A type mapper exists for each connection, as well as a single global type mapper
37-
/// (accessible via
38+
/// (accessible via <see cref="P:NpgsqlConnection.GlobalTypeMapper"/>).
3839
/// </summary>
3940
/// <remarks>
4041
/// </remarks>
42+
[PublicAPI]
4143
public interface INpgsqlTypeMapper
4244
{
4345
/// <summary>
44-
/// Adds a new type mapping to this mapper, overwriting any existing mapping in the process.
46+
/// The default name translator to convert CLR type names and member names.
4547
/// </summary>
46-
INpgsqlTypeMapper AddMapping(NpgsqlTypeMapping mapping);
48+
[NotNull]
49+
INpgsqlNameTranslator DefaultNameTranslator { get; }
4750

4851
/// <summary>
49-
/// Removes an existing mapping from this mapper. Attempts to read or write this type
50-
/// after removal will result in an exception.
52+
/// Enumerates all mappings currently set up on this type mapper.
5153
/// </summary>
52-
bool RemoveMapping(string pgTypeName);
54+
[NotNull]
55+
[ItemNotNull]
56+
IEnumerable<NpgsqlTypeMapping> Mappings { get; }
5357

5458
/// <summary>
55-
/// Enumerates all mappings currently set up on this type mapper.
59+
/// Adds a new type mapping to this mapper, overwriting any existing mapping in the process.
5660
/// </summary>
57-
IEnumerable<NpgsqlTypeMapping> Mappings { get; }
61+
[NotNull]
62+
INpgsqlTypeMapper AddMapping([NotNull] NpgsqlTypeMapping mapping);
63+
64+
/// <summary>
65+
/// Removes an existing mapping from this mapper. Attempts to read or write this type
66+
/// after removal will result in an exception.
67+
/// </summary>
68+
/// <param name="pgTypeName">A PostgreSQL type name for the type in the database.</param>
69+
bool RemoveMapping([NotNull] string pgTypeName);
5870

5971
/// <summary>
6072
/// Maps a CLR enum to a PostgreSQL enum type.
@@ -76,7 +88,10 @@ public interface INpgsqlTypeMapper
7688
/// Defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>
7789
/// </param>
7890
/// <typeparam name="TEnum">The .NET enum type to be mapped</typeparam>
79-
INpgsqlTypeMapper MapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null)
91+
[NotNull]
92+
INpgsqlTypeMapper MapEnum<TEnum>(
93+
[CanBeNull] string pgName = null,
94+
[CanBeNull] INpgsqlNameTranslator nameTranslator = null)
8095
where TEnum : struct, Enum;
8196

8297
/// <summary>
@@ -90,7 +105,9 @@ INpgsqlTypeMapper MapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nam
90105
/// A component which will be used to translate CLR names (e.g. SomeClass) into database names (e.g. some_class).
91106
/// Defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>
92107
/// </param>
93-
bool UnmapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator = null)
108+
bool UnmapEnum<TEnum>(
109+
[CanBeNull] string pgName = null,
110+
[CanBeNull] INpgsqlNameTranslator nameTranslator = null)
94111
where TEnum : struct, Enum;
95112

96113
/// <summary>
@@ -113,7 +130,10 @@ bool UnmapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator
113130
/// Defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>
114131
/// </param>
115132
/// <typeparam name="T">The .NET type to be mapped</typeparam>
116-
INpgsqlTypeMapper MapComposite<T>(string pgName = null, INpgsqlNameTranslator nameTranslator = null) where T : new();
133+
[NotNull]
134+
INpgsqlTypeMapper MapComposite<T>(
135+
[CanBeNull] string pgName = null,
136+
[CanBeNull] INpgsqlNameTranslator nameTranslator = null) where T : new();
117137

118138
/// <summary>
119139
/// Removes an existing enum mapping.
@@ -126,7 +146,9 @@ bool UnmapEnum<TEnum>(string pgName = null, INpgsqlNameTranslator nameTranslator
126146
/// A component which will be used to translate CLR names (e.g. SomeClass) into database names (e.g. some_class).
127147
/// Defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>
128148
/// </param>
129-
bool UnmapComposite<T>(string pgName = null, INpgsqlNameTranslator nameTranslator = null) where T : new();
149+
bool UnmapComposite<T>(
150+
[CanBeNull] string pgName = null,
151+
[CanBeNull] INpgsqlNameTranslator nameTranslator = null) where T : new();
130152

131153
/// <summary>
132154
/// Resets all mapping changes performed on this type mapper and reverts it to its original, starting state.

src/Npgsql/TypeMapping/TypeMapperBase.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#region License
2+
23
// The PostgreSQL License
34
//
45
// Copyright (C) 2018 The Npgsql Development Team
@@ -19,11 +20,13 @@
1920
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
2021
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
2122
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23+
2224
#endregion
2325

2426
using System;
2527
using System.Collections.Generic;
2628
using System.Reflection;
29+
using JetBrains.Annotations;
2730
using Npgsql.TypeHandlers;
2831
using NpgsqlTypes;
2932

@@ -33,7 +36,15 @@ abstract class TypeMapperBase : INpgsqlTypeMapper
3336
{
3437
internal Dictionary<string, NpgsqlTypeMapping> Mappings { get; set; }
3538

36-
public INpgsqlNameTranslator DefaultNameTranslator { get; set; }
39+
public INpgsqlNameTranslator DefaultNameTranslator { get; }
40+
41+
protected TypeMapperBase([NotNull] INpgsqlNameTranslator defaultNameTranslator)
42+
{
43+
if (defaultNameTranslator == null)
44+
throw new ArgumentNullException(nameof(defaultNameTranslator));
45+
46+
DefaultNameTranslator = defaultNameTranslator;
47+
}
3748

3849
#region Mapping management
3950

@@ -112,7 +123,7 @@ public INpgsqlTypeMapper MapComposite<T>(string pgName = null, INpgsqlNameTransl
112123
}
113124

114125
public bool UnmapComposite<T>(string pgName = null, INpgsqlNameTranslator nameTranslator = null)
115-
where T: new()
126+
where T : new()
116127
{
117128
if (pgName != null && pgName.Trim() == "")
118129
throw new ArgumentException("pgName can't be empty", nameof(pgName));
@@ -129,13 +140,11 @@ public bool UnmapComposite<T>(string pgName = null, INpgsqlNameTranslator nameTr
129140

130141
#region Misc
131142

143+
// TODO: why does ReSharper think `GetCustomAttribute<T>` is non-nullable?
144+
// ReSharper disable once ConstantConditionalAccessQualifier ConstantNullCoalescingCondition
132145
static string GetPgName<T>(INpgsqlNameTranslator nameTranslator)
133-
{
134-
var attr = typeof(T).GetTypeInfo().GetCustomAttribute<PgNameAttribute>();
135-
return attr == null
136-
? nameTranslator.TranslateTypeName(typeof(T).Name)
137-
: attr.PgName;
138-
}
146+
=> typeof(T).GetCustomAttribute<PgNameAttribute>()?.PgName
147+
?? nameTranslator.TranslateTypeName(typeof(T).Name);
139148

140149
#endregion Misc
141150
}

0 commit comments

Comments
 (0)