CSHARP-4779: Support Dictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection) constructor in LINQ… - #1657
Conversation
|
|
||
| var stages = Translate(collection, queryable); | ||
|
|
||
| AssertStages(stages, "{ $project : { _v : { $arrayToObject : [[{ k : 'A', v : '$A' }, { k : 'B', v : '$B' }]] }, _id : 0 } }"); |
There was a problem hiding this comment.
A further simplifcation could turn this into:
{ $project : { _v : { A : "$A", B : "$B" }, _id : 0 } }
When all the k values passed to $arrayToObject are string constants you can do this simplification.
adelinowona
left a comment
There was a problem hiding this comment.
Overall looks good to me, just left some minor comments.
| // [Fact] | ||
| // public void NewDictionary_with_KeyValuePairs_Create_should_translate() | ||
| // { | ||
| // var collection = Fixture.Collection; | ||
| // | ||
| // var queryable = collection.AsQueryable() | ||
| // .Select(d => new Dictionary<string, string>( | ||
| // new[] { KeyValuePair.Create("A", d.A), KeyValuePair.Create("B", d.B) })); | ||
| // | ||
| // var stages = Translate(collection, queryable); | ||
| // | ||
| // AssertStages(stages, "{ $project : { _v : { $arrayToObject : [[{ k : 'A', v : '$A' }, { k : 'B', v : '$B' }]] }, _id : 0 } }"); | ||
| // | ||
| // var result = queryable.Single(); | ||
| // result.Should().Equal(new Dictionary<string, string>{ ["A"] = "a", ["B"] = "b" }); | ||
| // } |
There was a problem hiding this comment.
I've created another ticket to add support of KeyValuePair.Create. Then will uncomment this test. But thank you for reminding me not to merge like this =)
There was a problem hiding this comment.
This code is uncommented now.
| if (keySerializer is not IRepresentationConfigurable representationConfigurableSerializer | ||
| || representationConfigurableSerializer.Representation != BsonType.String) |
There was a problem hiding this comment.
could be simplified to if (keySerializer is not IRepresentationConfigurable { Representation: BsonType.String })
| if (itemSerializationInfo.Serializer is IRepresentationConfigurable representationConfigurable && | ||
| representationConfigurable.Representation == BsonType.Array) |
There was a problem hiding this comment.
could also be simplified to if (itemSerializationInfo.Serializer is IRepresentationConfigurable { Representation: BsonType.Array })
|
|
||
| var collectionExpression = arguments.Single(); | ||
| var collectionTranslation = ExpressionToAggregationExpressionTranslator.TranslateEnumerable(context, collectionExpression); | ||
| AstExpression collectionTranslationAst; |
There was a problem hiding this comment.
nit: move this declaration inside the first if statement below
…>> collection) constructor in LINQ3
rstam
left a comment
There was a problem hiding this comment.
Discussed suggested changes offline.
rstam
left a comment
There was a problem hiding this comment.
Two small changes suggested.
|
|
||
| namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators | ||
| { | ||
| internal static class NewDictionaryExpressionToAggregationExpressionTranslator |
There was a problem hiding this comment.
nit: this line is indented too far.
| { | ||
| public static bool CanTranslate(NewExpression expression) | ||
| => expression.Type.IsConstructedGenericType && | ||
| expression.Type.GetGenericTypeDefinition() == typeof(Dictionary<,>) && |
There was a problem hiding this comment.
I think the checks in line 31-32 should actually be in IsWithIEnumerableKeyValuePairConstructor, not here.
You should be able to call DictionaryConstructor.IsWithIEnumerableKeyValuePairConstructor with ANY ConstructorInfo and it should return the correct answer.
No description provided.