Skip to content

Commit a3efebf

Browse files
authored
Use indexers to set default mapping values (#3807)
This commit updates DefaultMappingFor methods to assign default mapping values using indexers into dictionaries as opposed to using the Add method. Fixes #3706
1 parent 8462393 commit a3efebf

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ namespace Nest
1212
public class ConnectionSettings : ConnectionSettingsBase<ConnectionSettings>
1313
{
1414
/// <summary>
15-
/// A delegate used to construct a serializer to serialize CLR types representing documents and other types related to documents.
15+
/// A delegate used to construct a serializer to serialize CLR types representing documents and other types related to
16+
/// documents.
1617
/// By default, the internal serializer will be used to serializer all types.
1718
/// </summary>
1819
public delegate IElasticsearchSerializer SourceSerializerFactory(IElasticsearchSerializer builtIn, IConnectionSettingsValues values);
@@ -55,6 +56,7 @@ public abstract class ConnectionSettingsBase<TConnectionSettings> : ConnectionCo
5556
private readonly FluentDictionary<Type, string> _defaultIndices;
5657

5758
private readonly FluentDictionary<Type, string> _defaultRelationNames;
59+
private readonly HashSet<Type> _disableIdInference = new HashSet<Type>();
5860

5961
private readonly FluentDictionary<Type, string> _idProperties = new FluentDictionary<Type, string>();
6062

@@ -67,13 +69,11 @@ public abstract class ConnectionSettingsBase<TConnectionSettings> : ConnectionCo
6769
private readonly FluentDictionary<Type, string> _routeProperties = new FluentDictionary<Type, string>();
6870

6971
private readonly IElasticsearchSerializer _sourceSerializer;
72+
private bool _defaultDisableAllInference;
7073

7174
private Func<string, string> _defaultFieldNameInferrer;
7275
private string _defaultIndex;
7376

74-
private HashSet<Type> _disableIdInference = new HashSet<Type>();
75-
private bool _defaultDisableAllInference;
76-
7777
protected ConnectionSettingsBase(
7878
IConnectionPool connectionPool,
7979
IConnection connection,
@@ -83,7 +83,6 @@ IPropertyMappingProvider propertyMappingProvider
8383
: base(connectionPool, connection, null)
8484
{
8585
var formatterResolver = new NestFormatterResolver(this);
86-
//Utf8Json.JsonSerializer.SetDefaultResolver(formatterResolver);
8786
var defaultSerializer = new InternalSerializer(formatterResolver);
8887
_sourceSerializer = sourceSerializerFactory?.Invoke(defaultSerializer, this) ?? defaultSerializer;
8988
UseThisRequestResponseSerializer = defaultSerializer;
@@ -95,19 +94,19 @@ IPropertyMappingProvider propertyMappingProvider
9594
_defaultRelationNames = new FluentDictionary<Type, string>();
9695
_inferrer = new Inferrer(this);
9796
}
97+
bool IConnectionSettingsValues.DefaultDisableIdInference => _defaultDisableAllInference;
9898

9999
Func<string, string> IConnectionSettingsValues.DefaultFieldNameInferrer => _defaultFieldNameInferrer;
100100
string IConnectionSettingsValues.DefaultIndex => _defaultIndex;
101101
FluentDictionary<Type, string> IConnectionSettingsValues.DefaultIndices => _defaultIndices;
102+
HashSet<Type> IConnectionSettingsValues.DisableIdInference => _disableIdInference;
102103
FluentDictionary<Type, string> IConnectionSettingsValues.DefaultRelationNames => _defaultRelationNames;
103104
FluentDictionary<Type, string> IConnectionSettingsValues.IdProperties => _idProperties;
104105
Inferrer IConnectionSettingsValues.Inferrer => _inferrer;
105106
IPropertyMappingProvider IConnectionSettingsValues.PropertyMappingProvider => _propertyMappingProvider;
106107
FluentDictionary<MemberInfo, IPropertyMapping> IConnectionSettingsValues.PropertyMappings => _propertyMappings;
107108
FluentDictionary<Type, string> IConnectionSettingsValues.RouteProperties => _routeProperties;
108109
IElasticsearchSerializer IConnectionSettingsValues.SourceSerializer => _sourceSerializer;
109-
HashSet<Type> IConnectionSettingsValues.DisableIdInference => _disableIdInference;
110-
bool IConnectionSettingsValues.DefaultDisableIdInference => _defaultDisableAllInference;
111110

112111
/// <inheritdoc cref="IConnectionSettingsValues.DefaultIndex"/>
113112
public TConnectionSettings DefaultIndex(string defaultIndex) => Assign(defaultIndex, (a, v) => a._defaultIndex = v);
@@ -116,7 +115,7 @@ IPropertyMappingProvider propertyMappingProvider
116115
public TConnectionSettings DefaultFieldNameInferrer(Func<string, string> fieldNameInferrer) =>
117116
Assign(fieldNameInferrer, (a, v) => a._defaultFieldNameInferrer = v);
118117

119-
/// <inheritdoc cref="IConnectionSettingsValues.DisableIdInference"/>
118+
/// <inheritdoc cref="IConnectionSettingsValues.DefaultDisableIdInference" />
120119
public TConnectionSettings DefaultDisableIdInference(bool disable = true) => Assign(disable, (a, v) => a._defaultDisableAllInference = v);
121120

122121
/// <inheritdoc cref="IConnectionSettingsValues.IdProperties"/>
@@ -211,10 +210,10 @@ public TConnectionSettings DefaultMappingFor<TDocument>(Func<ClrTypeMappingDescr
211210
{
212211
var inferMapping = selector(new ClrTypeMappingDescriptor<TDocument>());
213212
if (!inferMapping.IndexName.IsNullOrEmpty())
214-
_defaultIndices.Add(inferMapping.ClrType, inferMapping.IndexName);
213+
_defaultIndices[inferMapping.ClrType] = inferMapping.IndexName;
215214

216215
if (!inferMapping.RelationName.IsNullOrEmpty())
217-
_defaultRelationNames.Add(inferMapping.ClrType, inferMapping.RelationName);
216+
_defaultRelationNames[inferMapping.ClrType] = inferMapping.RelationName;
218217

219218
if (!string.IsNullOrWhiteSpace(inferMapping.IdPropertyName))
220219
_idProperties[inferMapping.ClrType] = inferMapping.IdPropertyName;
@@ -242,29 +241,29 @@ public TConnectionSettings DefaultMappingFor(Type documentType, Func<ClrTypeMapp
242241
{
243242
var inferMapping = selector(new ClrTypeMappingDescriptor(documentType));
244243
if (!inferMapping.IndexName.IsNullOrEmpty())
245-
_defaultIndices.Add(inferMapping.ClrType, inferMapping.IndexName);
244+
_defaultIndices[inferMapping.ClrType] = inferMapping.IndexName;
246245

247246
if (!inferMapping.RelationName.IsNullOrEmpty())
248-
_defaultRelationNames.Add(inferMapping.ClrType, inferMapping.RelationName);
247+
_defaultRelationNames[inferMapping.ClrType] = inferMapping.RelationName;
249248

250249
if (!string.IsNullOrWhiteSpace(inferMapping.IdPropertyName))
251250
_idProperties[inferMapping.ClrType] = inferMapping.IdPropertyName;
252251

253252
return (TConnectionSettings)this;
254253
}
255254

256-
/// <inheritdoc cref="DefaultMappingFor(Type, Func{ClrTypeMappingDescriptor,IClrTypeMapping})"/>
255+
/// <inheritdoc cref="DefaultMappingFor(Type, Func{ClrTypeMappingDescriptor,IClrTypeMapping})" />
257256
public TConnectionSettings DefaultMappingFor(IEnumerable<IClrTypeMapping> typeMappings)
258257
{
259258
if (typeMappings == null) return (TConnectionSettings)this;
260259

261260
foreach (var inferMapping in typeMappings)
262261
{
263262
if (!inferMapping.IndexName.IsNullOrEmpty())
264-
_defaultIndices.Add(inferMapping.ClrType, inferMapping.IndexName);
263+
_defaultIndices[inferMapping.ClrType] = inferMapping.IndexName;
265264

266265
if (!inferMapping.RelationName.IsNullOrEmpty())
267-
_defaultRelationNames.Add(inferMapping.ClrType, inferMapping.RelationName);
266+
_defaultRelationNames[inferMapping.ClrType] = inferMapping.RelationName;
268267
}
269268

270269
return (TConnectionSettings)this;

0 commit comments

Comments
 (0)