Skip to content

Commit aceb897

Browse files
committed
simplified Inferrer
1 parent 0832b32 commit aceb897

File tree

6 files changed

+50
-41
lines changed

6 files changed

+50
-41
lines changed

src/Nest/CommonAbstractions/Infer/Field/FieldResolver.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Collections.ObjectModel;
5+
using System.Globalization;
56
using System.Linq;
67
using System.Linq.Expressions;
78
using System.Reflection;
@@ -18,12 +19,33 @@ public class FieldResolver : ExpressionVisitor
1819

1920
public FieldResolver(IConnectionSettingsValues settings)
2021
{
21-
if (settings == null)
22-
throw new ArgumentNullException("settings");
23-
_settings = settings;
22+
settings.ThrowIfNull(nameof(settings));
23+
this._settings = settings;
2424
}
2525

26-
public string Resolve(MemberInfo info)
26+
public string Resolve(Field field) =>
27+
field.IsConditionless() ? null : Resolve(field.Name, field.Expression, field.Property);
28+
29+
internal string Resolve(PropertyName property) =>
30+
property.IsConditionless() ? null : Resolve(property.Name, property.Expression, property.Property);
31+
32+
private string Resolve(string verbatim, Expression expression, MemberInfo member)
33+
{
34+
var name = !verbatim.IsNullOrEmpty()
35+
? verbatim
36+
: expression != null
37+
? this.ResolveExpression(expression)
38+
: member != null
39+
? this.ResolveMemberInfo(member)
40+
: null;
41+
42+
if (name == null)
43+
throw new ArgumentException("Could not resolve a property name");
44+
45+
return name;
46+
}
47+
48+
private string ResolveMemberInfo(MemberInfo info)
2749
{
2850
if (info == null)
2951
return null;
@@ -37,7 +59,7 @@ public string Resolve(MemberInfo info)
3759
return _settings.Serializer?.CreatePropertyName(info) ?? _settings.DefaultFieldNameInferrer(name);
3860
}
3961

40-
public string Resolve(Expression expression)
62+
private string ResolveExpression(Expression expression)
4163
{
4264
var stack = new Stack<string>();
4365
var properties = new Stack<ElasticsearchPropertyAttribute>();
@@ -52,11 +74,9 @@ public string Resolve(Expression expression)
5274

5375
protected override Expression VisitMemberAccess(MemberExpression expression, Stack<string> stack, Stack<ElasticsearchPropertyAttribute> properties)
5476
{
55-
if (stack != null)
56-
{
57-
var resolvedName = this.Resolve(expression.Member);
58-
stack.Push(resolvedName);
59-
}
77+
if (stack == null) return base.VisitMemberAccess(expression, stack, properties);
78+
var resolvedName = this.ResolveMemberInfo(expression.Member);
79+
stack.Push(resolvedName);
6080
return base.VisitMemberAccess(expression, stack, properties);
6181
}
6282

src/Nest/CommonAbstractions/Infer/Id/IdResolver.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public IdResolver(IConnectionSettingsValues connectionSettings)
2121

2222
internal Func<T, string> CreateIdSelector<T>() where T : class
2323
{
24-
Func<T, string> idSelector = this.GetIdFor;
24+
Func<T, string> idSelector = this.Resolve;
2525
return idSelector;
2626
}
2727

@@ -31,8 +31,12 @@ internal static Func<object, object> MakeDelegate<T, U>(MethodInfo @get)
3131
return t => f((T)t);
3232
}
3333

34-
public string GetIdFor(Type type, object @object)
34+
public string Resolve<T>(T @object) => @object == null ? null : Resolve(@object.GetType(), @object);
35+
36+
public string Resolve(Type type, object @object)
3537
{
38+
if (type == null || @object == null) return null;
39+
3640
Func<object, string> cachedLookup;
3741
string field;
3842

@@ -64,14 +68,6 @@ public string GetIdFor(Type type, object @object)
6468
return cachedLookup(@object);
6569
}
6670

67-
public string GetIdFor<T>(T @object)
68-
{
69-
if (@object == null)
70-
return null;
71-
72-
//var type = typeof(T);
73-
return GetIdFor(@object.GetType(), @object);
74-
}
7571

7672
private PropertyInfo GetInferredId(Type type)
7773
{

src/Nest/CommonAbstractions/Infer/IndexName/IndexNameExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static string Resolve(this IndexName marker, IConnectionSettingsValues co
1111

1212
if (marker.Type == null)
1313
return marker.Name;
14-
return new IndexNameResolver(connectionSettings).GetIndexForType(marker.Type);
14+
return new IndexNameResolver(connectionSettings).Resolve(marker.Type);
1515
}
1616
}
1717
}

src/Nest/CommonAbstractions/Infer/IndexName/IndexNameResolver.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ public IndexNameResolver(IConnectionSettingsValues connectionSettings)
1111
connectionSettings.ThrowIfNull(nameof(connectionSettings));
1212
this._connectionSettings = connectionSettings;
1313
}
14+
public string Resolve<T>() where T : class => this.Resolve(typeof(T));
1415

15-
public string GetIndexForType(Type type)
16+
public string Resolve(IndexName i)
17+
{
18+
if (i == null) return this.Resolve((Type)null);
19+
return i.Name ?? this.Resolve(i.Type);
20+
}
21+
22+
public string Resolve(Type type)
1623
{
1724
var defaultIndices = this._connectionSettings.DefaultIndices;
1825

@@ -29,11 +36,5 @@ public string GetIndexForType(Type type)
2936
}
3037

3138

32-
internal string GetIndexForType(IndexName i)
33-
{
34-
if (i == null) return this.GetIndexForType((Type)null);
35-
36-
return i.Name ?? this.GetIndexForType(i.Type);
37-
}
3839
}
3940
}

src/Nest/CommonAbstractions/Infer/TypeName/TypeNameResolver.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ public TypeNameResolver(IConnectionSettingsValues connectionSettings)
1212
this._connectionSettings = connectionSettings;
1313
}
1414

15-
public string GetTypeNameFor<T>()
16-
{
17-
return this.GetTypeNameFor(typeof(T));
18-
}
15+
public string Resolve<T>() where T : class => this.Resolve(typeof(T));
1916

20-
public string GetTypeNameFor(Type type)
17+
public string Resolve(TypeName t) => t?.Name ?? this.ResolveType(t?.Type);
18+
19+
private string ResolveType(Type type)
2120
{
2221
if (type == null) return null;
2322
string typeName;
@@ -33,12 +32,5 @@ public string GetTypeNameFor(Type type)
3332
return typeName;
3433
}
3534

36-
37-
internal string GetTypeNameFor(TypeName t)
38-
{
39-
if (t == null) return null;
40-
41-
return t.Name ?? this.GetTypeNameFor(t.Type);
42-
}
4335
}
4436
}

src/Nest/Nest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@
877877
<Compile Include="Mapping\DynamicMapping.cs" />
878878
<Compile Include="Search\Suggesters\CompletionSuggester\CompletionSuggester.cs" />
879879
<Compile Include="QueryDsl\Compound\FunctionScore\FunctionScoreQuery.cs" />
880-
<Compile Include="CommonAbstractions\Infer\ElasticInferrer.cs" />
880+
<Compile Include="CommonAbstractions\Infer\Inferrer.cs" />
881881
<Compile Include="Document\Multiple\Reindex\IReindexResponse.cs" />
882882
<Compile Include="Search\Suggesters\Suggest.cs" />
883883
<Compile Include="Search\Suggesters\SuggestOption.cs" />

0 commit comments

Comments
 (0)