From cf48006a31f51294ac232e18a7579a0448faa721 Mon Sep 17 00:00:00 2001 From: Mpdreamz Date: Thu, 11 Jan 2018 22:11:52 +0100 Subject: [PATCH] ProperyNameJsonConverter and field resolver for propertyname was new'ing an Inferrer --- .../ConnectionSettingsBase.cs | 7 +++++++ .../Extensions/ExpressionExtensions.cs | 17 ++++++----------- .../Infer/Field/FieldExpressionVisitor.cs | 3 +-- .../Infer/Field/FieldResolver.cs | 6 ++---- .../CommonAbstractions/Infer/Id/IdResolver.cs | 13 +++++-------- .../Infer/PropertyName/PropertyName.cs | 9 +++------ .../PropertyName/PropertyNameJsonConverter.cs | 3 ++- 7 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs b/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs index a5ada2e7094..6733bec587f 100644 --- a/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs +++ b/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs @@ -19,6 +19,13 @@ public class ConnectionSettings : ConnectionSettingsBase public ConnectionSettings(Uri uri = null) : this(new SingleNodeConnectionPool(uri ?? new Uri("http://localhost:9200"))) { } + /// + /// Instantiate connection settings using a using the provided + /// that never uses any IO. + /// + 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) diff --git a/src/Nest/CommonAbstractions/Extensions/ExpressionExtensions.cs b/src/Nest/CommonAbstractions/Extensions/ExpressionExtensions.cs index b66084d7bfd..bead4b1151e 100644 --- a/src/Nest/CommonAbstractions/Extensions/ExpressionExtensions.cs +++ b/src/Nest/CommonAbstractions/Extensions/ExpressionExtensions.cs @@ -24,11 +24,11 @@ public static Expression> AppendSuffix(this Expression 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) @@ -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\."); @@ -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); } diff --git a/src/Nest/CommonAbstractions/Infer/Field/FieldExpressionVisitor.cs b/src/Nest/CommonAbstractions/Infer/Field/FieldExpressionVisitor.cs index cef1ecc4448..35aca6b39f6 100644 --- a/src/Nest/CommonAbstractions/Infer/Field/FieldExpressionVisitor.cs +++ b/src/Nest/CommonAbstractions/Infer/Field/FieldExpressionVisitor.cs @@ -151,8 +151,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCall) private static void VisitConstantOrVariable(MethodCallExpression methodCall, Stack 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); diff --git a/src/Nest/CommonAbstractions/Infer/Field/FieldResolver.cs b/src/Nest/CommonAbstractions/Infer/Field/FieldResolver.cs index 9083f2f20e9..b258d5fb31c 100644 --- a/src/Nest/CommonAbstractions/Infer/Field/FieldResolver.cs +++ b/src/Nest/CommonAbstractions/Infer/Field/FieldResolver.cs @@ -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); @@ -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); diff --git a/src/Nest/CommonAbstractions/Infer/Id/IdResolver.cs b/src/Nest/CommonAbstractions/Infer/Id/IdResolver.cs index 5ac69f553c0..7bc0ccdca7d 100644 --- a/src/Nest/CommonAbstractions/Infer/Id/IdResolver.cs +++ b/src/Nest/CommonAbstractions/Infer/Id/IdResolver.cs @@ -25,9 +25,9 @@ internal Func CreateIdSelector() where T : class return idSelector; } - internal static Func MakeDelegate(MethodInfo @get) + internal static Func MakeDelegate(MethodInfo @get) { - var f = (Func)@get.CreateDelegate(typeof(Func)); + var f = (Func)@get.CreateDelegate(typeof(Func)); return t => f((T)t); } @@ -37,12 +37,9 @@ public string Resolve(Type type, object @object) { if (type == null || @object == null) return null; - Func 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)) @@ -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)generic.Invoke(null, new[] { getMethod }); + var func = (Func)generic.Invoke(null, new object[] { getMethod }); cachedLookup = o => { var v = func(o); diff --git a/src/Nest/CommonAbstractions/Infer/PropertyName/PropertyName.cs b/src/Nest/CommonAbstractions/Infer/PropertyName/PropertyName.cs index c582e57be96..43a36862d14 100644 --- a/src/Nest/CommonAbstractions/Infer/PropertyName/PropertyName.cs +++ b/src/Nest/CommonAbstractions/Infer/PropertyName/PropertyName.cs @@ -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; } @@ -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); } } } diff --git a/src/Nest/CommonAbstractions/Infer/PropertyName/PropertyNameJsonConverter.cs b/src/Nest/CommonAbstractions/Infer/PropertyName/PropertyNameJsonConverter.cs index 02de94b7fb7..96d954582e7 100644 --- a/src/Nest/CommonAbstractions/Infer/PropertyName/PropertyNameJsonConverter.cs +++ b/src/Nest/CommonAbstractions/Infer/PropertyName/PropertyNameJsonConverter.cs @@ -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) {