|
14 | 14 | */ |
15 | 15 |
|
16 | 16 | using System; |
17 | | -using System.Collections; |
18 | 17 | using System.Collections.Generic; |
19 | 18 | using System.Linq.Expressions; |
20 | 19 | using System.Reflection; |
@@ -66,11 +65,21 @@ public static TranslatedExpression Translate(TranslationContext context, MemberE |
66 | 65 |
|
67 | 66 | if (!DocumentSerializerHelper.AreMembersRepresentedAsFields(containerTranslation.Serializer, out _)) |
68 | 67 | { |
69 | | - if (member is PropertyInfo propertyInfo && propertyInfo.Name == "Length") |
| 68 | + if (member is PropertyInfo propertyInfo && propertyInfo.Name == "Length") |
70 | 69 | { |
71 | 70 | return LengthPropertyToAggregationExpressionTranslator.Translate(context, expression); |
72 | 71 | } |
73 | 72 |
|
| 73 | + if (TryTranslateDictionaryProperty(expression, containerTranslation, member, out var translatedDictionaryProperty)) |
| 74 | + { |
| 75 | + return translatedDictionaryProperty; |
| 76 | + } |
| 77 | + |
| 78 | + if (TryTranslateKeyValuePairProperty(expression, containerTranslation, member, out var translatedKeyValuePairProperty)) |
| 79 | + { |
| 80 | + return translatedKeyValuePairProperty; |
| 81 | + } |
| 82 | + |
74 | 83 | if (TryTranslateCollectionCountProperty(expression, containerTranslation, member, out var translatedCount)) |
75 | 84 | { |
76 | 85 | return translatedCount; |
@@ -121,11 +130,20 @@ private static bool TryTranslateCollectionCountProperty(MemberExpression express |
121 | 130 | { |
122 | 131 | if (EnumerableProperty.IsCountProperty(expression)) |
123 | 132 | { |
124 | | - SerializationHelper.EnsureRepresentationIsArray(expression, container.Serializer); |
| 133 | + AstExpression ast; |
125 | 134 |
|
126 | | - var ast = AstExpression.Size(container.Ast); |
127 | | - var serializer = Int32Serializer.Instance; |
| 135 | + if (container.Serializer is IBsonDictionarySerializer dictionarySerializer && |
| 136 | + dictionarySerializer.DictionaryRepresentation == DictionaryRepresentation.Document) |
| 137 | + { |
| 138 | + ast = AstExpression.Size(AstExpression.ObjectToArray(container.Ast)); |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + SerializationHelper.EnsureRepresentationIsArray(expression, container.Serializer); |
| 143 | + ast = AstExpression.Size(container.Ast); |
| 144 | + } |
128 | 145 |
|
| 146 | + var serializer = Int32Serializer.Instance; |
129 | 147 | result = new TranslatedExpression(expression, ast, serializer); |
130 | 148 | return true; |
131 | 149 | } |
@@ -187,5 +205,142 @@ private static bool TryTranslateDateTimeProperty(MemberExpression expression, Tr |
187 | 205 |
|
188 | 206 | return false; |
189 | 207 | } |
| 208 | + |
| 209 | + private static bool TryTranslateKeyValuePairProperty(MemberExpression expression, TranslatedExpression container, MemberInfo memberInfo, out TranslatedExpression result) |
| 210 | + { |
| 211 | + result = null; |
| 212 | + |
| 213 | + if (container.Expression.Type.IsGenericType && |
| 214 | + container.Expression.Type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>) && |
| 215 | + container.Serializer is IKeyValuePairSerializerV2 { Representation: BsonType.Array } kvpSerializer) |
| 216 | + { |
| 217 | + AstExpression ast; |
| 218 | + IBsonSerializer serializer; |
| 219 | + |
| 220 | + switch (memberInfo.Name) |
| 221 | + { |
| 222 | + case "Key": |
| 223 | + ast = AstExpression.ArrayElemAt(container.Ast, 0); |
| 224 | + serializer = kvpSerializer.KeySerializer; |
| 225 | + break; |
| 226 | + case "Value": |
| 227 | + ast = AstExpression.ArrayElemAt(container.Ast, 1); |
| 228 | + serializer = kvpSerializer.ValueSerializer; |
| 229 | + break; |
| 230 | + default: |
| 231 | + throw new ExpressionNotSupportedException(expression); |
| 232 | + } |
| 233 | + result = new TranslatedExpression(expression, ast, serializer); |
| 234 | + return true; |
| 235 | + } |
| 236 | + |
| 237 | + return false; |
| 238 | + } |
| 239 | + |
| 240 | + private static bool TryTranslateDictionaryProperty(MemberExpression expression, TranslatedExpression container, MemberInfo memberInfo, out TranslatedExpression result) |
| 241 | + { |
| 242 | + result = null; |
| 243 | + |
| 244 | + if (memberInfo is PropertyInfo propertyInfo && |
| 245 | + propertyInfo.DeclaringType.IsGenericType && |
| 246 | + (propertyInfo.DeclaringType.GetGenericTypeDefinition() == typeof(Dictionary<,>) || |
| 247 | + propertyInfo.DeclaringType.GetGenericTypeDefinition() == typeof(IDictionary<,>))) |
| 248 | + { |
| 249 | + if (container.Serializer is IBsonDictionarySerializer dictionarySerializer) |
| 250 | + { |
| 251 | + switch (propertyInfo.Name) |
| 252 | + { |
| 253 | + case "Count": |
| 254 | + { |
| 255 | + AstExpression countAst; |
| 256 | + switch (dictionarySerializer.DictionaryRepresentation) |
| 257 | + { |
| 258 | + case DictionaryRepresentation.Document: |
| 259 | + countAst = AstExpression.Size(AstExpression.ObjectToArray(container.Ast)); |
| 260 | + break; |
| 261 | + case DictionaryRepresentation.ArrayOfArrays: |
| 262 | + case DictionaryRepresentation.ArrayOfDocuments: |
| 263 | + countAst = AstExpression.Size(container.Ast); |
| 264 | + break; |
| 265 | + default: |
| 266 | + throw new ExpressionNotSupportedException(expression); |
| 267 | + } |
| 268 | + |
| 269 | + var serializer = Int32Serializer.Instance; |
| 270 | + result = new TranslatedExpression(expression, countAst, serializer); |
| 271 | + return true; |
| 272 | + } |
| 273 | + |
| 274 | + case "Keys": |
| 275 | + { |
| 276 | + AstExpression keysAst; |
| 277 | + switch (dictionarySerializer.DictionaryRepresentation) |
| 278 | + { |
| 279 | + case DictionaryRepresentation.Document: |
| 280 | + keysAst = AstExpression.GetField(AstExpression.ObjectToArray(container.Ast), "k"); |
| 281 | + break; |
| 282 | + case DictionaryRepresentation.ArrayOfArrays: |
| 283 | + { |
| 284 | + var kvp = AstExpression.Var("kvp"); |
| 285 | + keysAst = AstExpression.Map( |
| 286 | + input: container.Ast, |
| 287 | + @as: kvp, |
| 288 | + @in: AstExpression.ArrayElemAt(kvp, 0)); |
| 289 | + break; |
| 290 | + } |
| 291 | + case DictionaryRepresentation.ArrayOfDocuments: |
| 292 | + keysAst = AstExpression.GetField(container.Ast, "k"); |
| 293 | + break; |
| 294 | + |
| 295 | + default: |
| 296 | + throw new ExpressionNotSupportedException(expression); |
| 297 | + } |
| 298 | + |
| 299 | + var serializer = ArraySerializerHelper.CreateSerializer(dictionarySerializer.KeySerializer); |
| 300 | + result = new TranslatedExpression(expression, keysAst, serializer); |
| 301 | + return true; |
| 302 | + } |
| 303 | + |
| 304 | + case "Values": |
| 305 | + { |
| 306 | + AstExpression valuesAst; |
| 307 | + switch (dictionarySerializer.DictionaryRepresentation) |
| 308 | + { |
| 309 | + case DictionaryRepresentation.Document: |
| 310 | + valuesAst = AstExpression.GetField(AstExpression.ObjectToArray(container.Ast), "v"); |
| 311 | + break; |
| 312 | + case DictionaryRepresentation.ArrayOfArrays: |
| 313 | + { |
| 314 | + var kvp = AstExpression.Var("kvp"); |
| 315 | + valuesAst = AstExpression.Map( |
| 316 | + input: container.Ast, |
| 317 | + @as: kvp, |
| 318 | + @in: AstExpression.ArrayElemAt(kvp, 1)); |
| 319 | + break; |
| 320 | + } |
| 321 | + case DictionaryRepresentation.ArrayOfDocuments: |
| 322 | + valuesAst = AstExpression.GetField(container.Ast, "v"); |
| 323 | + break; |
| 324 | + |
| 325 | + default: |
| 326 | + throw new ExpressionNotSupportedException(expression); |
| 327 | + } |
| 328 | + |
| 329 | + var serializer = ArraySerializerHelper.CreateSerializer(dictionarySerializer.ValueSerializer); |
| 330 | + result = new TranslatedExpression(expression, valuesAst, serializer); |
| 331 | + return true; |
| 332 | + } |
| 333 | + |
| 334 | + default: |
| 335 | + throw new ExpressionNotSupportedException(expression); |
| 336 | + } |
| 337 | + } |
| 338 | + |
| 339 | + throw new ExpressionNotSupportedException(expression, because: "serializer does not implement IBsonDictionarySerializer"); |
| 340 | + } |
| 341 | + |
| 342 | + return false; |
| 343 | + } |
| 344 | + |
190 | 345 | } |
191 | 346 | } |
0 commit comments