-
Couldn't load subscription status.
- Fork 1.3k
CSHARP-5730: Support static String.Compare method #1789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fb728dc
3a85339
f4e62c6
eea6c74
3852a30
d34f3d3
b429c2e
bc8cb71
0d10969
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,8 @@ | |
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
|
|
||
| namespace MongoDB.Driver.Linq.Linq3Implementation.Misc | ||
|
|
@@ -38,6 +40,30 @@ public static bool Is(this MethodInfo method, MethodInfo comparand) | |
| return false; | ||
| } | ||
|
|
||
| public static bool IsInstanceCompareToMethod(this MethodInfo method) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put here so that it can be used from more than one place. |
||
| { | ||
| if (method.IsPublic && | ||
| !method.IsStatic && | ||
| method.ReturnType == typeof(int) && | ||
| method.Name == "CompareTo" && | ||
| method.GetParameters() is var parameters && | ||
| parameters.Length == 1) | ||
| { | ||
| var declaringType = method.DeclaringType; | ||
| var comparandType = declaringType switch | ||
| { | ||
| _ when declaringType == typeof(IComparable) => typeof(object), | ||
| _ when declaringType.IsConstructedGenericType && declaringType.GetGenericTypeDefinition() == typeof(IComparable<>) => declaringType.GetGenericArguments().Single(), | ||
| _ => declaringType | ||
| }; | ||
|
|
||
| var parameterType = parameters[0].ParameterType; | ||
| return parameterType == comparandType; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public static bool IsOneOf(this MethodInfo method, MethodInfo comparand1, MethodInfo comparand2) | ||
| { | ||
| return method.Is(comparand1) || method.Is(comparand2); | ||
|
|
@@ -78,5 +104,18 @@ public static bool IsOneOf(this MethodInfo method, params MethodInfo[][] compara | |
|
|
||
| return false; | ||
| } | ||
|
|
||
| public static bool IsStaticCompareMethod(this MethodInfo method) | ||
| { | ||
| return | ||
| method.IsPublic && | ||
| method.IsStatic && | ||
| method.ReturnType == typeof(int) && | ||
| method.Name == "Compare" && | ||
| method.GetParameters() is var parameters && | ||
| parameters.Length == 2 && | ||
| parameters[0].ParameterType == method.DeclaringType && | ||
| parameters[1].ParameterType == method.DeclaringType; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* Copyright 2010-present MongoDB Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System.Linq.Expressions; | ||
| using System.Reflection; | ||
| using MongoDB.Bson.Serialization.Serializers; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
|
|
||
| namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators | ||
| { | ||
| internal static class CompareMethodToAggregationExpressionTranslator | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| private static readonly MethodInfo[] __stringCompareMethods = | ||
| [ | ||
| StringMethod.StaticCompare, | ||
| StringMethod.StaticCompareWithIgnoreCase | ||
| ]; | ||
|
|
||
| public static TranslatedExpression Translate(TranslationContext context, MethodCallExpression expression) | ||
| { | ||
| var method = expression.Method; | ||
| var arguments = expression.Arguments; | ||
|
|
||
| if (method.IsStaticCompareMethod() || method.IsInstanceCompareToMethod() || method.IsOneOf(__stringCompareMethods)) | ||
| { | ||
| Expression value1Expression; | ||
| Expression value2Expression; | ||
| if (method.IsStatic) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main difference between |
||
| { | ||
| value1Expression = arguments[0]; | ||
| value2Expression = arguments[1]; | ||
| } | ||
| else | ||
| { | ||
| value1Expression = expression.Object; | ||
| value2Expression = arguments[0]; | ||
| } | ||
|
|
||
| var value1Translation = ExpressionToAggregationExpressionTranslator.Translate(context, value1Expression); | ||
| var value2Translation = ExpressionToAggregationExpressionTranslator.Translate(context, value2Expression); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check if the |
||
| AstExpression ast; | ||
| if (method.Is(StringMethod.StaticCompareWithIgnoreCase)) | ||
| { | ||
| var ignoreCaseExpression = arguments[2]; | ||
| var ignoreCase = ignoreCaseExpression.GetConstantValue<bool>(containingExpression: expression); | ||
| ast = ignoreCase | ||
| ? AstExpression.StrCaseCmp(value1Translation.Ast, value2Translation.Ast) | ||
| : AstExpression.Cmp(value1Translation.Ast, value2Translation.Ast); | ||
| } | ||
| else | ||
| { | ||
| ast = AstExpression.Cmp(value1Translation.Ast, value2Translation.Ast); | ||
| } | ||
|
|
||
| return new TranslatedExpression(expression, ast, Int32Serializer.Instance); | ||
| } | ||
|
|
||
| throw new ExpressionNotSupportedException(expression); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* Copyright 2010-present MongoDB Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System.Linq.Expressions; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators; | ||
|
|
||
| namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionTranslators | ||
| { | ||
| internal static class CompareComparisonExpressionToFilterTranslator | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| public static bool CanTranslate(Expression leftExpression) | ||
| { | ||
| return | ||
| leftExpression is MethodCallExpression leftMethodCallExpression && | ||
| leftMethodCallExpression.Method is var method && | ||
| (method.IsStaticCompareMethod() || method.IsInstanceCompareToMethod() || method.Is(StringMethod.StaticCompareWithIgnoreCase)); | ||
| } | ||
|
|
||
| // caller is responsible for ensuring constant is on the right | ||
| public static AstFilter Translate( | ||
| TranslationContext context, | ||
| Expression expression, | ||
| Expression leftExpression, | ||
| AstComparisonFilterOperator outerComparisonOperator, | ||
| Expression rightExpression) | ||
| { | ||
| if (CanTranslate(leftExpression)) | ||
| { | ||
| var compareMethodCallExpression = (MethodCallExpression)leftExpression; | ||
| var compareMethod = compareMethodCallExpression.Method; | ||
| var compareArguments = compareMethodCallExpression.Arguments; | ||
| var outerValue = rightExpression.GetConstantValue<int>(containingExpression: expression); | ||
|
|
||
| Expression fieldExpression; | ||
| Expression innerValueExpression; | ||
| if (compareMethod.IsStatic) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main difference between Compare and CompareTo is whether it's a static method or not. |
||
| { | ||
| fieldExpression = compareArguments[0]; | ||
| innerValueExpression = compareArguments[1]; | ||
| } | ||
| else | ||
| { | ||
| fieldExpression = compareMethodCallExpression.Object; | ||
| innerValueExpression = compareArguments[0]; | ||
| } | ||
|
|
||
| if (compareMethod.Is(StringMethod.StaticCompareWithIgnoreCase)) | ||
| { | ||
| var ignoreCaseExpression = compareArguments[2]; | ||
| var ignoreCase = ignoreCaseExpression.GetConstantValue<bool>(containingExpression: compareMethodCallExpression); | ||
| if (ignoreCase) | ||
| { | ||
| throw new ExpressionNotSupportedException(compareMethodCallExpression, because: "ignoreCase must be false"); | ||
| } | ||
| } | ||
|
|
||
| var fieldComparisonOperator = (outerComparisonOperator, outerValue) switch | ||
| { | ||
| (AstComparisonFilterOperator.Eq, -1) => AstComparisonFilterOperator.Lt, | ||
| (AstComparisonFilterOperator.Ne, -1) => AstComparisonFilterOperator.Gte, | ||
| (AstComparisonFilterOperator.Gt, -1) => AstComparisonFilterOperator.Gte, | ||
| (AstComparisonFilterOperator.Eq, 0) => AstComparisonFilterOperator.Eq, | ||
| (AstComparisonFilterOperator.Ne, 0) => AstComparisonFilterOperator.Ne, | ||
| (AstComparisonFilterOperator.Lt, 0) => AstComparisonFilterOperator.Lt, | ||
| (AstComparisonFilterOperator.Lte, 0) => AstComparisonFilterOperator.Lte, | ||
| (AstComparisonFilterOperator.Gt, 0) => AstComparisonFilterOperator.Gt, | ||
| (AstComparisonFilterOperator.Gte, 0) => AstComparisonFilterOperator.Gte, | ||
| (AstComparisonFilterOperator.Eq, 1) => AstComparisonFilterOperator.Gt, | ||
| (AstComparisonFilterOperator.Ne, 1) => AstComparisonFilterOperator.Lte, | ||
| (AstComparisonFilterOperator.Lt, 1) => AstComparisonFilterOperator.Lte, | ||
| _ => throw new ExpressionNotSupportedException(expression) | ||
| }; | ||
|
|
||
| if (fieldExpression.NodeType == ExpressionType.Constant && innerValueExpression.NodeType != ExpressionType.Constant) | ||
| { | ||
| (fieldExpression, innerValueExpression) = (innerValueExpression, fieldExpression); | ||
| fieldComparisonOperator = fieldComparisonOperator.GetComparisonOperatorForSwappedLeftAndRight(); | ||
| } | ||
|
|
||
| var fieldTranslation = ExpressionToFilterFieldTranslator.Translate(context, fieldExpression); | ||
| var value = innerValueExpression.GetConstantValue<object>(containingExpression: expression); | ||
| var serializedValue = SerializationHelper.SerializeValue(fieldTranslation.Serializer, value); | ||
|
|
||
| return AstFilter.Compare(fieldTranslation.Ast, fieldComparisonOperator, serializedValue); | ||
| } | ||
|
|
||
| throw new ExpressionNotSupportedException(expression); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put here so that it can be used from more than one place.