|
13 | 13 | * limitations under the License.
|
14 | 14 | */
|
15 | 15 |
|
16 |
| -using System; |
| 16 | +using System.Linq; |
17 | 17 | using System.Linq.Expressions;
|
| 18 | +using System.Reflection; |
18 | 19 | using MongoDB.Bson.Serialization.Serializers;
|
19 | 20 | using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;
|
| 21 | +using MongoDB.Driver.Linq.Linq3Implementation.Misc; |
| 22 | +using MongoDB.Driver.Linq.Linq3Implementation.Reflection; |
20 | 23 |
|
21 | 24 | namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators
|
22 | 25 | {
|
23 | 26 | internal static class ToStringMethodToAggregationExpressionTranslator
|
24 | 27 | {
|
| 28 | + private static readonly MethodInfo[] __dateTimeToStringMethods = new[] |
| 29 | + { |
| 30 | + DateTimeMethod.ToStringWithFormat, |
| 31 | + DateTimeMethod.ToStringWithFormatAndTimezone, |
| 32 | + NullableDateTimeMethod.ToStringWithFormatAndTimezoneAndOnNull, |
| 33 | + }; |
| 34 | + |
| 35 | + private static readonly MethodInfo[] __dateTimeToStringMethodsWithTimezone = new[] |
| 36 | + { |
| 37 | + DateTimeMethod.ToStringWithFormatAndTimezone, |
| 38 | + NullableDateTimeMethod.ToStringWithFormatAndTimezoneAndOnNull, |
| 39 | + }; |
| 40 | + |
| 41 | + private static readonly MethodInfo[] __dateTimeToStringMethodsWithOnNull = new[] |
| 42 | + { |
| 43 | + NullableDateTimeMethod.ToStringWithFormatAndTimezoneAndOnNull, |
| 44 | + }; |
| 45 | + |
25 | 46 | public static AggregationExpression Translate(TranslationContext context, MethodCallExpression expression)
|
26 | 47 | {
|
27 |
| - if (TryTranslateInstanceToStringWithNoArguments(context, expression, out var translation) || |
28 |
| - TryTranslateDateTimeToStringWithFormat(context, expression, out translation)) |
| 48 | + var method = expression.Method; |
| 49 | + var arguments = expression.Arguments.ToArray(); |
| 50 | + |
| 51 | + if (IsInstanceToStringMethodWithNoArguments(method)) |
29 | 52 | {
|
30 |
| - return translation; |
| 53 | + return TranslateInstanceToStringMethodWithNoArguments(context, expression); |
| 54 | + } |
| 55 | + |
| 56 | + if (method.IsOneOf(__dateTimeToStringMethods)) |
| 57 | + { |
| 58 | + return TranslateDateTimeToStringMethod(context, expression, method, arguments); |
31 | 59 | }
|
32 | 60 |
|
33 | 61 | throw new ExpressionNotSupportedException(expression);
|
34 | 62 | }
|
35 | 63 |
|
36 |
| - private static bool TryTranslateDateTimeToStringWithFormat(TranslationContext context, MethodCallExpression expression, out AggregationExpression translation) |
| 64 | + private static bool IsInstanceToStringMethodWithNoArguments(MethodInfo method) |
37 | 65 | {
|
38 |
| - var method = expression.Method; |
39 |
| - var arguments = expression.Arguments; |
| 66 | + return |
| 67 | + !method.IsStatic && |
| 68 | + method.Name == "ToString" && |
| 69 | + method.ReturnType == typeof(string) && |
| 70 | + method.GetParameters().Length == 0; |
| 71 | + } |
40 | 72 |
|
41 |
| - if (method.DeclaringType != typeof(DateTime) || method.IsStatic || method.ReturnType != typeof(string) || method.Name != "ToString" || arguments.Count != 1 || arguments[0].Type != typeof(string)) |
42 |
| - { |
43 |
| - translation = null; |
44 |
| - return false; |
45 |
| - } |
| 73 | + private static AggregationExpression TranslateDateTimeToStringMethod(TranslationContext context, MethodCallExpression expression, MethodInfo method, Expression[] arguments) |
| 74 | + { |
46 | 75 |
|
47 |
| - var dateTimeExpression = expression.Object; |
| 76 | + var dateTimeExpression = method.IsStatic ? arguments[0] : expression.Object; |
48 | 77 | var dateTimeTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, dateTimeExpression);
|
49 |
| - var formatExpression = arguments[0]; |
50 |
| - var formatTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, formatExpression); |
51 |
| - var ast = AstExpression.DateToString(dateTimeTranslation.Ast, formatTranslation.Ast); |
52 |
| - translation = new AggregationExpression(expression, ast, new StringSerializer()); |
53 |
| - return true; |
54 |
| - } |
| 78 | + var dateAst = dateTimeTranslation.Ast; |
55 | 79 |
|
56 |
| - private static bool TryTranslateInstanceToStringWithNoArguments(TranslationContext context, MethodCallExpression expression, out AggregationExpression translation) |
57 |
| - { |
58 |
| - var method = expression.Method; |
59 |
| - var arguments = expression.Arguments; |
60 |
| - translation = null; |
| 80 | + AstExpression formatAst = null; |
| 81 | + var formatExpression = method.IsStatic ? arguments[1] : arguments[0]; |
| 82 | + if (!(formatExpression is ConstantExpression constantExprssion) || constantExprssion.Value != null) |
| 83 | + { |
| 84 | + var formatTranslataion = ExpressionToAggregationExpressionTranslator.Translate(context, formatExpression); |
| 85 | + formatAst = formatTranslataion.Ast; |
| 86 | + } |
61 | 87 |
|
62 |
| - if (method.IsStatic || method.ReturnType != typeof(string) || method.Name != "ToString" || arguments.Count != 0) |
| 88 | + AstExpression timezoneAst = null; |
| 89 | + if (method.IsOneOf(__dateTimeToStringMethodsWithTimezone)) |
63 | 90 | {
|
64 |
| - return false; |
| 91 | + var timezoneExpression = arguments[2]; |
| 92 | + if (!(timezoneExpression is ConstantExpression constantExpression) || constantExpression.Value != null) |
| 93 | + { |
| 94 | + var timezoneTranslataion = ExpressionToAggregationExpressionTranslator.Translate(context, timezoneExpression); |
| 95 | + timezoneAst = timezoneTranslataion.Ast; |
| 96 | + } |
65 | 97 | }
|
66 | 98 |
|
67 |
| - var objectExpression = expression.Object; |
| 99 | + AstExpression onNullAst = null; |
| 100 | + if (method.IsOneOf(__dateTimeToStringMethodsWithOnNull)) |
| 101 | + { |
| 102 | + var onNullExpression = arguments[3]; |
| 103 | + var onNullTranslataion = ExpressionToAggregationExpressionTranslator.Translate(context, onNullExpression); |
| 104 | + if (!(onNullExpression is ConstantExpression constantExpression) || constantExpression.Value != null) |
| 105 | + { |
| 106 | + onNullAst = onNullTranslataion.Ast; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + var ast = AstExpression.DateToString(dateAst, formatAst, timezoneAst, onNullAst); |
| 111 | + return new AggregationExpression(expression, ast, StringSerializer.Instance); |
| 112 | + } |
68 | 113 |
|
| 114 | + private static AggregationExpression TranslateInstanceToStringMethodWithNoArguments(TranslationContext context, MethodCallExpression expression) |
| 115 | + { |
| 116 | + var objectExpression = expression.Object; |
69 | 117 | var objectTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, objectExpression);
|
70 | 118 | var ast = AstExpression.ToString(objectTranslation.Ast);
|
71 |
| - |
72 |
| - translation = new AggregationExpression(expression, ast, new StringSerializer()); |
73 |
| - return true; |
| 119 | + return new AggregationExpression(expression, ast, StringSerializer.Instance); |
74 | 120 | }
|
75 | 121 | }
|
76 | 122 | }
|
0 commit comments