Skip to content

Conversation

@rstam
Copy link
Contributor

@rstam rstam commented Nov 7, 2025

No description provided.

@rstam rstam requested a review from adelinowona November 7, 2025 23:51
@rstam rstam added the improvement Optimizations or refactoring (no new features or fixes). label Nov 7, 2025
@rstam rstam requested a review from a team as a code owner November 7, 2025 23:51
return new AstComputedDocumentExpression(fields);
}

public static AstExpression ComputedDocument(IEnumerable<(string Name, AstExpression Value)> fields)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more convenient overload of ComputedDocument.

}
}

internal class DictionaryKeyCollectionSerializer<TKey, TValue> : EnumerableSerializerBase<Dictionary<TKey, TValue>.KeyCollection>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a serializer for Dictionary<TKey, TValue>.KeyCollection to be able to work with this type in LINQ queries or to deserialize them client-side.

}
}

internal class DictionaryValueCollectionSerializer<TKey, TValue> : SerializerBase<Dictionary<TKey, TValue>.ValueCollection>, IBsonArraySerializer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a serializer for Dictionary<TKey, TValue>.ValueCollection to be able to work with this type in LINQ queries or to deserialize them client-side.

}
}

internal class DictionarySerializer<TKey, TValue> : DictionarySerializerBase<Dictionary<TKey, TValue>, TKey, TValue>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a more convenient way to create a serializer for Dictionary<TKey, TValue>.

}
}

internal class ICollectionSerializer<TItem> : EnumerableSerializerBase<ICollection<TItem>>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a more convenient way to create a serializer for ICollection<TItem>.

}
}

internal class KeyValuePairWrappedValueSerializer<TKey, TValue> : SerializerBase<TValue>, IWrappedValueSerializer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a special serializer that deserializes a KeyValuePair but then extracts only the Value portion of it as the result.

It also represents values that are still coupled with the corresponding key as needed to work with a Dictionary<TKey, TValue>.ValueCollection.

{
// public static methods
public static TranslatedExpression Translate(TranslationContext context, Expression expression)
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever we want to use a value we go ahead and unwrap it if it is wrapped.


var ast = AstExpression.ObjectToArray(aggregateExpression.Ast);
var ienumerableSerializer = ArraySerializerHelper.CreateSerializer(keyValuePairSerializer);
var arrayOfDocumentsDictionarySerializer = DictionarySerializer.Create(DictionaryRepresentation.ArrayOfDocuments, keySerializer, valueSerializer);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were creating the "wrong" serializer before.

case "Values":
if (declaringTypeDefinition == typeof(Dictionary<,>))
{
var kvpPairsAst = dictionaryRepresentation switch
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tricky part about supporting Values is that we have to keep the keys that go with them, at least until some later time where the keys might no longer be needed.

}
else if (declaringTypeDefinition == typeof(IDictionary<,>))
{
var valuesAst = dictionaryRepresentation switch
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supporting Values for IDictionary<TKey, TValue> is MUCH more straightforward because the type is ICollection<TValue>.

@rstam rstam requested a review from sanych-sun November 8, 2025 00:02
case "Keys":
var keysAst = dictionaryRepresentation switch
{
DictionaryRepresentation.ArrayOfDocuments => AstExpression.Map(containerAst, kvpVar, AstExpression.GetField(kvpVar, "k")),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the time we get here we will never encounter DictionaryRepresentation.Document, because the call to TranslateEnumerable on line 204 above will have already used $objectToArray to transform the representation from Document to ArrayOfDocuments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Optimizations or refactoring (no new features or fixes).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant