-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathExpressionToAggregationExpressionTranslator.cs
158 lines (143 loc) · 8.33 KB
/
ExpressionToAggregationExpressionTranslator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* 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;
using System.Linq;
using System.Linq.Expressions;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
using MongoDB.Driver.Linq.Linq3Implementation.Serializers;
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators
{
internal static class ExpressionToAggregationExpressionTranslator
{
// public static methods
public static AggregationExpression Translate(TranslationContext context, Expression expression, IBsonSerializer targetSerializer = null)
{
switch (expression.NodeType)
{
case ExpressionType.Convert:
case ExpressionType.TypeAs:
return ConvertExpressionToAggregationExpressionTranslator.Translate(context, (UnaryExpression)expression);
case ExpressionType.Not:
return NotExpressionToAggregationExpressionTranslator.Translate(context, (UnaryExpression)expression);
case ExpressionType.Add:
case ExpressionType.And:
case ExpressionType.AndAlso:
case ExpressionType.Coalesce:
case ExpressionType.Divide:
case ExpressionType.Equal:
case ExpressionType.ExclusiveOr:
case ExpressionType.GreaterThan:
case ExpressionType.GreaterThanOrEqual:
case ExpressionType.LessThan:
case ExpressionType.LessThanOrEqual:
case ExpressionType.Modulo:
case ExpressionType.Multiply:
case ExpressionType.NotEqual:
case ExpressionType.Or:
case ExpressionType.OrElse:
case ExpressionType.Power:
case ExpressionType.Subtract:
return BinaryExpressionToAggregationExpressionTranslator.Translate(context, (BinaryExpression)expression);
case ExpressionType.ArrayIndex:
return ArrayIndexExpressionToAggregationExpressionTranslator.Translate(context, (BinaryExpression)expression);
case ExpressionType.ArrayLength:
return ArrayLengthExpressionToAggregationExpressionTranslator.Translate(context, (UnaryExpression)expression);
case ExpressionType.Call:
return MethodCallExpressionToAggregationExpressionTranslator.Translate(context, (MethodCallExpression)expression, targetSerializer);
case ExpressionType.Conditional:
return ConditionalExpressionToAggregationExpressionTranslator.Translate(context, (ConditionalExpression)expression);
case ExpressionType.Constant:
return ConstantExpressionToAggregationExpressionTranslator.Translate((ConstantExpression)expression, targetSerializer);
case ExpressionType.Index:
return IndexExpressionToAggregationExpressionTranslator.Translate(context, (IndexExpression)expression);
case ExpressionType.ListInit:
return ListInitExpressionToAggregationExpressionTranslator.Translate(context, (ListInitExpression)expression);
case ExpressionType.MemberAccess:
return MemberExpressionToAggregationExpressionTranslator.Translate(context, (MemberExpression)expression);
case ExpressionType.MemberInit:
return MemberInitExpressionToAggregationExpressionTranslator.Translate(context, (MemberInitExpression)expression, targetSerializer);
case ExpressionType.Negate:
return NegateExpressionToAggregationExpressionTranslator.Translate(context, (UnaryExpression)expression);
case ExpressionType.New:
return NewExpressionToAggregationExpressionTranslator.Translate(context, (NewExpression)expression, targetSerializer);
case ExpressionType.NewArrayInit:
return NewArrayInitExpressionToAggregationExpressionTranslator.Translate(context, (NewArrayExpression)expression, targetSerializer);
case ExpressionType.Parameter:
return ParameterExpressionToAggregationExpressionTranslator.Translate(context, (ParameterExpression)expression);
case ExpressionType.TypeIs:
return TypeIsExpressionToAggregationExpressionTranslator.Translate(context, (TypeBinaryExpression)expression);
}
throw new ExpressionNotSupportedException(expression);
}
public static AggregationExpression TranslateEnumerable(TranslationContext context, Expression expression, IBsonSerializer targetSerializer = null)
{
var aggregateExpression = Translate(context, expression, targetSerializer);
var resultSerializer = aggregateExpression.Serializer;
if (resultSerializer is IWrappedEnumerableSerializer wrappedEnumerableSerializer)
{
var enumerableFieldName = wrappedEnumerableSerializer.EnumerableFieldName;
var itemSerializer = wrappedEnumerableSerializer.EnumerableElementSerializer;
var ast = AstExpression.GetField(aggregateExpression.Ast, enumerableFieldName);
resultSerializer = IEnumerableSerializer.Create(itemSerializer);
return new AggregationExpression(aggregateExpression.Expression, ast, resultSerializer);
}
return aggregateExpression;
}
public static AggregationExpression TranslateLambdaBody(
TranslationContext context,
LambdaExpression lambdaExpression,
IBsonSerializer parameterSerializer,
bool asRoot)
{
var parameterExpression = lambdaExpression.Parameters.Single();
if (parameterSerializer.ValueType != parameterExpression.Type)
{
throw new ArgumentException($"ValueType '{parameterSerializer.ValueType.FullName}' of parameterSerializer does not match parameter type '{parameterExpression.Type.FullName}'.", nameof(parameterSerializer));
}
var parameterSymbol =
asRoot ?
context.CreateRootSymbol(parameterExpression, parameterSerializer) :
context.CreateSymbol(parameterExpression, parameterSerializer, isCurrent: false);
return TranslateLambdaBody(context, lambdaExpression, parameterSymbol);
}
public static AggregationExpression TranslateLambdaBody(
TranslationContext context,
LambdaExpression lambdaExpression,
Symbol parameterSymbol)
{
var lambdaContext = context.WithSymbol(parameterSymbol);
var translatedBody = Translate(lambdaContext, lambdaExpression.Body);
var lambdaReturnType = lambdaExpression.ReturnType;
var bodySerializer = translatedBody.Serializer;
var bodyType = bodySerializer.ValueType;
if (bodyType != lambdaReturnType)
{
if (lambdaReturnType.IsAssignableFrom(bodyType))
{
var downcastingSerializer = DowncastingSerializer.Create(baseType: lambdaReturnType, derivedType: bodyType, derivedTypeSerializer: bodySerializer);
translatedBody = new AggregationExpression(translatedBody.Expression, translatedBody.Ast, downcastingSerializer);
}
else
{
throw new ExpressionNotSupportedException(lambdaExpression, because: "lambda body type is not convertible to lambda return type");
}
}
return translatedBody;
}
}
}