forked from mongodb/mongo-csharp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializerFinderVisitMember.cs
More file actions
229 lines (206 loc) · 11 KB
/
SerializerFinderVisitMember.cs
File metadata and controls
229 lines (206 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* 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;
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.Misc;
using MongoDB.Driver.Linq.Linq3Implementation.Serializers;
namespace MongoDB.Driver.Linq.Linq3Implementation.SerializerFinders;
internal partial class SerializerFinderVisitor
{
protected override Expression VisitMember(MemberExpression node)
{
IBsonSerializer containingSerializer;
var member = node.Member;
var declaringType = member.DeclaringType;
var memberName = member.Name;
base.VisitMember(node);
if (IsNotKnown(node))
{
var containingExpression = node.Expression;
if (IsKnown(containingExpression, out containingSerializer))
{
// TODO: are there are other cases that still need to be handled?
var resultSerializer = node.Member switch
{
_ when typeof(BsonDocument).IsAssignableFrom(declaringType) => UnknowableSerializer.Create(node.Type), // we do not support translation of BsonDocument properties
_ when declaringType == typeof(BsonValue) => GetBsonValuePropertySerializer(),
_ when IsCollectionCountOrLengthProperty() => GetCollectionCountOrLengthPropertySerializer(),
_ when declaringType == typeof(DateTime) => GetDateTimePropertySerializer(),
_ when declaringType.IsConstructedGenericType && declaringType.GetGenericTypeDefinition() == typeof(Dictionary<,>) => GetDictionaryPropertySerializer(),
_ when declaringType.IsConstructedGenericType && declaringType.GetGenericTypeDefinition() == typeof(IDictionary<,>) => GetIDictionaryPropertySerializer(),
_ when declaringType.IsNullable() => GetNullablePropertySerializer(),
_ when declaringType.IsTupleOrValueTuple() => GetTupleOrValueTuplePropertySerializer(),
_ => GetPropertySerializer()
};
AddNodeSerializer(node, resultSerializer);
}
}
return node;
IBsonSerializer GetBsonValuePropertySerializer()
{
return memberName switch
{
"AsBoolean" => BooleanSerializer.Instance,
"AsBsonArray" => BsonArraySerializer.Instance,
"AsBsonBinaryData" => BsonBinaryDataSerializer.Instance,
"AsBsonDateTime" => BsonDateTimeSerializer.Instance,
"AsBsonDocument" => BsonDocumentSerializer.Instance,
"AsBsonJavaScript" => BsonJavaScriptSerializer.Instance,
"AsBsonJavaScriptWithScope" => BsonJavaScriptWithScopeSerializer.Instance,
"AsBsonMaxKey" => BsonMaxKeySerializer.Instance,
"AsBsonMinKey" => BsonMinKeySerializer.Instance,
"AsBsonNull" => BsonNullSerializer.Instance,
"AsBsonRegularExpression" => BsonRegularExpressionSerializer.Instance,
"AsBsonSymbol" => BsonSymbolSerializer.Instance,
"AsBsonTimestamp" => BsonTimestampSerializer.Instance,
"AsBsonUndefined" => BsonUndefinedSerializer.Instance,
"AsBsonValue" => BsonValueSerializer.Instance,
"AsByteArray" => ByteArraySerializer.Instance,
"AsDecimal128" => Decimal128Serializer.Instance,
"AsDecimal" => DecimalSerializer.Instance,
"AsDouble" => DoubleSerializer.Instance,
"AsGuid" => GuidSerializer.StandardInstance,
"AsInt32" => Int32Serializer.Instance,
"AsInt64" => Int64Serializer.Instance,
"AsLocalTime" => DateTimeSerializer.LocalInstance,
"AsNullableBoolean" => NullableSerializer.NullableBooleanInstance,
"AsNullableDecimal128" => NullableSerializer.NullableDecimal128Instance,
"AsNullableDecimal" => NullableSerializer.NullableDecimalInstance,
"AsNullableDouble" => NullableSerializer.NullableDoubleInstance,
"AsNullableGuid" => NullableSerializer.NullableStandardGuidInstance,
"AsNullableInt32" => NullableSerializer.NullableInt32Instance,
"AsNullableInt64" => NullableSerializer.NullableInt64Instance,
"AsNullableLocalTime" => NullableSerializer.NullableLocalDateTimeInstance,
"AsNullableObjectId" => NullableSerializer.NullableObjectIdInstance,
"AsNullableUniversalTime" => NullableSerializer.NullableUtcDateTimeInstance,
"AsObjectId" => ObjectIdSerializer.Instance,
"AsRegex" => RegexSerializer.RegularExpressionInstance,
"AsString" => StringSerializer.Instance,
"AsUniversalTime" => DateTimeSerializer.UtcInstance,
// TODO: return UnknowableSerializer???
_ => throw new ExpressionNotSupportedException(node, because: $"Unexpected member name: {memberName}")
};
}
IBsonSerializer GetCollectionCountOrLengthPropertySerializer()
{
return Int32Serializer.Instance;
}
IBsonSerializer GetDateTimePropertySerializer()
{
return memberName switch
{
"Date" => DateTimeSerializer.Instance,
"Day" => Int32Serializer.Instance,
"DayOfWeek" => new EnumSerializer<DayOfWeek>(BsonType.Int32),
"DayOfYear" => Int32Serializer.Instance,
"Hour" => Int32Serializer.Instance,
"Millisecond" => Int32Serializer.Instance,
"Minute" => Int32Serializer.Instance,
"Month" => Int32Serializer.Instance,
"Now" => DateTimeSerializer.Instance,
"Second" => Int32Serializer.Instance,
"Ticks" => Int64Serializer.Instance,
"TimeOfDay" => new TimeSpanSerializer(BsonType.Int64, TimeSpanUnits.Milliseconds),
"Today" => DateTimeSerializer.Instance,
"UtcNow" => DateTimeSerializer.Instance,
"Year" => Int32Serializer.Instance,
// TODO: return UnknowableSerializer???
_ => throw new ExpressionNotSupportedException(node, because: $"Unexpected member name: {memberName}")
};
}
IBsonSerializer GetDictionaryPropertySerializer()
{
if (containingSerializer.GetValueSerializerIfWrapped() is not IBsonDictionarySerializer dictionarySerializer)
{
throw new ExpressionNotSupportedException(node, because: "dictionary serializer does not implement IBsonDictionarySerializer");
}
var keySerializer = dictionarySerializer.KeySerializer;
var valueSerializer = dictionarySerializer.ValueSerializer;
return memberName switch
{
"Keys" => DictionaryKeyCollectionSerializer.Create(keySerializer, valueSerializer),
"Values" => DictionaryValueCollectionSerializer.Create(keySerializer, valueSerializer),
_ => throw new ExpressionNotSupportedException(node, because: $"Unexpected member name: {memberName}")
};
}
IBsonSerializer GetIDictionaryPropertySerializer()
{
if (containingSerializer is not IBsonDictionarySerializer dictionarySerializer)
{
throw new ExpressionNotSupportedException(node, because: "IDictionarySerializer does not implement IBsonDictionarySerializer");
}
var keySerializer = dictionarySerializer.KeySerializer;
var valueSerializer = dictionarySerializer.ValueSerializer;
return memberName switch
{
"Keys" => ICollectionSerializer.Create(keySerializer),
"Values" => ICollectionSerializer.Create(valueSerializer),
_ => throw new ExpressionNotSupportedException(node, because: $"Unexpected member name: {memberName}")
};
}
IBsonSerializer GetNullablePropertySerializer()
{
return memberName switch
{
"HasValue" => BooleanSerializer.Instance,
"Value" => (containingSerializer as INullableSerializer)?.ValueSerializer,
// TODO: return UnknowableSerializer???
_ => throw new ExpressionNotSupportedException(node, because: $"Unexpected member name: {memberName}")
};
}
IBsonSerializer GetPropertySerializer()
{
if (containingSerializer is IBsonDocumentSerializer documentSerializer &&
documentSerializer.TryGetMemberSerializationInfo(memberName, out var memberSerializationInfo))
{
return memberSerializationInfo.Serializer;
}
return UnknowableSerializer.Create(node.Type);
}
IBsonSerializer GetTupleOrValueTuplePropertySerializer()
{
if (containingSerializer is not IBsonTupleSerializer tupleSerializer)
{
throw new ExpressionNotSupportedException(node, because: $"serializer type {containingSerializer.GetType()} does not implement the {nameof(IBsonTupleSerializer)} interface");
}
return memberName switch
{
"Item1" => tupleSerializer.GetItemSerializer(1),
"Item2" => tupleSerializer.GetItemSerializer(2),
"Item3" => tupleSerializer.GetItemSerializer(3),
"Item4" => tupleSerializer.GetItemSerializer(4),
"Item5" => tupleSerializer.GetItemSerializer(5),
"Item6" => tupleSerializer.GetItemSerializer(6),
"Item7" => tupleSerializer.GetItemSerializer(7),
"Rest" => tupleSerializer.GetItemSerializer(8),
// TODO: return UnknowableSerializer???
_ => throw new ExpressionNotSupportedException(node, because: $"Unexpected member name: {memberName}")
};
}
bool IsCollectionCountOrLengthProperty()
{
return
(declaringType.ImplementsInterface(typeof(IEnumerable)) || declaringType == typeof(BitArray)) &&
node.Type == typeof(int) &&
(member.Name == "Count" || member.Name == "Length");
}
}
}