|
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; |
@@ -71,11 +70,21 @@ public static TranslatedExpression Translate(TranslationContext context, MemberE |
71 | 70 |
|
72 | 71 | if (!DocumentSerializerHelper.AreMembersRepresentedAsFields(containerTranslation.Serializer, out _)) |
73 | 72 | { |
74 | | - if (member is PropertyInfo propertyInfo && propertyInfo.Name == "Length") |
| 73 | + if (member is PropertyInfo propertyInfo && propertyInfo.Name == "Length") |
75 | 74 | { |
76 | 75 | return LengthPropertyToAggregationExpressionTranslator.Translate(context, expression); |
77 | 76 | } |
78 | 77 |
|
| 78 | + if (TryTranslateDictionaryProperty(expression, containerTranslation, member, out var translatedDictionaryProperty)) |
| 79 | + { |
| 80 | + return translatedDictionaryProperty; |
| 81 | + } |
| 82 | + |
| 83 | + if (TryTranslateKeyValuePairProperty(expression, containerTranslation, member, out var translatedKeyValuePairProperty)) |
| 84 | + { |
| 85 | + return translatedKeyValuePairProperty; |
| 86 | + } |
| 87 | + |
79 | 88 | if (TryTranslateCollectionCountProperty(expression, containerTranslation, member, out var translatedCount)) |
80 | 89 | { |
81 | 90 | return translatedCount; |
@@ -126,11 +135,20 @@ private static bool TryTranslateCollectionCountProperty(MemberExpression express |
126 | 135 | { |
127 | 136 | if (EnumerableProperty.IsCountProperty(expression)) |
128 | 137 | { |
129 | | - SerializationHelper.EnsureRepresentationIsArray(expression, container.Serializer); |
| 138 | + AstExpression ast; |
130 | 139 |
|
131 | | - var ast = AstExpression.Size(container.Ast); |
132 | | - var serializer = Int32Serializer.Instance; |
| 140 | + if (container.Serializer is IBsonDictionarySerializer dictionarySerializer && |
| 141 | + dictionarySerializer.DictionaryRepresentation == DictionaryRepresentation.Document) |
| 142 | + { |
| 143 | + ast = AstExpression.Size(AstExpression.ObjectToArray(container.Ast)); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + SerializationHelper.EnsureRepresentationIsArray(expression, container.Serializer); |
| 148 | + ast = AstExpression.Size(container.Ast); |
| 149 | + } |
133 | 150 |
|
| 151 | + var serializer = Int32Serializer.Instance; |
134 | 152 | result = new TranslatedExpression(expression, ast, serializer); |
135 | 153 | return true; |
136 | 154 | } |
@@ -261,5 +279,141 @@ private static bool TryTranslateDictionaryProperty(TranslationContext context, M |
261 | 279 | translatedDictionaryProperty = null; |
262 | 280 | return false; |
263 | 281 | } |
| 282 | + |
| 283 | + private static bool TryTranslateKeyValuePairProperty(MemberExpression expression, TranslatedExpression container, MemberInfo memberInfo, out TranslatedExpression result) |
| 284 | + { |
| 285 | + result = null; |
| 286 | + |
| 287 | + if (container.Expression.Type.IsGenericType && |
| 288 | + container.Expression.Type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>) && |
| 289 | + container.Serializer is IKeyValuePairSerializerV2 { Representation: BsonType.Array } kvpSerializer) |
| 290 | + { |
| 291 | + AstExpression ast; |
| 292 | + IBsonSerializer serializer; |
| 293 | + |
| 294 | + switch (memberInfo.Name) |
| 295 | + { |
| 296 | + case "Key": |
| 297 | + ast = AstExpression.ArrayElemAt(container.Ast, 0); |
| 298 | + serializer = kvpSerializer.KeySerializer; |
| 299 | + break; |
| 300 | + case "Value": |
| 301 | + ast = AstExpression.ArrayElemAt(container.Ast, 1); |
| 302 | + serializer = kvpSerializer.ValueSerializer; |
| 303 | + break; |
| 304 | + default: |
| 305 | + throw new ExpressionNotSupportedException(expression); |
| 306 | + } |
| 307 | + result = new TranslatedExpression(expression, ast, serializer); |
| 308 | + return true; |
| 309 | + } |
| 310 | + |
| 311 | + return false; |
| 312 | + } |
| 313 | + |
| 314 | + private static bool TryTranslateDictionaryProperty(MemberExpression expression, TranslatedExpression container, MemberInfo memberInfo, out TranslatedExpression result) |
| 315 | + { |
| 316 | + result = null; |
| 317 | + |
| 318 | + if (memberInfo is PropertyInfo propertyInfo && |
| 319 | + propertyInfo.DeclaringType.IsGenericType && |
| 320 | + (propertyInfo.DeclaringType.GetGenericTypeDefinition() == typeof(Dictionary<,>) || |
| 321 | + propertyInfo.DeclaringType.GetGenericTypeDefinition() == typeof(IDictionary<,>))) |
| 322 | + { |
| 323 | + if (container.Serializer is IBsonDictionarySerializer dictionarySerializer) |
| 324 | + { |
| 325 | + switch (propertyInfo.Name) |
| 326 | + { |
| 327 | + case "Count": |
| 328 | + { |
| 329 | + AstExpression countAst; |
| 330 | + switch (dictionarySerializer.DictionaryRepresentation) |
| 331 | + { |
| 332 | + case DictionaryRepresentation.Document: |
| 333 | + countAst = AstExpression.Size(AstExpression.ObjectToArray(container.Ast)); |
| 334 | + break; |
| 335 | + case DictionaryRepresentation.ArrayOfArrays: |
| 336 | + case DictionaryRepresentation.ArrayOfDocuments: |
| 337 | + countAst = AstExpression.Size(container.Ast); |
| 338 | + break; |
| 339 | + default: |
| 340 | + throw new ExpressionNotSupportedException(expression); |
| 341 | + } |
| 342 | + |
| 343 | + var serializer = Int32Serializer.Instance; |
| 344 | + result = new TranslatedExpression(expression, countAst, serializer); |
| 345 | + return true; |
| 346 | + } |
| 347 | + |
| 348 | + case "Keys": |
| 349 | + { |
| 350 | + AstExpression keysAst; |
| 351 | + switch (dictionarySerializer.DictionaryRepresentation) |
| 352 | + { |
| 353 | + case DictionaryRepresentation.Document: |
| 354 | + keysAst = AstExpression.GetField(AstExpression.ObjectToArray(container.Ast), "k"); |
| 355 | + break; |
| 356 | + case DictionaryRepresentation.ArrayOfArrays: |
| 357 | + { |
| 358 | + var kvp = AstExpression.Var("kvp"); |
| 359 | + keysAst = AstExpression.Map( |
| 360 | + input: container.Ast, |
| 361 | + @as: kvp, |
| 362 | + @in: AstExpression.ArrayElemAt(kvp, 0)); |
| 363 | + break; |
| 364 | + } |
| 365 | + case DictionaryRepresentation.ArrayOfDocuments: |
| 366 | + keysAst = AstExpression.GetField(container.Ast, "k"); |
| 367 | + break; |
| 368 | + |
| 369 | + default: |
| 370 | + throw new ExpressionNotSupportedException(expression); |
| 371 | + } |
| 372 | + |
| 373 | + var serializer = ArraySerializerHelper.CreateSerializer(dictionarySerializer.KeySerializer); |
| 374 | + result = new TranslatedExpression(expression, keysAst, serializer); |
| 375 | + return true; |
| 376 | + } |
| 377 | + |
| 378 | + case "Values": |
| 379 | + { |
| 380 | + AstExpression valuesAst; |
| 381 | + switch (dictionarySerializer.DictionaryRepresentation) |
| 382 | + { |
| 383 | + case DictionaryRepresentation.Document: |
| 384 | + valuesAst = AstExpression.GetField(AstExpression.ObjectToArray(container.Ast), "v"); |
| 385 | + break; |
| 386 | + case DictionaryRepresentation.ArrayOfArrays: |
| 387 | + { |
| 388 | + var kvp = AstExpression.Var("kvp"); |
| 389 | + valuesAst = AstExpression.Map( |
| 390 | + input: container.Ast, |
| 391 | + @as: kvp, |
| 392 | + @in: AstExpression.ArrayElemAt(kvp, 1)); |
| 393 | + break; |
| 394 | + } |
| 395 | + case DictionaryRepresentation.ArrayOfDocuments: |
| 396 | + valuesAst = AstExpression.GetField(container.Ast, "v"); |
| 397 | + break; |
| 398 | + |
| 399 | + default: |
| 400 | + throw new ExpressionNotSupportedException(expression); |
| 401 | + } |
| 402 | + |
| 403 | + var serializer = ArraySerializerHelper.CreateSerializer(dictionarySerializer.ValueSerializer); |
| 404 | + result = new TranslatedExpression(expression, valuesAst, serializer); |
| 405 | + return true; |
| 406 | + } |
| 407 | + |
| 408 | + default: |
| 409 | + throw new ExpressionNotSupportedException(expression); |
| 410 | + } |
| 411 | + } |
| 412 | + |
| 413 | + throw new ExpressionNotSupportedException(expression, because: "serializer does not implement IBsonDictionarySerializer"); |
| 414 | + } |
| 415 | + |
| 416 | + return false; |
| 417 | + } |
264 | 418 | } |
265 | 419 | } |
0 commit comments