Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ public class ConnectionSettings : ConnectionSettingsBase<ConnectionSettings>
public ConnectionSettings(Uri uri = null)
: this(new SingleNodeConnectionPool(uri ?? new Uri("http://localhost:9200"))) { }

/// <summary>
/// Instantiate connection settings using a <see cref="SingleNodeConnectionPool"/> using the provided
/// <see cref="InMemoryConnection"/> that never uses any IO.
/// </summary>
public ConnectionSettings(InMemoryConnection connection)
: this(new SingleNodeConnectionPool(new Uri("http://localhost:9200")), connection) { }

public ConnectionSettings(IConnectionPool connectionPool) : this(connectionPool, null, null) { }

public ConnectionSettings(IConnectionPool connectionPool, SourceSerializerFactory sourceSerializer)
Expand Down
17 changes: 6 additions & 11 deletions src/Nest/CommonAbstractions/Extensions/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public static Expression<Func<T, object>> AppendSuffix<T>(this Expression<Func<T
/// </summary>
private class SuffixExpressionVisitor : ExpressionVisitor
{
private readonly string suffix;
private readonly string _suffix;

public SuffixExpressionVisitor(string suffix)
{
this.suffix = suffix;
this._suffix = suffix;
}

public override Expression Visit(Expression node)
Expand All @@ -38,13 +38,10 @@ public override Expression Visit(Expression node)
nameof(SuffixExtensions.Suffix),
null,
node,
Expression.Constant(suffix));
Expression.Constant(_suffix));
}

protected override Expression VisitUnary(UnaryExpression node)
{
return node;
}
protected override Expression VisitUnary(UnaryExpression node) => node;
}

private static readonly Regex ExpressionRegex = new Regex(@"^\s*(.*)\s*\=\>\s*\1\.");
Expand All @@ -56,14 +53,12 @@ internal static object ComparisonValueFromExpression(this Expression expression,

if (expression == null) return null;

var lambda = expression as LambdaExpression;
if (lambda == null)
if (!(expression is LambdaExpression lambda))
return ExpressionRegex.Replace(expression.ToString(), string.Empty);

type = lambda.Parameters.FirstOrDefault()?.Type;

var memberExpression = lambda.Body as MemberExpression;
return memberExpression != null
return lambda.Body is MemberExpression memberExpression
? MemberExpressionRegex.Replace(memberExpression.ToString(), string.Empty)
: ExpressionRegex.Replace(expression.ToString(), string.Empty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCall)
private static void VisitConstantOrVariable(MethodCallExpression methodCall, Stack<string> stack)
{
var lastArg = methodCall.Arguments.Last();
var constantExpression = lastArg as ConstantExpression;
var value = constantExpression != null
var value = lastArg is ConstantExpression constantExpression
? constantExpression.Value.ToString()
: Expression.Lambda(lastArg).Compile().DynamicInvoke().ToString();
stack.Push(value);
Expand Down
6 changes: 2 additions & 4 deletions src/Nest/CommonAbstractions/Infer/Field/FieldResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ private string ResolveFieldName(Field field)
return this.Resolve(field.Expression, field.Property);
}

string fieldName;
if (this.Fields.TryGetValue(field, out fieldName))
if (this.Fields.TryGetValue(field, out var fieldName))
return fieldName;

fieldName = this.Resolve(field.Expression, field.Property);
Expand All @@ -61,8 +60,7 @@ public string Resolve(PropertyName property)
return this.Resolve(property.Expression, property.Property);
}

string propertyName;
if (this.Properties.TryGetValue(property, out propertyName))
if (this.Properties.TryGetValue(property, out var propertyName))
return propertyName;

propertyName = this.Resolve(property.Expression, property.Property, true);
Expand Down
13 changes: 5 additions & 8 deletions src/Nest/CommonAbstractions/Infer/Id/IdResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ internal Func<T, string> CreateIdSelector<T>() where T : class
return idSelector;
}

internal static Func<object, object> MakeDelegate<T, U>(MethodInfo @get)
internal static Func<object, object> MakeDelegate<T, TReturn>(MethodInfo @get)
{
var f = (Func<T, U>)@get.CreateDelegate(typeof(Func<T, U>));
var f = (Func<T, TReturn>)@get.CreateDelegate(typeof(Func<T, TReturn>));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ for clearer generic parameter names

return t => f((T)t);
}

Expand All @@ -37,12 +37,9 @@ public string Resolve(Type type, object @object)
{
if (type == null || @object == null) return null;

Func<object, string> cachedLookup;
string field;
var preferLocal = this._connectionSettings.IdProperties.TryGetValue(type, out _);

var preferLocal = this._connectionSettings.IdProperties.TryGetValue(type, out field);

if (LocalIdDelegates.TryGetValue(type, out cachedLookup))
if (LocalIdDelegates.TryGetValue(type, out var cachedLookup))
return cachedLookup(@object);

if (!preferLocal && IdDelegates.TryGetValue(type, out cachedLookup))
Expand All @@ -55,7 +52,7 @@ public string Resolve(Type type, object @object)
}
var getMethod = idProperty.GetGetMethod();
var generic = MakeDelegateMethodInfo.MakeGenericMethod(type, getMethod.ReturnType);
var func = (Func<object, object>)generic.Invoke(null, new[] { getMethod });
var func = (Func<object, object>)generic.Invoke(null, new object[] { getMethod });
cachedLookup = o =>
{
var v = func(o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ public PropertyName(string name)

public PropertyName(Expression expression)
{
Type type;
Expression = expression;
CacheableExpression = !new HasVariableExpressionVisitor(expression).Found;
_comparisonValue = expression.ComparisonValueFromExpression(out type);
_comparisonValue = expression.ComparisonValueFromExpression(out var type);
_type = type;
}

Expand Down Expand Up @@ -86,10 +85,8 @@ public override bool Equals(object obj)
string IUrlParameter.GetString(IConnectionConfigurationValues settings)
{
var nestSettings = settings as IConnectionSettingsValues;
if (nestSettings == null)
throw new Exception("Tried to pass field name on querysting but it could not be resolved because no nest settings are available");
var infer = new Inferrer(nestSettings);
return infer.PropertyName(this);

return nestSettings?.Inferrer.PropertyName(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch 👍

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
writer.WriteNull();
return;
}
writer.WriteValue(new Inferrer(serializer.GetConnectionSettings()).PropertyName(property));
var infer = serializer.GetConnectionSettings().Inferrer;
writer.WriteValue(infer.PropertyName(property));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
Expand Down