-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-4779: Support Dictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection) constructor in LINQ… #1657
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
Changes from 3 commits
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* 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.Collections.Generic; | ||
using System.Reflection; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Reflection | ||
{ | ||
internal static class DictionaryConstructor | ||
{ | ||
public static bool IsWithIEnumerableKeyValuePairConstructor(ConstructorInfo constructor) | ||
{ | ||
var parameters = constructor.GetParameters(); | ||
return | ||
parameters.Length == 1 && | ||
parameters[0].ParameterType.ImplementsIEnumerable(out var enumerableType) && | ||
enumerableType.IsConstructedGenericType && | ||
enumerableType.GetGenericTypeDefinition() == typeof(KeyValuePair<,>); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* 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.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization; | ||
using MongoDB.Bson.Serialization.Options; | ||
using MongoDB.Bson.Serialization.Serializers; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Reflection; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators | ||
{ | ||
internal static class NewDictionaryExpressionToAggregationExpressionTranslator | ||
{ | ||
public static bool CanTranslate(NewExpression expression) | ||
=> expression.Type.IsConstructedGenericType && | ||
expression.Type.GetGenericTypeDefinition() == typeof(Dictionary<,>) && | ||
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. I think the checks in line 31-32 should actually be in You should be able to call |
||
DictionaryConstructor.IsWithIEnumerableKeyValuePairConstructor(expression.Constructor); | ||
|
||
public static TranslatedExpression Translate(TranslationContext context, NewExpression expression) | ||
{ | ||
var arguments = expression.Arguments; | ||
|
||
var collectionExpression = arguments[0]; | ||
var collectionTranslation = ExpressionToAggregationExpressionTranslator.TranslateEnumerable(context, collectionExpression); | ||
var itemSerializer = ArraySerializerHelper.GetItemSerializer(collectionTranslation.Serializer); | ||
|
||
IBsonSerializer keySerializer; | ||
IBsonSerializer valueSerializer; | ||
AstExpression collectionTranslationAst; | ||
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. nit: move this declaration inside the first if statement below 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. Done |
||
|
||
if (itemSerializer is IBsonDocumentSerializer itemDocumentSerializer) | ||
{ | ||
if (!itemDocumentSerializer.TryGetMemberSerializationInfo("Key", out var keyMemberSerializationInfo)) | ||
{ | ||
throw new ExpressionNotSupportedException(expression, because: $"serializer class {itemSerializer.GetType()} does not have a Key member"); | ||
} | ||
keySerializer = keyMemberSerializationInfo.Serializer; | ||
|
||
if (!itemDocumentSerializer.TryGetMemberSerializationInfo("Value", out var valueMemberSerializationInfo)) | ||
{ | ||
throw new ExpressionNotSupportedException(expression, because: $"serializer class {itemSerializer.GetType()} does not have a Value member"); | ||
} | ||
valueSerializer = valueMemberSerializationInfo.Serializer; | ||
|
||
if (keyMemberSerializationInfo.ElementName == "k" && valueMemberSerializationInfo.ElementName == "v") | ||
{ | ||
collectionTranslationAst = collectionTranslation.Ast; | ||
} | ||
else | ||
{ | ||
var pairVar = AstExpression.Var("pair"); | ||
var computedDocumentAst = AstExpression.ComputedDocument([ | ||
AstExpression.ComputedField("k", AstExpression.GetField(pairVar, keyMemberSerializationInfo.ElementName)), | ||
AstExpression.ComputedField("v", AstExpression.GetField(pairVar, valueMemberSerializationInfo.ElementName)) | ||
]); | ||
|
||
collectionTranslationAst = AstExpression.Map(collectionTranslation.Ast, pairVar, computedDocumentAst); | ||
} | ||
} | ||
else | ||
{ | ||
throw new ExpressionNotSupportedException(expression); | ||
} | ||
|
||
if (keySerializer is not IRepresentationConfigurable { Representation: BsonType.String }) | ||
{ | ||
throw new ExpressionNotSupportedException(expression, because: "key does not serialize as a string"); | ||
} | ||
|
||
var ast = AstExpression.Unary(AstUnaryOperator.ArrayToObject, collectionTranslationAst); | ||
var resultSerializer = CreateResultSerializer(keySerializer, valueSerializer); | ||
return new TranslatedExpression(expression, ast, resultSerializer); | ||
} | ||
|
||
private static IBsonSerializer CreateResultSerializer(IBsonSerializer keySerializer, IBsonSerializer valueSerializer) | ||
{ | ||
var dictionaryType = typeof(Dictionary<,>).MakeGenericType(keySerializer.ValueType, valueSerializer.ValueType); | ||
var serializerType = typeof(DictionaryInterfaceImplementerSerializer<,,>).MakeGenericType(dictionaryType, keySerializer.ValueType, valueSerializer.ValueType); | ||
|
||
return (IBsonSerializer)Activator.CreateInstance(serializerType, DictionaryRepresentation.Document, keySerializer, valueSerializer); | ||
} | ||
} | ||
} |
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.
nit: this line is indented too far.